Announcement

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

  • save output from foreach loop

    Dear Listers,

    As shown below, I am using foreach loop twice. I try to write out the output . I know my estout `y' is incorrect, in the sense that it is not saving all the regression outputs, only the last dependent varaible referred in the y.

    Can someone point out my error and make edits to my code? I get confused using double foreach loop.

    Thanks,

    Code:
    foreach x in ar  net  {
    
    
        foreach y in fd1_`x' fd2_`x' fd3_`x' fd3y_`x'{
        xtreg `y'   $ctrl , fe cluster(firmid)
        est store `y'  , title(`y' )
        }
        estout `y' using "ResultsTables\out_`x'.rtf" , replace style(fixed) 
    }
    rochelle

  • #2
    I don't actually use -estout- myself very often. But there are two problems with your code:

    1. The -estout- command belongs inside the inner loop. Otherwise, you do four regressions and store four sets of estimates, but you only call -estout- once, so it uses only the most recent results for the given value of x.

    2. Once you move it inside the inner loop, you will need to change the -estout- command. As written, it will overwrite the out_`x'.rtf file four times, so that only the final results will survive intact. You either need to change -replace- to -append- (if you want all four `y' results put together in the same file) or you need to have a new destination file for each. So something like this to end up with separate files for each set of results:

    Code:
    foreach x in ar  net  {
        foreach y in fd1_`x' fd2_`x' fd3_`x' fd3y_`x'{
            xtreg `y'   $ctrl , fe cluster(firmid)
            est store `y'  , title(`y' )
            estout `y' using "ResultsTables\out_`y'_`x'.rtf" , replace style(fixed)
       }
    }

    Comment


    • #3
      Thank you Clyde very much !

      Comment


      • #4
        The other way to do this is to use different names for each est store (e.g.,
        est store `y'`x' And then do the estout outside the loops.

        Comment


        • #5
          Thanks Phil !

          Comment

          Working...
          X