在做事件研究法,前两天发了求助帖http://bbs.pinggu.org/thread-1192259-1-1.html,问题是想把每个事件日的数据与距事件日最近的财务数据(如EPS)对应起来
今天终于把问题解决了,基本的想法是用date命令把事件日以及财务数据公布日的天数列出来,然后进行区分,借鉴了网站上教的事件研究法处理初始数据的方法(详见http://dss.princeton.edu/online_help/stats_packages/stata/eventstudydataprep.html)想来还是把用到的命令贴出来,希望对童鞋们有用~
use 用来做每个减持事件日对应最近的EPS的.dta,clear
sort stock_code event_date
by stock_code event_date:gen n=_n
keep if n==1
save eventdates
by stock_code: gen eventcount=_N
drop n
by stock_code: keep if _n==1
by stock_code: gen eventcount=_N
drop n
by stock_code: keep if _n==1
sort stock_code
keep stock_code eventcount
save eventcount
use 只有EPS每季度的数据.dta, clear
sort stock_code
merge m:m stock_code using eventcount
use 只有EPS每季度的数据.dta, clear
sort stock_code
merge m:m stock_code using eventcount
keep if _merge==3
drop _merge
expand eventcount
drop eventcount
sort stock_code accounting_period
by stock_code accounting_period: gen set=_n
sort stock_code set accounting_period
save stockdata2
use eventdates, clear
drop n
by stock_code: gen set=_n
sort stock_code set
save eventdates2
use stockdata2, clear
merge m:m stock_code set using eventdates2
keep if _merge==3
drop _merge
egen group_id = group( stock_code set)
save 处理好的每个减持公告日对应了全部的EPS
use 处理好的每个减持公告日对应了全部的EPS.dta
gen date1=date( event_date,\
gen date2=date( accounting_period,\
gen dif1= date1- date2
sort group_id accounting_period
by group_id:egen dif2=min( dif1) if dif1>=0
keep if dif1== dif2
save 已经把每个减持公告日对应了距离最近的每股收益数据
use 做子市场反应的起点.dta,clear
drop set volume percent group_id p target
merge m:m stock_code event_date using 已经把每个减持公告日对应了距离最近的每股收益数据.dta
keep if _merge==3
drop set group_id dif1 dif2 _merge
drop date1 date2
sort stock_code event_date trading_date
egen group_id=group( stock_code event_date)
save 做不同水平EPS的基础数据
Data Preparation for Event Studies using Stata
Preparing your own data
You may have downloaded datasets for an event study, or created ones by entering data into excel sheets. Usually people have two files, one for stock returns, and the other for your event of interest. In this example, we start with two data sets, one called eventdates and the other called
stockdata. In the eventdates data, we have company id (company_id) and the date of event (event_date) as variables. In the stock data, we have matching company id (company_id), stock return date (date), stock return (ret), and market return value (market_return).
If a set of observations for each company can be matched to a single event date, the study will be much simpler. In some situations one may wish to examine more than one event date for each company. In multiple
observations per company, it is necessary to create a duplicate set of observations for each event date/company combination. You need a full set of stock observations to examine each event. If you are not sure how many events per company you have on your dataset, we recommend that you go through this exercise to check the number of events per company. If you already know that you have only one event per company, you may skip the instruction below, merge the eventdate and stockdata data files and go to the Event Study with Stata page.
Using example data
Alternatively, you may try the commands in our event studies example using our sample data set. There are two data sets: one called eventdates, that contain event information, and the other called stockdata. The events, in this example, are merger announcement dates for 2007 obtained from SDC Platinum. The stock return data for 2007 were obtained from CRSP daily stock.
The computer you are using Stata needs to be connected to the internet for this download to work. Please be patient with the download. You can see a counter on the bottom of Stata window which shows how much percent of the file has been downloaded. Once you finish the download, save the data on your computer or where you have write permission, like thesis folder in your H drive. Make sure that you have enough space in your drive to save these data files.
set memory 200m use http://dss.princeton.edu/sampleData/eventdates.dta /* about 11k */
save H:/thesis/eventdates use http://dss.princeton.edu/sampleData/stockdata.dta, clear /* about 90m */
save H:/thesis/stockdata
Combining event and stock data
First, set memory to a large enough size so that you can do the rest of the operations below. We will be creating some variables and possibly duplicating cases, so the dataset can get VERY BIG. To check how much memory you have allocated, the command is query, and to check how big your file is, the command is describe.
Now, we need to find out how many event dates there are for each company. Use the dataset of event dates and generate a variable that counts the number of event dates per company.
use eventdates, clear
by company_id: gen eventcount=_N
Cut the dataset down to just one observation for each company. Each company observation is associated with the count of event dates for that company. Save this as a new dataset - don't overwrite your dataset of event dates!
by company_id: keep if _n==1 sort company_id
keep company_id eventcount save eventcount
The next step is to merge the new 'eventcount' dataset with your dataset of stock data.
use stockdata, clear sort company_id
merge company_id using eventcount tab _merge
keep if _merge==3 drop _merge
Now use Stata's 'expand' command to create the duplicate observations. The 'eventcount' variable has been merged on to each stock observation, and tells Stata how many copies of that observation are needed. This is where your dataset can get VERY BIG, as we are duplicating the observations to however many counts of event we have per company.
expand eventcount
You need to create a variable that indicates which 'set' of observations