I have panel data in long format with a panel variable firm id
and a monthly time variable between 1986m7 and 2015m12.
I want to get trailing ten-year beta estimates of Fama-MacBeth regressions and store those in a new file to later plot them.
I want a beta for the fmb regression from 1986m7 until 1996m7, a beta for 1986m8 until 1996m8 and so on, with the last regression from 2005m12 to 2015m12.
I thought first I could do this with the rolling
command but I don't know how to tell Stata to only "roll" over time not id as well or if that is even possible with this command.
My second idea was to work with a while
loop. But I don't know how to tell Stata that after every loop it should up the local variable by one month. Is it even possible to save local variables as dates?
Maybe someone knows another way to solve my problem.
Also a solution to store those betas in a new file and append the new betas would be appreciated. I thought of storing them as scalars but then I don't know how to put the scalars back as a new variable. With the rolling command this problem would not be one since it creates a new variable for me.
What I have as code so far for the rolling idea, window(120)
because of the 120 month (10 year) rolling window:
use mydata, clear
ssc install xtfbm
xtset id date
rolling _b, clear window(120): xtfmb y x
What I have as code so far for the loop idea:
ssc install xtfmb
local i = tm(1986m7)
while `i' <= tm(2005m7) {
use mydata, clear
drop if date < tm(`i')
drop if date >= tm(`i' + "1year")
xtset id date
quietly xtfmb y x
scalar x`i' = _b[x]
local i = `i' + "1month"
}