这就是我对滞后变量数据进行排序的方式
tsset permno date, monthly
sort permno date
by permno: gen lagret1=ret[_n-1]
by permno: gen lagret2=ret[_n-2]
by permno: gen lagret3=ret[_n-3]
by permno: gen lagret4=ret[_n-4]
by permno: gen lagret5=ret[_n-5]
我什么都不知道
答案 0 :(得分:0)
*Step 1: Upload the data and create key variables
*Upload the dataset that contains CRSP information and create key variables.
use "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/CRSPforMOM.dta", clear
*Keep only common stock
keep if shrcd == 10 | shrcd == 11
*Create monthindex variable
gen monthindex = year(date)*12+month(date)
*Create past 5 months of returns using lag function
*in order to use the built- in lag function I need to tell stata the
*structure of the data
tsset permno date, monthly
sort permno date
by permno: gen lagret1=ret[_n-1]
by permno: gen lagret2=ret[_n-2]
by permno: gen lagret3=ret[_n-3]
by permno: gen lagret4=ret[_n-4]
by permno: gen lagret5=ret[_n-5]
*Create a variable that captures cumulative retruns of stock i,
*from month -5 through current month
*Compounding requires multiplying consecutive returns
gen cumret6 = (1+ret)*(1+lagret1)*(1+lagret2)*(1+lagret3)*(1+lagret4)* (1+lagret5)
*Save
save "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM1.dta", replace
*Step 2: Create and apply filters
*Before allocating stocks to portfolios, we should create and apply filters
*Select only NYSE stocks and find the 10th percentil of NYSE size in each month
use "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM1.dta", clear
*Keep only NYSE stocks
keep if exchcd == 1
*Keep if market cap is larger than 0
keep if mktcap >0
*Drop missing observations where marketcap is missing
drop if missing(mktcap)
*Since we create portfolios monthly, we need breakpoints monthly
sort date
by date: egen p10=pctile(mktcap), p(10)
*We only need date variable (for merging) and p10 variable (as a filter),
*so we drop everything else
keep date p10
*Drop duplicates so that p10 repeats once for every month in the sample
duplicates drop
*save
save "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOMNYSEBreakpoints.dta", replace
*Merge the breakpoints into the dataset created in step 1,
*so that we can remove small firms
*Break points are date specific so merge on date
use "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM1.dta", clear
sort date
merge m:1 date using "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOMNYSEBreakpoints.dta"
*merge==3 indicates that an observation is present in both
*master and using datasets, that is the only data that is properly merged
*and the only data that should be kept
保持_merge == 3
*We need to drop _merge variable to be able to merge data again
拖放_merge
*Apply filters, i.e. remove small firms and firms priced below $5
如果丢失则丢弃(mktcap) 如果mktcap <= p10则下降 *使用绝对值,因为CRSP用负号表示BID-ASK中点 如果abs(prc)<5
则删除*Save
保存“ / Users / dk / Desktop / USD Documents / MSF / MFIN 518 / MOM2.dta”,替换
*Step 3: Allocate stocks in 10 portfolios and hold for 6 months
*Use new file
use "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM2.dta", clear
排序日期
*We will create variable prret6, which will tell us which portfolio a stock
*belongs to based on cumret 6
*We will use command xtile puts a prespecified percent of firms into
*each portfolio
*nq() tells stata how many portfolios we want
by date: egen prret6 = xtile (cumret6), nq(10) // takes ~20min to run
*Save
save "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM3.dta", replace
*Use the portfolios
use "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM3.dta", clear
drop if missing(prret6)
*Expand data, i.e. create 6 copies of the data
展开6
sort permno date
*Create variable n which trackswhat copy of the data it is,
*n will go from 1 to 6
*_n is the count for the dataset/ the number for each observation
by permno date: gen n=_n
*Use n variable to increment monthindex by 1
replace monthindex = monthindex+n
sort permno monthindex
*Drop return from the master dataset because we want the one from the
*using dataset
drop ret
merge m:1 permno monthindex using "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM1.dta"
keep if _merge==3
drop _merge
save "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM4.dta", replace
*Step 4: Analysis
use "/Users/dk/Desktop/USD Documents/MSF/MFIN 518/MOM4.dta", clear
sort monthindex prret6 date
*Average returns based on each portfolio in each calendar month and by
*formation month
collapse (mean) ret, by (monthindex prret6 date)
*Summarize again to get average portfolio returns by calendar month (monthindex)
collapse (mean) ret, by (monthindex prret6)
*Transpose the data
reshape wide ret, i(monthindex) j(prret6) // i(rows) j(columns)
*Generate year and month variable for clarity
gen year= round(monthindex/12)
gen month=(monthindex-year*12)+6
*create momentum return variable and check for significance
gen momret=ret10-ret1
ttest ret10=ret1
*testing momentum returns from year 2000 onward
keep if monthindex>=24000
ttest ret10=ret1