clear
cd c:\stata
use eventdates1, clear
sort company_id
by company_id: gen eventcount=_N
by company_id: keep if _n==1
sort company_id
keep company_id eventcount
save eventcount
use stockdata1, clear
sort company_id
merge m:1 company_id using 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 stockdata2
use eventdates1, clear
sort company_id
by company_id: gen set=_n
sort company_id set
save eventdates2
use stockdata2, clear
merge m:1 company_id set using eventdates2
tab _merge
list company_id if _merge==2
keep if _merge==3
drop _merge
egen group_id = group(company_id set)
sort group_id date
by group_id: gen datenum=_n
by group_id: gen target=datenum if date==event_date
egen td=min(target), by(group_id)
drop target
gen dif=datenum-td
by group_id: gen event_window=1 if dif>=-10 & dif<=10
egen count_event_obs=count(event_window), by(group_id)
by group_id: gen estimation_window=1 if dif<-30 & dif>=-160
egen count_est_obs=count(estimation_window), by(group_id)
replace event_window=0 if event_window==.
replace estimation_window=0 if estimation_window==.
tab group_id if count_event_obs<21
tab group_id if count_est_obs<130
drop if count_event_obs <21
drop if count_est_obs < 130
set more off
browse /*程序的主体部分我有点搞不清是怎样循环的,为什么没有求77个样本平均值的过程呢?
gen predicted_return=.
egen id=group(group_id)
forvalues i=1(1)77{ /*样本中有77家公司 求大神解释一下经过筛选之后的样本数要怎么快速的看出来,我是自己数出来的
l id group_id if id==`i' & dif==0
reg ret market_ret if id==`i' & estimation_window==1
predict p if id==`i'
replace predicted_return = p if id==`i' & event_window==1
drop p
}
outreg2 using test.rtf
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(21)) * ( cumulative_abnormal_return /ar_sd)
list company_id abnormal_return cumulative_abnormal_return test if dif==0
outsheet company_id event_date abnormal_return cumulative_abnormal_return test using stats.csv if dif==0, comma name
sort dif
by dif: egen c_abnormal_return = sum(abnormal_return)
by dif: egen ar_sd1 = sd(abnormal_return)
gen test1 =(1/sqrt(77)) * ( c_abnormal_return /ar_sd1)
outsheet dif c_abnormal_return test1 using stats1.csv if event_window==1 , comma name
。