Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • -esttab- -foreach- looping: How do I -replace- on the first loop, but -append- on each subsequent loop?

    Hi Statalist,

    I'd like to write a -foreach- loop that runs a number of regressions (let's say I want to run `z' number of regressions), then creates a new *.rtf file (replacing any previous *.rtf output) which has all `z' regression results in the same file. I'm thinking I need to -replace- on the first iteration, and then -append- on all subsequent iterations.

    Here's the code format I'm working with, can anybody help where it says XXXreplace/appendXXX?

    Code:
    counter=1
    foreach indep_var in ///
    "y1" ///
    "y2" ///
    "y3" ///
    ... ///
    "yz" ///
        {
            reg `indep_var' x
            estimates store m`counter'
            local counter=`counter'+1
        }
    esttab m* using "output.rtf", XXXreplace/appendXXX
    Would you use the code below? Do I need to use -esttab- twice? The reason I ask is that my -esttab- is a sprawling multi-line command with many options, and I'd rather not have all of those replicated in the code twice. And for the second -esttab- command, is there an easy way to get Stata to -esttab- all models (m1 m2 m3 m4 ... mz) except m1, when I don't always know what the value of `z' is?

    Thank you.

    Code:
    counter=1
    foreach indep_var in ///
    "y1" ///
    "y2" ///
    "y3" ///
    ... ///
    "yz" ///
        {
            reg `indep_var' x
            estimates store m`counter'
            local counter=`counter'+1
        }
    esttab m1 using "output.rtf", replace
    esttab m2-mz using "output.rtf", append
    Last edited by Amy Ng; 14 Sep 2015, 21:30.

  • #2
    First remove the existing output.rtf. Then use esttab *, append.

    On another note, instead of using a counter, replace estimates store with eststo and it's prefix option.

    Comment


    • #3
      Thank you, Nils!

      So perhaps

      Code:
      capture erase "output.rtf"
      at the beginning?

      Why didn't I think of that?! Thank you.

      Comment


      • #4
        That's correct. I should have spelled that out.

        Simple solutions are not always obvious solutions.

        Comment

        Working...
        X