Announcement

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

  • scalar in loop (using estadd and esttab export to latex)

    Dear all,
    I am quite new to stata and have what I think must be a silly problem. I want to export OLS regression results to latex and add a row with the means of the DV. Everything works fine with the exception that only the second mean shows up in the latex table, meaning that the first mean gets somehow overwritten when the second mean is generated. I am attaching the pdf with the table.

    Here is what I am doing:

    eststo clear

    foreach var of varlist inq_problem deserve {
    eststo: quietly xi: regress `var' treated i.fieldworker i.wave , robust

    summarize `var', meanonly
    scalar m`var' = r(mean)

    estadd scalar m`var'
    estadd local covars "Yes"

    esttab using temp.tex, replace keep(treated ) ///
    title("Title Here") nolabel ///
    cells(b(star fmt(3)) se(par fmt(3))) nonumbers label compress ///
    stats(m`var' covars N, fmt(2 "" 0) label("Control Group Mean" "Covariates" "Obs")) ///
    addnote("Robust standard errors" ///
    "Covariates: wave and fieldworker fixed effects" ///
    "p-values: * 0.10 ** 0.05 *** 0.01") ///
    star(* 0.10 ** 0.05 *** .01)
    }

    I patched this code together from various posts, so I don't understand exactly what is happening when in this code.
    I would really appreciate your help!
    Thanks a lot,
    Eva


    Attached Files

  • #2
    The problem is that you're giving the mean scalar a new name each time. That is why only the second one appears on iteration 2 of the loop. You're not telling Stata to include the first one as well. However, with different names, they would be on different rows, which I am guessing you don't want. Also, you should also put the esttab outside the loop (most likely).

    I am guessing you want something like this:

    Code:
    sysuse auto, clear
    eststo clear
    
    foreach var of varlist price mpg {
        eststo: reg `var' i.foreign weight
        
        summarize `var', meanonly
        scalar Mean = r(mean)
    
        estadd scalar Mean
        estadd local covars "Yes"
    }
    
    esttab, stats(Mean covars)

    Comment


    • #3
      Originally posted by Dimitriy V. Masterov View Post
      The problem is that you're giving the mean scalar a new name each time. That is why only the second one appears on iteration 2 of the loop. You're not telling Stata to include the first one as well. However, with different names, they would be on different rows, which I am guessing you don't want. Also, you should also put the esttab outside the loop (most likely).

      I am guessing you want something like this:

      Code:
      sysuse auto, clear
      eststo clear
      
      foreach var of varlist price mpg {
      eststo: reg `var' i.foreign weight
      
      summarize `var', meanonly
      scalar Mean = r(mean)
      
      estadd scalar Mean
      estadd local covars "Yes"
      }
      
      esttab, stats(Mean covars)

      Thanks a lot, this works perfectly including the export to latex.
      Eva

      Comment

      Working...
      X