Help with a basic loop in Stata (ver 11)

meg

New Member
#1
Hi,

Im new to Stata and am having trouble running a loop. I want to reg one variable on another, subject to fund no.
Fund no is a variable name with values from 1 to 357.
So basically I want to repeat the same regression (reg equity return) 357 times without having to do
reg equity return if fundno ==1
reg equity return if fundno ==2
etc

So I tried
foreach var of varlist fundno {reg equity return}

I know its something simple but I cant figure it out!
Also, is there anyway of storing all the output? The output window only displays a certain amount of results and 357 will be too much and Ill miss out of some of the results.

Thanks a lot!
 
#2
How about:

forvalues i = 1/357 {

reg equity return if fundno ==`i'

}


With regard to the output, you could probably write some code but this would increase the loop difficult. How about setting up a log file to store the results in and leaving it to run?
 

meg

New Member
#3
Thanks so much!! Very helpful.

One more question if you can help. How can I add in the fund no into each regression. In other words, Id like to print the fund no before or after each regression. There isnt output for every fund so I need to know which result corresponds with each fund.

Thanks!
 

bukharin

RoboStataRaptor
#4
Code:
forvalues i = 1/357 {
display "fund number = `i'"
reg equity return if fundno ==`i'
}
But that's going to be a lot of output. You may be better off using something like:
Code:
statsby, by(fundno) clear: reg equity return
This will replace your dataset with a new dataset containing the results of the separate regressions. If you don't want to replace the dataset, use -saving(filename)- instead of -clear- (this will save the "results" dataset into filename so that you can look at them later)