use stock,clear
sort company
merge m:m company using event
drop if _merge==2
drop _merge
save eventda,replace
use eventda
sort company date
by company:gen datenum=_n
by company:gen target=datenum if date==event
egen td =min(target),by (company)
gen dif=datenum-td
by company:gen event_window=1 if (dif>=-2 & dif<=2)
egen count_event_obs=count(event_window),by(company)
by company:gen estimation_window=1 if dif>=-60 & dif<=-30
egen count_est_obs=count(estimation_window),by(company)
drop if count_event_obs<5
drop if count_est_obs<30
replace event_window=0 if event_window==.
replace estimation_window=0 if estimation_window==.
gen predicted_return=.
egen id=group(company)
local N=r(r)
出现问题:
forvalues i=1(1)N{
l id company if id=='i'&dif==0
reg ret mktreturn if id=='i' & estimation_window==1
predict p if id=='i'
replace predicted_return=p if id=='i'&event_window==1
drop p
}