Routine to export tables from Stata to LaTeX

When it comes to exporting regression tables from Stata to LaTeX, there are several packages that can be used. For a long time, I used outreg2, but now improved my code for the the estout package by Ben Jann. The remainder of this post describes the routine that works best for, you might want to…

When it comes to exporting regression tables from Stata to LaTeX, there are several packages that can be used. For a long time, I used outreg2, but now improved my code for the the estout package by Ben Jann. The remainder of this post describes the routine that works best for, you might want to adjust it for your own purposes. (I actually have written about this package before, this post presents a more elaborated code.)

My aim is to have a “skeleton” of the table in the LaTeX file including the header of the table (title, placement of the table, label, number of columns, etc.) and the footer of the table (table notes, closing commands etc.). Below I will first describe the commands used in Stata to export the table, and then the code I use in LaTeX to read the exported file.

In Stata, I make use of the estout package (install this first if you haven’t done yet). In this example, I have two regressions that I would like to import to my LaTeX document. Next to the standard output, I would also like to export the number of individuals (variable ind) to the table (this comes in handy when using panel data). For the latter, we have to use the package estadd, which comes with the installation of the estout package.

In the beginning of the do-file, I specified a global $output to define the path where the table should be stored. The esttab code after the regressions is written in such a way that just the lines with the numbers are stored (fragment option), and that I can then adjust all the table layout easily in the LaTeX file.

* Regression for Column 1
reg depvar xvar1 xvar2, cluster(ind)
xtsum ind if e(sample)==1
estadd scalar n_ind = r(n)
eststo reg1

* Regression for Column 2
reg depvar xvar1 xvar2 xvar3, cluster(ind)
xtsum ind if e(sample)==1
estadd scalar n_ag = r(n)
eststo reg2

esttab reg1 reg2 using $output/table1.tex, mtitles("Spec 1" "Spec 2") keep(xvar1 xvar2 xvar3 _cons) star(* 0.10 ** 0.05 *** 0.01) collabels(none) label stats(r2 n_ind N, fmt(%9.4f %9.0f %9.0fc) labels("R-squared" "Number of individuals" "Number of observations")) plain b(%9.4f) se(%9.4f) noabbrev se nonumbers lines parentheses replace fragment

After running this code, the LaTeX file just needs the following code to read this table.

% This is the code I have in my LaTeX file to generate the table 

\begin{table}[!htbp]
\begin{center}
\caption{Title of the table \label{tab:est1}}
\begin{scriptsize}
\begin{tabular}{l|cc}
\hline\hline
& (1) & (2)\\
\input{infile/est1.tex}
\hline\hline
\end{tabular}\\
\parbox{\textwidth}{\scriptsize {\em Note:} *** p<0.01, ** p<0.05, * p<0.1. Dependent variable: y_{it}. And add other notes here.} 
\end{scriptsize} 
\end{center} 
\end{table}

When working on this code, I came across one more extremely helpful tool in Stata to adjust the tables even further. Suppose your code generates three additional statistics lines (R2, number of individuals, and number of observations), and you want, for example, another horizontal line between the R2 and the number of individuals and number of observations. This can easily be achieved by running the filefilter command in Stata.

filefilter "$output/table1.tex" "$output/table1adj.tex", from("Number of individuals") to("\W\BShline\W Number of individuals") replace

In -help filefilter– the codes are explained (\BS stands for backslash, for example). The only disadvantage I see with it, that you cannot directly overwrite the files, but have to generate a new one.

Leave a Reply