# Stata to Latex – Part 1: Exporting customized tables

Anyone writing an empirical paper eventually has to deal with the issue of exporting Stata outputs to a Latex file. While standard statistics and regressions are easy to handle, as soon as customized outputs are required, it quickly becomes messy, requiring significant head scratching. Making tables by hand is not really advisable, since one is likely to tweak the outputs several times. In this entry, we look at a simple example of stacking standard errors from different regression outputs which have the same beta coefficient but different significance levels. The main aim is to create a compact table where multiple regression results can be neatly displayed using the custom “estout” package in Stata.

NOTE: The “estout” package is required in Stata to generate outputs in Latex. This can be easily installed using the “net install estout” command.

Here we start off with a simple example in Stata:

sysuse auto, clear     // sysuse will always open the sample file even if the directory path is different.

eststo: regress price weight mpg
eststo: regress price weight mpg, vce(robust)

esttab, keep(weight)    // let’s just look at weight only

——————————————–
(1)           (2)
price         price
——————————————–
weight             1.747**        1.747*
(2.72)        (2.25)
——————————————–
N                    74            74
——————————————–
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

As one can see, the coefficients are the same but standard errors and significance levels are different. A neat and concise way would be to stack the standard errors under one coefficient and highlight the significance levels on the standard errors. We would like to get something of the following form:

—————————
(1)
price
—————————
weight         1.747
(2.72)**
[2.25]*

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

To starting building this table, we make use of customization outputs using the esttab command in the estout package. We start off by creating the first two rows of the above table:

est clear
eststo: regress price weight mpg
esttab, cells(b(fmt(a3)) se(fmt(a3) star par)) keep(weight)

which gives:
—————————-
(1)
price
b/se
—————————-
weight           1.747
(0.641)**
—————————-
N                 74
—————————-

The above output can be broken down as follows:

cells: cells command can all variables stored in the eclass version of the regression. Type ereturn list to see all the stored variables after regressions.
b(fmt(a3): we first call in the variable matrix e(b) and fmt(a3) says that we show only three decimal places.
se(fmt(a3) star par): this command calls in the e(se) matrix which is also formatted up to three decimal places. star adds the significance levels to this variable and par adds the parenthesis.

Next we export this file in rtf (we will come back to latex later which is slightly trickier to format):
esttab using stacking.rtf, replace cells(b(fmt(a3)) se(fmt(a3) star par)) keep(weight)

The next step is to add another row to this column using only the standard errors and the star values. This is done as follows:

est clear  // we want to get rid of old estimates. All new estimates are added at the bottom of the old file.
esttab using stacking.rtf, append cells(se(fmt(a3) star par([ ]))) keep(weight)  // notice the “append” option here.

This gives us the following output stacked under the above output. The par([ ]) replaces the round brackets with square brackets.

—————————-
(1)
price
se
—————————-
weight            [0.778]*
—————————-
N                    74
—————————-

This table has the right output but also includes too much additional information. We need to get rid of the column headers and the lines. Similarly if we want to stack this below the first output, we need to get rid of the “N” row.

We do this using the following set of commands to generate the clear rtf file.
est clear
eststo: regress price weight mpg
esttab using stacking.rtf, replace cells(b(fmt(a3)) se(fmt(a3) star par)) keep(weight) ///
label noobs nonotes nomtitle noline collabels(none)

est clear
label weight ” “
eststo: regress price weight mpg, vce(robust)
esttab using stacking.rtf, append cells(se(fmt(a3) star par([ ]))) keep(weight)
label nomtitle noline nonumber collabels(none)

Several options are used here:
label: adds variable labels to the output. For the second regression output, we change the label to a blank. NOTE the blank space in the label.
noobs: is no observations, which removes the “N” row in the first output.
nonotes: removes all notes from the first output. Usually these are stars and significance levels.
noline: removes all lines in the output.
nonumber: removes the column number from the header. First value.
nomtitle: removes the name of the dependent variables from the header given the second line (price in this case).
colllabels(none): removes the third line from the header (b/se in the first output and se in the second output).

This will give us an output which is very similar to the original table we wanted:

—————————
(1)
—————————
Weight         1.747
(lbs.)        (0.641)**
[0.778]*

—————————
N               74
—————————

In the second blog – Stata to Latex – Part 2, we discuss how this output can be exported and formatted to Latex in a slightly more complex example.