Reputation: 507
I am using the rolling command in a foreach loop:
use "MyFile.dta"
tsset time, monthly
foreach i of varlist var1 var2 {
rolling _b, window(12) saving(beta_`i'): reg `i' DependentVariable
}
Now, this code saves a different file for each rolling regression. What I would really like is to save each vector of betas obtained from the rolling estimation as a variable. The final result I would like to obtain is a dataset with a time variable and a "beta_var#" variable for each rolling:
time | beta_var1 | beta_var2
_________|___________|__________
1990m1 | ## | ##
1990m2 | ## | ##
... | ## | ##
200m12 | ## | ##
1990m1 | ## | ##
(PS: secondary question: is there a shortcut to indicate a varlist = to all the variables in the dataset?)
Upvotes: 0
Views: 2559
Reputation: 11102
I misread your post and my initial answer does not give what you ask for. Here's one way. Not elegant nor very efficient but it works (just change directory names):
clear all
set more off
* Do not mix with previous trials
capture erase "/home/roberto/results.dta"
* Load data
sysuse sp500
tsset date
* Set fixed independent variable
local var open
foreach depvar of varlist high low close volume {
rolling _b, window(30) saving(temp, replace): regress `depvar' `var'
use "/home/roberto/temp.dta", clear
rename (_b_`var' _b_cons) (b_`depvar' b_cons_`depvar')
capture noisily merge 1:1 start end using "/home/roberto/results.dta", assert(match)
capture noisily drop _merge
save "/home/roberto/results.dta", replace
sysuse sp500, clear
tsset date
}
* Delete auxiliary file
capture erase "/home/roberto/temp.dta"
* Check results
use "/home/roberto/results.dta"
browse
Maybe other solutions can be proposed using postfile
or concatenating vectors of results and converting to a dataset using svmat
. I'm not sure.
Original answer
Use the saving()
option with replace
and provide only one file name (drop the macro suffix) :
clear all
set more off
webuse lutkepohl2
tsset qtr
rolling _b, window(30) saving(results, every(5) replace): regress dln_inv dln_inc dln_consump
Upvotes: 2