benjann / estout

Stata module to make regression tables
http://repec.sowi.unibe.ch/stata/estout/index.html
MIT License
71 stars 18 forks source link

esttab does not take care of labels with special characters #20

Closed nimafazeli closed 5 years ago

nimafazeli commented 5 years ago

Thanks for the great work and also putting the source on github. Hopefully this becomes more common for people who write packages for Stata. I actually have two related issues here.

Steps to reproduce the error I have defined labels for values of a variable. I had

label define      myvar_labels          1 "Moody's"     2 "S&P's"
label values      myvar                    myvar_labels

when doing

estpost tab myvar myvar2
esttab using "table.tex", replace label cells("b pct") 

I face the following two issues:

Issue 1: Apostrophe ' in labels of values

It seems that when exporting table, esttab cannot handle an apostrophe sign ' in labels for values of variables.

I get the following error

invalid syntax
in not found
r(111)

Removing the apostrophes from the labels solved the issue but, well, it is a restriction and makes the output ugly.

Issue 2: Ampersand & in labels of values

When exporting to tex file, esttab does not escape the special characters of latex , in this case the & gets passed directly to the latex table and causes errors when compiling the tex document since it gets interpreted as alignment character. The correct behavior should be to escape it and exporting it as \&. A quick fix is to use \& instead of & in the label.

benjann commented 5 years ago

Issue 1: Apostrophe ' in labels of values

Thanks for reporting this. The problem occurs when the coefficient vector e(b) has names that contain an apostrophe. With official Stata commands this should never happen, but estpost tabulate (or estpost svy: tabulate) is an example where such funny names can occur. This is because estpost tabulate uses the value labels as coefficient names (unless the labels contain characters that are not allowed in coefficient names).

I identified the source of the problem in estout.ado and will try to fix it. In the meantime I suggest you apply the elabels option with estpost tabulate. This prevents estpost tabulate from using the labels as coefficient names and instead returns the labels in macro e(labels). This macro can then be passed on to esttab or estout to label the coefficients. Example:

. sysuse auto, clear
(1978 Automobile Data)

. lab def origin 1 "Don't know", modify

. estpost tab foreign, elabels

     foreign |      e(b)     e(pct)  e(cumpct) 
-------------+---------------------------------
           0 |        52   70.27027   70.27027 
           1 |        22   29.72973        100 
-------------+---------------------------------
       Total |        74        100            

row labels saved in macro e(labels)

. esttab, coeflabel(`e(labels)')

----------------------------
                      (1)   
                  foreign   
----------------------------
Domestic               52   

Don't know             22   

Total                  74   

----------------------------
N                      74   
----------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

Issue 2: Ampersand & in labels of values

In general, esttab and estout do not contain code to deal with characters that may be problematic in one or the other output format. Of course such code could be added, but at some point I decided to leave it to the user to handle such issues. You approach, that is, to type \& in the label instead of just & is the way to go.

benjann commented 5 years ago

I now fixed the problem with the apostrophes. The fix will be in the next SSC update.