╰不滅信念 发表于 2014-5-3 21:21 
这样的程序肯定是没错的,至于为什么语法错误,要把代码贴出来才能分析出来
运行结果:
capture program drop x
program x
forvalue i = 2 (i) 6 {
use "e:\match.dta",clear
sort Stkcd Trddt
bys Stkcd: gen datenum=_n
by Stkcd: gen target_1=datenum if i_n==`i'
egen td = min (target_1),by(Stkcd)
drop target_1
*find the interval day
gen dif=datenum-td
*choose the length of the event window
by Stkcd: gen event_window=1 if dif>=0 & dif <=21
forvalues j = 2(1)36{
by Stkcd: replace event_window=`j' if dif>=(`j'-1)*21+(`j'-1) & dif <=`j'*21+(`j'-1)
}
*caculate the length of the window for every company
egen count_event_obs=count(event_window),by(Stkcd)
sort Stkcd event_window
keep if event_window !=.
keep if count_event_obs==792
count if i_m==1
sort Stkcd Trddt
*2.3 caculate the buy and hold stock return
sort Stkcd event_window Trddt
by Stkcd event_window: gen return_stock=(Clsprc[_N]-Clsprc[1])/Clsprc[1]
by Stkcd event_window: gen return_stock1=return_stock+1
sort Stkcd event_window Trddt
by Stkcd event_window: gen return_stock2=return_stock1 if i_m ==1
by Stkcd event_window: replace return_stock2=1 if return_stock2==.
egen R1=prod(return_stock2),by(Stkcd)
keep if i_n==`i'
egen r1_sum = sum(R1)
saveold "E:\1992_nonipo_`i'",replace
}
end
x
程序是这样的,子LOOP是用来生成窗口的,母的LOOP是用来选择股票的,分别是2 ,3, 4 到6