# Tables of Descriptive Statistics using esttab in Stata and Latex

Suppose you have Stata’s dataset auto.dta and want to build a table in latex containing the descriptive statistics for the cars by origin, that is, foreign or national as indicated by the dummy foreign. Consider in addition that you want to divide the variables in the dataset into two groups, Group 1 and Group 2, and have them identified in the table. Here is the Stata code to accomplish that.

sysuse auto

eststo clear

estpost sum headroom trunk weight length turn displacement gear_ratio price mpg rep78 if foreign==1
est store a
estpost sum headroom trunk weight length turn displacement gear_ratio price mpg rep78 if foreign==0
est store b

esttab a b using desc.tex, replace ///
mtitles("\textbf{\emph{Foreign}}" "\textbf{\emph{National}}") ///
refcat(headroom "\textbf{\emph{Group 1}}" price "\textbf{\emph{Group 2}}", nolabel) ///
collabels(\multicolumn{1}{c}{{Mean}} \multicolumn{1}{c}{{Std.Dev.}} \multicolumn{1}{l}{{Obs}})///
cells("mean(fmt(2)) sd(fmt(2)) count(fmt(0))") label nonumber f noobs alignment(S) booktabs


The esttab options make use of the packages booktabs, which produces better looking tables, and siunitx, that allows for adjustments in the alignment of the columns.
So in your tex file you have to specify these two packages and add the extra lines of code below, which I have adapted from Jorg Weber tutorials. Check his posts for more details on the code and more types of tables.
Here is the table and tex source code.

\documentclass[11pt]{article}
\usepackage{booktabs}
\usepackage{siunitx}

% *****************************************************************
% Estout related things
% *****************************************************************
\newcommand{\sym}[1]{\rlap{#1}}

\let\estinput=\input

\newcommand{\estwide}[3]{
\vspace{.75ex}{
\begin{tabular*}
{\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\end{tabular*}
}
}

\newcommand{\estauto}[3]{
\vspace{.75ex}{
\begin{tabular}{l*{#2}{#3}}
\toprule
\estinput{#1}
\bottomrule
\end{tabular}
}
}

% Allow line breaks with \\ in specialcells
\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}

% *****************************************************************
% Custom subcaptions
% *****************************************************************
% Note/Source/Text after Tables
\newcommand{\figtext}[1]{
\vspace{-0.6ex}
\captionsetup{justification=justified,font=footnotesize,labelformat = simple}
\caption*{\hspace{0pt}\hangindent=1.0em #1}
}

\newcommand{\fignote}[1]{\figtext{\emph{Note:~}~#1}}
\newcommand{\figsource}[1]{\figtext{\emph{Source:~}~#1}}

% Add significance note with \starnote

\newcommand{\starnotea}{\figtext{* $$p<0.1$$, ** $$p<0.05$$, *** $$p<0.01$$.  Standard errors in brackets.}}%
\newcommand{\starnote}{\figtext{* $$p<0.1$$, ** $$p<0.05$$, *** $$p<0.01$$.  Standard errors are clustered by state.}}%

% Note/Source/Text after Tables
\newcommand{\Figtext}[1]{%
\begin{tablenotes}[para,flushleft]
\hspace{6pt}
\hangindent=1.75em
#1
\end{tablenotes}
}
\newcommand{\Fignote}[1]{\Figtext{\emph{Note:~}~#1}}
\newcommand{\Figsource}[1]{\Figtext{\emph{Source:~}~#1}}

\usepackage{siunitx} % centering in tables
\sisetup{
detect-mode,
tight-spacing		= true,
group-digits		= false ,
input-signs		= ,
input-symbols		= ( ) [ ] - + *,
input-open-uncertainty	= ,
input-close-uncertainty	= ,
table-align-text-post	= false
}

\begin{document}
\begin{table}\centering
\caption{Descriptive Statistics by Type of Car}
\estauto{desc}{7}{S}
\end{table}

\end{document}


## 2 thoughts on “Tables of Descriptive Statistics using esttab in Stata and Latex”

1. Fanny Paulet Lemus Garcìa says:

what is the desc.tex yu mentioned?

Like