Some speed-ups are certainly possible.
* Using egen to get maximum, minimum, totals is very inefficient compared with using summarize, meanonly.
* There's no need to scale analytic weights. Stata does that for you.
There are some puzzles.
* This won't work well unless your firms are already numbered 1 up, in which case you can find out directly how many there are by looking at the values of firm
* You probably don't need to replace the weights nearly so often.
Code is completely untested. Check very very carefully!
* Using egen to get maximum, minimum, totals is very inefficient compared with using summarize, meanonly.
* There's no need to scale analytic weights. Stata does that for you.
There are some puzzles.
* This won't work well unless your firms are already numbered 1 up, in which case you can find out directly how many there are by looking at the values of firm
* You probably don't need to replace the weights nearly so often.
Code is completely untested. Check very very carefully!
Code:
clear all cd "C:\Users\md_kh\Dropbox\ECF\Codes" use "stock_inflation_data.dta", clear * create "time" variable which is consecutive date of observations bysort permno (date): gen time = _n order permno firm date time dur shrout price exret inflation * variable related to the regressions gen a = . gen b = . gen w = . gen n = . gen w1 = . * finding numbers of firms by permno, sort: gen nvals = _n == 1 replace nvals = sum(nvals) /* the last value is sum of the distinct permnos */ local firm_number = nvals[_N] drop nvals quietly forval j = 1/`firm_number' { * finding max and min of time for each firm j (min for all of them is 1) su time if firm == `j', meanonly forval T = `r(min)'/`r(max)' { count if exret < . (time < `T') & (time >= `T'-12) & firm ==`j' if r(N) < 4 continue replace w1 = exp(-(`T' - time)* log(2)/60) if `T' > time & firm == `j' capture { regress exret inflation [aweight=w] if firm == `j' replace a = _b[_cons] if firm == `j' & time == `T' replace b = _b[inflation] if firm == `j' & time == `T' replace n = e(N) if firm == `j' & time == `T' } } } save "beta.dta"
Code:
Comment