7

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.

Danielle
  • 329
  • 2
  • 13
  • I want to note that I can change the label on price to at least get the dollar sign in the variable label: label variable price "Price (\\$)" – Danielle Apr 09 '17 at 20:14
  • Does anyone know if it is acceptable to cross-post to Statalist? – Danielle Apr 12 '17 at 14:29
  • Acceptable to whom? Statalist has its own explicit policy that cross-posting should be flagged. Cross-posting elsewhere (meaning, not on Stack Exchange) and telling SO about it is also considered courteous. https://meta.stackoverflow.com/questions/341906/posting-a-question-that-is-coming-from-another-domain-specific-forum/ – Nick Cox Apr 18 '17 at 12:13
  • Cross-posted to Statlist: http://www.statalist.org/forums/forum/general-stata-discussion/general/1384251-using-stata-s-esttab-add-dollar-sign-to-cell-format-export-to-latex – Danielle Apr 18 '17 at 15:46

1 Answers1

1

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.

Andrei
  • 2,585
  • 1
  • 14
  • 17
  • Oblique comment: Naturally this is what programmers do, alter programs to do something different which they want done. The warning (for those who need it) is to think about how your own changes need to be edited in again if the original program is updated. – Nick Cox May 15 '17 at 12:55
  • @Nick Cox Use diff + patch, I imagine. If one can make a good case for the addition, maybe the package author would consider adding the option... – Andrei May 15 '17 at 13:07
  • Indeed. You'd need to communicate with him (in this case). – Nick Cox May 15 '17 at 13:11