1
votes

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"

    }
1

1 Answers

0
votes

A bundle of comments purporting to be an answer:

The program is called Stata, not STATA. This is elementary.

xtfbm is presumably a typo for xtfmb. Either way, I have never used it, or even looked at it, so refrain from trying to advise on it specifically.

More interestingly, rolling does support panel set-ups. Here is a dopey example you can try:

webuse grunfeld 
xtset 
rolling _b, window(10): regress invest year

As your first code block appears to be trying something similar, I am not clear what you are asking there.

You ask "Is it even possible to save local variables as dates?" This is backwards, but I think I understand your meaning. Stata dates are just numbers, so you can save them to local macros (which in Stata are not regarded as variables). This would be legal:

local t = ym(1986, 7) 
while `t' <= ym(2005, 7) {
    ...
    local T = `t' + 12 
    ...
    local ++t 
}

and this would be better:

forval t = `=ym(1986, 7)'/`=ym(2005, 7)' {
    ...
    local T = `t' + 12 
    ...
}

which might well seem clearer as

local t1 = ym(1986, 7) 
local t2 = ym(2005, 7) 
forval t = `t1'/`t2' {
    ...
    local T = `t' + 12 
    ...
}

but my advice would still be to try rolling, which does all this for you.