0
votes

I am trying to use esttab to create a LaTeX table with summary statistics using the summarize command. I can use code like the following to do this if I summarize multiple variables at once:

sysuse auto, clear
global vars price mpg headroom
eststo clear
eststo: estpost sum $vars, listwise
esttab est*, cells("count mean(fmt(2)) sd") nomtitles nonumber noobs

However, I am not sure how to summarize one line, store it, summarize another, store it, etc., and then combine all of them in the same table without creating unnecessary columns. I may want to summarize each variable individually if I want to make individualized restrictions by variable on which observations to summarize.

Here is code that doesn't get me what I want. Specifically, it does not put the summary statistics for each variable under the same column, but instead creates new columns, each set of which correspond to a different variable.

eststo clear
gen count = 1
foreach i in $vars {
    eststo: estpost sum `i' if `i'>count
    replace count = count+1
}

esttab est*, cells("count mean(fmt(2)) sd") nomtitles nonumber noobs

What should I change to get me my desired result?

2

2 Answers

1
votes

You can use the fragment and append options to make tables line-by-line. You might want to do one variable without the fragment option to generate the same table header/footer, then cut-and-paste the remaining lines into this table.

1
votes

Your problem is analogous to stacking models; instead of "models" you have summaries. The user-written command estout doesn't stack models, so one way out is to create your own matrix and feed it to estout (or esttab):

clear
set more off

*----- example data -----

sysuse auto

*----- two-variable example -----

eststo clear

// process price
estpost summarize price
matrix mymat = e(mean), e(count)

// process mpg
estpost summarize mpg if mpg > 15
matrix mymat = mymat \ e(mean), e(count)

// finish formatting matrix
matrix colnames mymat = mean count
matrix rownames mymat = price mpg
matrix list mymat

// tabulate
esttab matrix(mymat), nomtitles

With additional work, you can automatize the steps.

See http://repec.org/bocode/e/estout/advanced.html#advanced901 for another example.