Danielle Danielle - 4 months ago 20
LaTeX Question

When using esttab, why does keep() suboption not work with all variables?

I am using the Stata command esttab to convert some tabulated output to Latex form. I want to include total number of observations in each column in the last row of the table. I have run across an issue using the

keep
suboption for
blist
. Sometimes keep performs as expected. Sometimes it does nothing.

I found the following on statalist, which suggests to me it may have something to do with the difference between coeflist and varlist, but I don't think that is the issue here. http://www.statalist.org/forums/forum/general-stata-discussion/general/131816-using-factor-variables-in-estimates-table-keep

Here is a reproducible example.

clear all
sysuse auto

gen big=(weight>3000)
label define Big 0 "Not in Big Set" 1 "In Big Set"
label values big Big
label variable big "Is Item Found in Big Set?"
compress big //want it to be byte to match my real data

estpost tabulate big foreign

esttab using myfilename.tex, replace f ///
cell(colpct(fmt(2)) b(fmt(g) par keep(Total))) ///
collabels(none) unstack noobs nonumber nomtitle ///
eqlabels(, lhs("Size Category")) ///
varlabels(, blist(Total "\hline "))

estpost tabulate rep78 foreign

esttab using myfilename2.tex, replace f ///
cell(colpct(fmt(2)) b(fmt(g) par keep(Total))) ///
collabels(none) unstack noobs nonumber nomtitle ///
eqlabels(, lhs("Repair Rec.")) ///
varlabels(, blist(Total "\hline "))


The latex output looks like this in the first case:

Size Category& Domestic& Foreign& Total\\
\hline
Not in Big Set& 28.85& 90.91& 47.30\\
& (15)& (20)& (35)\\
In Big Set & 71.15& 9.09& 52.70\\
& (37)& (2)& (39)\\
\hline Total & 100.00& 100.00& 100.00\\
& (52)& (22)& (74)\\


It looks like this (correct) in the second case:

Repair Rec. & Domestic& Foreign& Total\\
\hline
1 & 4.17& 0.00& 2.90\\
2 & 16.67& 0.00& 11.59\\
3 & 56.25& 14.29& 43.48\\
4 & 18.75& 42.86& 26.09\\
5 & 4.17& 42.86& 15.94\\
\hline Total & 100.00& 100.00& 100.00\\
& (48)& (21)& (69)\\


I also thought it may have something to do with my storage type, but adding
recast int big
before the first estpost didn't fix it.

Why is
keep
only working in one if these cases? How can it be fixed?

Or is there another workaround to achieve my goal of number of observations in each column appearing in the last row (labeling them "Number of observations" would be a bonus)?

Answer

Well, thanks to William's good hint, I figured out this solution. I am still in the dark about WHY the original code doesn't work, but here is a way to get the desired output from keep and a workaround for the loss of the variable labels using varlabels...

clear all
sysuse auto

gen big=(weight>3000)
label variable big "Is Item Found in Big Set?"
compress big  //want it to be byte to match my real data

estpost tabulate big foreign

esttab using myfilename.tex, replace f ///
cell(colpct(fmt(2)) b(fmt(g) par keep(Total))) ///
collabels(none) unstack noobs nonumber nomtitle    ///
eqlabels(, lhs("Size Category"))                     ///
varlabels(0 "Not in Big Set" 1 "In Big Set", blist(Total "\hline "))

This produces:

Size Category&    Domestic&     Foreign&       Total\\
\hline
Not in Big Set&       28.85&       90.91&       47.30\\
In Big Set  &       71.15&        9.09&       52.70\\
\hline Total       &      100.00&      100.00&      100.00\\
            &        (52)&        (22)&        (74)\\
Comments