Reputation: 2527
Say I have this data:
clear
set more off
input ///
float (b a_first a_second a_third control)
4 3 2 5 7
6 3 4 2 4
7 6 5 2 5
1 4 2 3 6
7 6 1 4 1
8 8 7 4 4
end
I want to create a table using outreg2:
foreach i in first second third {
reg b a_`i'
outreg2 using filename, replace
reg b a_`i' control
outreg2 using filename, append
}
(Note that `filename' is your file name of choice.) This doesn't quite do what I want. For every iteration, it creates a table with just two columns. The next time through, it replaces what was originally there with the two new regressions.
What I need it to do is only replace the very first time through and after that switch to append:
reg b a_first
outreg2 using filename, replace
reg b a_first control
outreg2 using filename, append
reg b a_second
outreg2 using filename, append
reg b a_second control
outreg2 using filename, append
reg b a_third
outreg2 using filename, append
reg b a_third control
outreg2 using filename, append
The best I can come up with is to create a local that takes value replace
if i==first
and use this local in the first outreg2
statement. Is there a more straightforward way?
Upvotes: 0
Views: 1622
Reputation: 107567
Consider storing the regression estimates, then use outreg2
outside the loop, concisely with a wildcard [*]
. See Example 3 of the outreg2
doc:
foreach i in first second third {
reg b a_`i'
est store `i'
reg b a_`i' control
est store `i'control
}
outreg2 [*] using filename, replace
Upvotes: 2
Reputation: 37208
For completeness, note a classical alternative, which is to take the first iteration outside the loop:
reg b a_first
outreg2 using filename, replace
reg b a_first control
outreg2 using filename, append
foreach v in second third {
reg b a_`v'
outreg2 using filename, append
reg b a_`v' control
outreg2 using filename, append
}
Upvotes: 1
Reputation:
This is pretty much what you thought of, but not too ugly, since it makes use of only needing to be run the first time to avoid needing an if. I wouldn't call this an answer, but I haven't figure out how to post clean code in a comment.
local rep replace
foreach i in first second third {
reg b a_`i'
outreg2 using filename, `rep'
local rep append
reg b a_`i' control
outreg2 using filename, append
}
Upvotes: 0