Announcement

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

  • Export Regressions to Different Sheets in Same Excel Doc.

    I have been trying to figure out a way to automate my regression output such that each set of regression goes to a separate sheet in the same Excel document. In every cell of the final output there is, for example, ="0.067" and the stripquotes(yes) option gets rid of the quotes, but I am still left with the equal sign in every cell. Any ideas on how to get rid of it? I've tried the "plain" option with esttab, and while this gets rids of the = and the "", it gets rid of a lot of the other formatting.

    // Regression set (1)
    foreach v of varlist outcome_1 outcome_2 {

    eststo: reg `v' x1 x2
    eststo: reg `v' x1 x2 x3
    eststo: reg `v' x1 x2 x3 x4

    }

    esttab using "$output\regressions_set_1.csv", ///
    n se nobaselevels noconstant r2 aic bic replace ///
    order(x1 x2 x3 x4) ///
    keep (x1 x2 x3 x4) ///
    label
    estimates clear


    // Regression set (1)
    foreach v of varlist outcome_1b outcome_2b {

    eststo: reg `v' z1 z2
    eststo: reg `v' z1 z2 z3
    eststo: reg `v' z1 z2 z3 z4

    }

    esttab using "$output\regressions_set_2.csv", ///
    n se nobaselevels noconstant r2 aic bic replace ///
    order(z1 z2 z3 z4) ///
    keep (z1 z2 z3 z4) ///
    label
    estimates clear

    // Read in each .csv and export excel so that I can automate getting
    // all output on separate sheets in same document
    foreach v in regression_set_1 regression_set_2 {
    preserve
    import delimited P:\Filepath\Regressions_`v'_final.csv, stripquotes(yes)
    export excel using "P:\Filepath\Final_Results.xlsx", missing("")sheetreplace sheet("`v'")
    restore
    }

  • #2
    This example seems to do what you request.
    Code:
    cls
    sysuse auto.dta, clear
    estimates clear
    quietly eststo: regress weight length foreign
    esttab using gnxl.csv, replace
    
    import delimited gnxl.csv, clear
    list, clean noobs
    foreach var of varlist * {
        replace `var' = substr(`var',3,length(`var')-3)
    }
    list, clean noobs
    Code:
    . list, clean noobs
    
                                         v1              v2  
                                        =""          ="(1)"  
                                        =""       ="weight"  
                                  ="length"     ="31.44***"  
                                        =""      ="(19.64)"  
                                 ="foreign"       ="-133.7"  
                                        =""      ="(-1.73)"  
                                   ="_cons"   ="-2850.2***"  
                                        =""      ="(-9.02)"  
                                       ="N"           ="74"  
             ="t statistics in parentheses"                  
        ="* p<0.05, ** p<0.01, *** p<0.001"                  
    
    . foreach var of varlist * {
      2.     replace `var' = substr(`var',3,length(`var')-3)
      3. }
    (11 real changes made)
    (9 real changes made)
    
    . list, clean noobs
    
                                      v1           v2  
                                                  (1)  
                                               weight  
                                  length     31.44***  
                                              (19.64)  
                                 foreign       -133.7  
                                              (-1.73)  
                                   _cons   -2850.2***  
                                              (-9.02)  
                                       N           74  
             t statistics in parentheses               
        * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      William Lisowski, thank you! This works perfectly.

      Comment

      Working...
      X