st: Better merge match-algorithm?

 From "Tobias Pfaff" To Subject st: Better merge match-algorithm? Date Fri, 3 Aug 2007 09:54:46 +0200

```Dear Statalisters,

I encountered the following problem in a finance research project: two
tables, one with option prices, the other with (underlying) stock prices.
The task is to match the appropriate stock price to each option price
observation. My current solution works, but seems to be inefficient due to
tremendous processing time (> 4h).

My current solution is the following (I refer to the following numbers in
the code below):
1) Fetch number of observations from underlying table.
2) Fetch number of obs. from option table
3) Merge the underlying prices to the option prices (one-to-one merge)
4) Using two nested forvalues loops, I iterate over the option observations
to find an appropriate underlying price again iterating over the underlying
prices in the second forvalues loop. [The matching criteria are an identical
subsequent underlying is bigger than the option trading time, i.e. looking
for the most recent underlying price.]

Before writing down my code, I would have the following questions:

A) IS THERE A MORE EFFICIENT WAY TO CARRY OUT THE CONDITIONAL MATCHING
WITHOUT HAVING TO ITERATE OVER EACH AND EVERY OBSERVATION ?

B) IF NOT, IS IT POSSIBLE TO 'OUTSOURCE' THE TASK TO A MATA PROGRAM, SUCH
THAT THE COMPILATION OF THE LOOP-CODE IS DONE ONCE INSTEAD OF A MILLION
TIMES ?

I thought about the Mata possibility when I read in a presentation by Kit
Baum:
invocations of the same commands. Stata's ado-file language is easy to read
and write, but it is interpreted: Stata must evaluate each statement and
translate it into machine code. The new Mata programming language (help
mata) creates compiled code which can run much faster than ado-file code."

Thanks a lot,
Tobias

University of Muenster,
Germany

My code:
-------------------------------------------------

preserve

use 01_Original/dta_files/_prepared_und_`year'`month'`day', clear
// cf. 1) Fetch number of observations from underlying table.
local obs_underlyings = _N
save underlyings_temp.dta, replace

restore
// cf. 2) Fetch number of obs. from option table
local obs_options = _N

// cf. 3) Merge the underlying prices to the option prices (one-to-one
merge)
merge using underlyings_temp
drop _merge
erase underlyings_temp.dta

gen double option_matched_underlying_price = .
gen option_matched_underlying_time = ""

// cf. 4) Using two nested forvalues loops, I iterate over the option
observations to find an appropriate
// underlying price again iterating over the underlying prices in the second
forvalues loop.
forvalues num = 1/`obs_options' {

forvalues num2 = 1/`obs_underlyings' {
if (underlying_isin[`num2'] == option_underlying_isin[`num']

quietly replace option_matched_underlying_price =
quietly replace option_matched_underlying_time =

continue, break		//exit loop once a matching
}
}
}

-------------------------------------------------

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```