正在做硕士论文,用到stata做时间研究法。之前对stata不太了解。我这里有些stata做事件研究法的代码和样本数据,但是感觉循环处的代码有些问题,错误显示invalid syntax 请版主帮我解答一下。
use D:\mystata\eventdates
sort company_id
by company_id: gen eventcount=_N
by company_id: keep if _n==1
keep company_id eventcount
save D:\mystata\eventcount
use D:\mystata\stockdata, clear
sort company_id
merge company_id using D:\mystata\eventcount
tab _merge
keep if _merge==3
drop _merge
expand eventcount
drop eventcount
sort company_id date
by company_id date: gen set=_n
sort company_id set
save D:\mystata\stockdata2
use D:\mystata\eventdates, clear
sort company_id
by company_id: gen set=_n
sort company_id set
save D:\mystata\eventdates2
use D:\mystata\stockdata2, clear
merge company_id set using D:\mystata\eventdates2
tab _merge
list company_id if _merge==2
keep if _merge==3
drop _merge
egen group_id = group(company_id set)
/*group()是把group中的变量看作n维数组,按照取值的不同组合进行编号*/
sort company_id date
by company_id: gen datenum=_n
by company_id: gen target=datenum if date==event_date
egen td=min(target), by(company_id)
drop target
gen dif=datenum-td
by company_id: gen event_window=1 if dif>=-2 & dif<=2
egen count_event_obs=count(event_window), by(company_id)
by company_id: gen estimation_window=1 if dif<-30 & dif>=-60
egen count_est_obs=count(estimation_window), by(company_id)
replace event_window=0 if event_window==.
replace estimation_window=0 if estimation_window==.
tab company_id if count_event_obs<5
tab company_id if count_est_obs<30
drop if count_event_obs < 5
drop if count_est_obs < 30
set more off
gen predicted_return=.
egen id=group(company_id)
forvalues i=1(1)N {
1 id company_id if id==`i'&dif==0
reg ret market_return if id==`i'& estimation_window==1
predict p if id==`i'
replace predicted_return=p if id==`i'&event_window==1
drop p
}
sort id date
gen abnormal_return=ret-predicted_return if event_window==1
by id: egen cumulative_abnormal_return = sum(abnormal_return)
sort id date
by id: egen ar_sd = sd(abnormal_return)
gen test =(1/sqrt(number of days in event window)) * ( cumulative_abnormal_return /ar_sd)
list company_id cumulative_abnormal_return test if dif==0
outsheet using D:/mystata/company_id event_date cumulative_abnormal_return test using stats.csv if dif==0, comma names