Search code examples
stata

Displaying counts for subgroups in summary table


I am trying to create a summary statistics table by subgroup, using estout in Stata. My efforts have been drawing off of the official estout online documentation.

I want the counts of each subgroup to appear in the final table. I know that noobs in the last line removes the observation count. However, if I include noobs only the total observation count is included and not the count for the individual subgroups.

For example:

. sysuse auto
(1978 Automobile Data)

. estpost tabstat price mpg rep78, by(foreign) statistics(mean sd) ///
columns(statistics) listwise

Summary statistics: mean sd
     for variables: price mpg rep78
  by categories of: foreign

     foreign |   e(mean)      e(sd) 
-------------+----------------------
Domestic     |                      
       price |   6179.25   3188.969 
         mpg |  19.54167   4.753312 
       rep78 |  3.020833    .837666 
-------------+----------------------
Foreign      |                      
       price |  6070.143   2220.984 
         mpg |  25.28571   6.309856 
       rep78 |  4.285714   .7171372 
-------------+----------------------
Total        |                      
       price |  6146.043    2912.44 
         mpg |  21.28986   5.866408 
       rep78 |  3.405797   .9899323 

. esttab, main(mean) aux(sd) nostar unstack noobs nonote nomtitle nonumber

---------------------------------------------------
                 Domestic      Foreign        Total
---------------------------------------------------
price              6179.2       6070.1       6146.0
                 (3189.0)     (2221.0)     (2912.4)

mpg                 19.54        25.29        21.29
                  (4.753)      (6.310)      (5.866)

rep78               3.021        4.286        3.406
                  (0.838)      (0.717)      (0.990)
---------------------------------------------------

Solution

  • The following does what you are looking for:

    clear
    sysuse auto
    
    by foreign: eststo: estpost summarize price mpg rep78, listwise
    eststo Total: estpost summarize price mpg rep78, listwise
    
    esttab, main(mean) aux(sd) nostar nonote label nonumber nodepvar
    
    -----------------------------------------------------------
                             Domestic      Foreign        Total
    -----------------------------------------------------------
    Price                      6179.2       6070.1       6146.0
                             (3189.0)     (2221.0)     (2912.4)
    
    Mileage (mpg)               19.54        25.29        21.29
                              (4.753)      (6.310)      (5.866)
    
    Repair Record 1978          3.021        4.286        3.406
                              (0.838)      (0.717)      (0.990)
    -----------------------------------------------------------
    Observations                   48           21           69
    -----------------------------------------------------------