Richard Herron
Richard Herron

Reputation: 10102

Fixed-width format with commas for summary statistics with estpost summarize in Stata

The estpost and esttab commands are from the estout package on SSC.

I use estpost summarize to make summary statistics tables. I am told that readers prefer decimals as fixed width/precision. Say, a leading zero and three decimal places. Option fmt(%9.3f) gives a leading zero and fixed width, but reports large numbers without commas. Option fmt(%12.3gc) gives commas, but does not report fixed width decimals.

Is there a way to be more granular with fmt() in esttab? Say, use %12.3gc for numbers above some threshold and %9.3f otherwise? This may create problems that I have not anticipated.

Here is an example of how I use estpost summarize and the shortcomings of each approach.

sysuse auto, clear
expand 1000

eststo clear
estpost summarize price rep78, detail
eststo

/* gives mean/median/SD with fixed number of decimals */
/* but must manually add commas to large numbers (price) */
local rightwidth_nocommas ///
    label noobs nonumbers nomtitles ///
    cell("count(fmt(%9.3gc) label(Obs.)) mean(fmt(%9.3f) label(Mean)) p50(fmt(%9.3f) label(Median)) sd(fmt(%9.3f) label(Std. Dev.))")
esttab, `rightwidth_nocommas'

. esttab, `rightwidth_nocommas'

------------------------------------------------------------------------
                             Obs.         Mean       Median    Std. Dev.
------------------------------------------------------------------------
Price                      74,000     6165.257     5006.500     2929.519
Repair Record 1978         69,000        3.406        3.000        0.983
------------------------------------------------------------------------



/* gives commas to large numbers (price) */
/* but must pad mean/median/SD to get a fixed number of decimals */
local commas_wrongwidth ///
    label noobs nonumbers nomtitles ///
    cell("count(fmt(%9.3gc) label(Obs.)) mean(fmt(%9.3gc) label(Mean)) p50(fmt(%9.3gc) label(Median)) sd(fmt(%9.3gc) label(Std. Dev.))")
esttab, `commas_wrongwidth'

. esttab, `commas_wrongwidth'

------------------------------------------------------------------------
                             Obs.         Mean       Median    Std. Dev.
------------------------------------------------------------------------
Price                      74,000        6,165        5,007        2,930
Repair Record 1978         69,000         3.41            3         .983
------------------------------------------------------------------------

Upvotes: 1

Views: 1745

Answers (1)

user4690969
user4690969

Reputation:

Here's a demo using fc for everything, no gc needed for counts, and using a set of surrounding parentheses instead of quotation marks around the argument to cells to force the four items onto a single row.

. esttab, ///
>     label noobs nonumbers nomtitles               ///
>     cell((                                        ///
>            count(fmt(%9.0fc)  label(Obs.))        ///
>             mean(fmt(%10.3fc) label(Mean))        ///
>              p50(fmt(%8.1fc)  label(Median))      ///
>               sd(fmt(%10.3fc) label(Std. Dev.))   ///
>          ))

------------------------------------------------------------------------
                             Obs.         Mean       Median    Std. Dev.
------------------------------------------------------------------------
Price                      74,000    6,165.257      5,006.5    2,929.519
Repair Record 1978         69,000        3.406          3.0        0.983
------------------------------------------------------------------------

Upvotes: 2

Related Questions