# multiple Rolling regression

#### Raphman

##### New Member
Dear STATA- User,

Im using STATA while writing my thesis and have one problem:

I regress Jensen Alphas for 303 funds while 200 days
and I need for every day and every fund one alpha,
so I would get a list of 303 funds with 200 Alphas.
If I use the command:
foreach fonds of varlist *** {
rolling _b, window(52) saving(dateiname, replace): reg fonds' rm smb hml mom
}

I can save in a new stata- file only one _b of fund1
how can i save all _b from fund1 to fund303.

I would be very happy, if you can help me by saving intercepts.

Thanks a lot for any help!

Best regards

Raphael Schuppener

#### bukharin

##### RoboStataRaptor
Maybe something like:
Code:
tempfile allresults results
foreach fonds of varlist *** {
rolling _b, window(52) saving(results', replace): reg fonds' rm smb hml mom
preserve
use results', clear
gen fonds="fonds'"
capture append using allresults'
save allresults', replace
restore
}

use allresults', clear

Last edited:

#### Raphman

##### New Member
Hey,

thanks a lot for the quick answer, wow.
But:
I do not understand the code exactly, because of:
Where would STATA save the answer? In the current
STATA- file?
By running the program:
STATA regress every fund (thats good), but at the end after
the end of do file!!?? - I get the answer:
time variable not set, when STATA want to run the temp- file.

Do you know the answer, how to open or use the file?

#### bukharin

##### RoboStataRaptor
Code:
tempfile allresults results
This tells Stata that you want to use 2 temporary files.

Code:
foreach fonds of varlist *** {
This loops through every variable in the dataset. For each iteration, fonds' contains the name of the current variable. Doing this will cause some problems because sometimes fonds' will be rm, smb, hml or mom - is that what you're actually trying to do? Perhaps you want something more like -foreach fonds of varlist fund*-

Code:
rolling _b, window(52) saving(results', replace): reg fonds' rm smb hml mom
This runs the rolling regression of fonds' on rm, smb, hml & mom and saves the result in the temporary file called results'

Code:
preserve
use results', clear
gen fonds="fonds'"
capture append using allresults'
save allresults', replace
restore
}
The -preserve- stores a quick copy of the current dataset. The -use- opens the results of the rolling regression above. We then generate a new variable called "fonds" which contains the name of the current fonds' variable, so that we know which iteraction is active. -capture append- then adds all of the results from previous iterations. You need -capture- because it won't work the first time, since allresults' has never been saved. -restore- then takes us back to the copy of the current dataset that was preserved for us when we used -preserve-.

Code:
use allresults', clear
This clears the current dataset from memory and replaces it with the contents of the allresults' dataset which we built up using the loop.

#### Raphman

##### New Member
Yeah I want to regress fonds*

But now I understand, the results
are all in one line in the new
document.
Thanks a lot for the exact explanation.

#### AlexWBSmsfin

##### New Member
Hi bukharin,

I am new to STATA and had the same question, thank you very much for your help. It works perfectly.

I was wondering, do you know a command I could use to concatenate the results in different columns?

The code is very close to yours:

tempfile allresults results
foreach premium of varlist *** {
rolling _b, window(20) stepsize(1) recursive saving(results', replace): regress premium' L.bm
preserve
use results', clear
generate premium="premium'"
capture append using allresults'
save allresults', replace
restore
}
use allresults', clear

The matrix of results looks like:

Start - End - _b[] - _b_cons - label
obs1
obs2

...and I would like it to look like
Start - End - _b[nodurp] - _b_cons[nodurp] - .... - _b[Equitypremium] - _b_cons[EquityPremium]
obs1
obs2

Thank you again for you help.

Best,

Alex

Last edited:

#### bukharin

##### RoboStataRaptor
Hi Alex, welcome to Stata

This looks like you need to take your final results dataset and -reshape wide- (see -help reshape-)