Announcement

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

  • Using Outreg2 with multiple regressions nested in a foreach loop

    I am running a couple of regressions that I have nested in a `foreach' loop but I want the regressions to appear in one table. The problem I have is the use of the `replace' option as I want to replace the stored table in my working directory each time I run my dofile. If I use the replace option, the end product will be that each succeeding regression column replaced the preceeding one. See my line of code;
    Code:
    global vars3 x1 x2 x3 x4 x5
    
    foreach var of global vars3 {
    **********************************
    display `var'
    ***********************************
    reg depvar `var' 
    outreg2 using reg_file, excel ct("reg"`var')
    }
    How do I ensure that each time the dofile runs, the reg_file in my working directory is replace?

  • #2
    You can detach the first regression from the loop.

    Code:
    global vars3 x1 x2 x3 x4 x5
    reg dv x1
    outreg2 using reg_file, excel ct("reg"x1) replace
    foreach var of global vars3 {
    if !inlist(`var', x1){  
    **********************************
    display `var'
    ***********************************
    reg dv `var'
    outreg2 using reg_file, excel ct("reg"`var')
    }
    }

    Comment


    • #3
      A similar approach to Andrew's, to use the replace option only on the first time through the loop. This version can be helpful with more complicated loops than the one in your example.
      Code:
      global vars3 x1 x2 x3 x4 x5
      local repl replace
      
      foreach var of global vars3 {
      **********************************
      display `var'
      ***********************************
      reg depvar `var' 
      outreg2 using reg_file, excel ct("reg"`var') `repl'
      local repl
      }

      Comment


      • #4
        Thank you, Andrew and William, for this. Most appreciated.

        Comment

        Working...
        X