Using Stata esttab, add a dollar sign to the cell format, export to latex

My advisor wants me to add dollar signs to the summary statistics table. I create this table and export it to latex using the Stata esttab command.

I need 1) Add dollar signs to some result cells (not all) and 2) Make sure latex can handle dollar signs.

I think 2 can be accomplished using the substitute option, but I cannot figure out how to do this. Here are some minimal codes 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 AB 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 gives:

  &\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 would like to get it so that the output has \ $ in front of 6,072 and 6,385

I see some discussion regarding the statist about workarounds for graphs, but nothing for esttab. Someone also mentions the creation of β€œcustom formats,” but I cannot find documentation about this anywhere.

+7
latex stata
source share
1 answer

I had a similar problem: I wanted to color the cells differently depending on the significance level. In the end, the simplest automated solution I could come up with was changing the esttab code ... It's easier to do than it seems.

Find the following code in estout.ado :

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

Right after that you can insert, for example

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

This will create:

  &\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 program drop estout before export to reload .ado)

So, all the numerical values ​​in the main table are enclosed in $ signs. If you need only certain values, you can fulfill the simple regex condition. For example, if you cannot capture only those values ​​where there is a comma (for some reason), you can do:

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

And you can also add your own variant (only at the beginning of estout.ado) so that the changed behavior does not start all the time.

0
source share

All Articles