Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Stata's esttab, add dollar sign to cell format, export to Latex

Tags:

latex

stata

My advisor wants me to add dollar signs to my table of summary statistics. I generate this table and export it to Latex using Stata's esttab command.

I need to 1) Add dollar signs to some of the results cells (not all) and 2) Make sure that Latex can handle the dollar signs.

I think that 2 might be accomplished using the substitute option, but I can't figure out how to do 1. Here is some minimal code that I am trying to use to solve this problem.

sysuse auto, clear

estpost summarize price mpg weight length if foreign==0
est store A
estpost summarize price mpg weight length if foreign==1
est store B

esttab A B  using $root/Outputs/test.tex, replace ///  //a file path on my machine
cells("mean (fmt(%9.0fc %9.2fc %9.0fc))" "sd(par fmt(%9.0fc %9.2fc %9.0fc))") ///
mtitle("Domestic" "Foreign") ///
mgroups("Type", pattern(1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat( \cmidrule(lr){@span})) ///
nonumber booktabs f label  collabels(none)

eststo clear

This produces:

                    &\multicolumn{2}{c}{Type} \\\cmidrule(lr){2-3}
                    &\multicolumn{1}{c}{Domestic}&\multicolumn{1}{c}{Foreign}\\
\midrule
Price               &       6,072&       6,385\\
                    &     (3,097)&     (2,622)\\
Mileage (mpg)       &       19.83&       24.77\\
                    &      (4.74)&      (6.61)\\
Weight (lbs.)       &       3,317&       2,316\\
                    &       (695)&       (433)\\
Length (in.)        &         196&         169\\
                    &        (20)&        (14)\\
\midrule
Observations        &          52&          22\\

I'd like to get it so the output would have \$ in front of the 6,072 and the 6,385

I see some discussion on the Statalist regarding workarounds for graphs, but nothing for esttab. Someone also mentions creating "custom formats" but I can't seem to find documentation on that anywhere.

like image 368
Danielle Avatar asked Apr 09 '17 19:04

Danielle


1 Answers

I had a similar problem once: I wanted to color cells differently depending on the significance level. In the end, the easiest automated solution I could come up with was modifying the esttab code... That is easier done than it sounds, in fact.

Look for the following code in estout.ado:

if `:length local thevalue'<245 {
    local thevalue: di `fmt_m' `"`macval(thevalue)'"'
}

Just after that you can insert, e.g.

local thevalue `"\$`macval(thevalue)'\$"'

That would produce:

                    &\multicolumn{2}{c}{Type} \\\cmidrule(lr){2-3}
                    &\multicolumn{1}{c}{Domestic}&\multicolumn{1}{c}{Foreign}\\
\midrule
Price               &$       6,072$&$       6,385$\\
                    &$     (3,097)$&$     (2,622)$\\
Mileage (mpg)       &$       19.83$&$       24.77$\\
                    &$      (4.74)$&$      (6.61)$\\
Weight (lbs.)       &$       3,317$&$       2,316$\\
                    &$       (695)$&$       (433)$\\
Length (in.)        &$         196$&$         169$\\
                    &$        (20)$&$        (14)$\\
\midrule
Observations        &          52&          22\\

(Don't forget to program drop estout before exporting, so that the .ado reloads)

So, all the numeric values in the main table are encapsulated in $ signs. If you want specific values only, you can do a simple regex condition. E.g., if you care capturing only those values where there is a comma (for whatever reason), you can do:

if strpos("`macval(thevalue)'", ",") {
    local thevalue `"\$`macval(thevalue)'\$"'
}

And you can also add you own option (just in the beginning of estout.ado), so that the modified behaviour is not triggered all the time.

like image 56
Andrei Avatar answered Nov 17 '22 16:11

Andrei