Search code examples
latexstataoverleaf

Stata - Esttab to Latex Decimal Alignment


I am trying to output a LaTex file using Stata's esttab command. I cannot get the decimals to align, nor can I get the nice formatting from the "booktabs" option to work. I also would like to increase the width of the columns. I have been tinkering with this for hours and have had no luck. Below is my Stata code and the LaTex code that I am running through Overleaf. Any help would be greatly appreciated!

Here is my Stata code:

  #delimit ;

 esttab r1 r2 r3 r4 using "C:\Users\user\Dropbox\Private 
     Code\Code\STATA\latex\table1.tex", replace
     b(3) nomtitle label star(* 0.10 ** 0.05 *** 0.01) 
     booktabs alignment(D{.}{.}{-1})
     title(Placeholder)
     s(N DAY FFE FIRM_QUARTER r2_a, 
     fmt(%9.0fc 0 0 0 3)
     label("Observations" "Day FE" "Firm FE" "Firm-Quarter FE" "Adj. R-Sq{\super 2}")) 
 compress nocons;

Here is the LaTex code it spits out:

\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{Placeholder}
\begin{tabular}{l*{4}{D{.}{.}{-1}}}
\toprule
            &\multicolumn{1}{c}{(1)}         &\multicolumn{1}{c}{(2)}         &\multicolumn{1} 
{c}{(3)}         &\multicolumn{1}{c}{(4)}         \\
\midrule
VAR1            &    0.007\sym{***}&    0.007\sym{***}&    0.006\sym{***}&    0.007\sym{***}\\
            &   (8.55)         &   (7.82)         &   (7.67)         &   (8.54)         \\
\addlinespace
VAR2            &    0.132\sym{***}&    0.131\sym{***}&    0.131\sym{***}&    0.132\sym{***}\\
            &  (20.35)         &  (20.15)         &  (20.15)         &  (20.35)         \\
\midrule
 Observations    &1,315,077         &1,315,077         &1,315,077         &1,315,077         
\\
Day FE          &       No         &      Yes         &      Yes         &       No         \\
Firm FE         &      Yes         &       No         &      Yes         &       No         \\
Firm-Quarter FE &       No         &       No         &       No         &      Yes         \\
Adj. R-Sq{\super 2}&    0.045         &    0.027         &    0.046         &    0.044         
\\
\bottomrule
\multicolumn{5}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{5}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \( 
p<0.01\)}\\
\end{tabular}
\end{table}

Solution

  • Since you haven't provided the full code, I can't see what would your output be. When I add \documentclass{article} with missing packages in a preamble, I get very wide table which does not fit a page. Also, I don't know whether the code is automatically generated or created/altered by you.

    Assuming the latter, I would do the following. Since you add footnotes in the table, I would enclose tabular within threeparttable, which splits the area for table into three parts: top for captions, middle for a table definition e.g. tabular, and bottom part for footnotes. It also adds a convenient macro \tnote{}.

    Secondly, dcolumn causes big numbers to be pushed way too much to the left. You should let dcolumn suppress their alignment. I personally don't know how, although I assume there is a way.

    I prefer siunitx for formatting numbers in tables. However siunitx needs a bit more work. It automatically parses numbers in cells and any non-number content generates errors. Therefore, non-numbers need to be enclosed within {...} which instructs siunitx to leave the content intact. It also forces numbers not to be formatted, aligned etc., which is what I did for the big numbers you have in the table. There's also option to format thousands and millions with any separator of your choice. For instance you could use one of the macros: \,, :, or \; for thin, mid or thick space, respectively instead of ,.

    Below is my suggestion for your table based on siunitx and threeparttable, so you can compare it with your approach.

    enter image description here

    \documentclass{article}
    \usepackage{array}
    \usepackage{booktabs}
    \usepackage{siunitx}
    \usepackage{threeparttable}
    
    \newcommand\super[1]{\textsuperscript{#1}}
    \sisetup{
      group-separator={,},
      table-format=1.3,
      table-align-text-after = false,
    }
    
    \begin{document}
    \begin{table}[htbp]
      \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
      \centering
      \begin{threeparttable}
        \caption{Placeholder}
        \begin{tabular}{l *{4}{S}}
          \toprule
          & \multicolumn{1}{c}{(1)}
            & \multicolumn{1}{c}{(2)}
            & \multicolumn{1}{c}{(3)}
            & \multicolumn{1}{c}{(4)} \\
          \midrule
          {VAR1}             & 0.007\tnote{***} & 0.007\tnote{***} & 0.006\tnote{***} & 0.007\tnote{***} \\
                             & {(8.55)}         & {(7.82)}         & {(7.67)}         & {(8.54)}         \\
          \addlinespace
          VAR2               & 0.132\tnote{***} & 0.131\tnote{***} & 0.131\tnote{***} & 0.132\tnote{***} \\
                             & {(20.35)}        & {(20.15)}        & {(20.15)}        & {(20.35)}        \\
          \midrule
          Observations
            & \multicolumn{1}{c}{\num{1315077}}
            & \multicolumn{1}{c}{\num{1315077}}
            & \multicolumn{1}{c}{\num{1315077}}
            & \multicolumn{1}{c}{\num{1315077}} \\
          Day FE             & {No}             & {Yes}            & {Yes}            & {No}             \\
          Firm FE            & {Yes}            & {No}             & {Yes}            & {No}             \\
          Firm-Quarter FE    & {No}             & {No}             & {No}             & {Yes}            \\
          Adj. R-Sq{\super2} & 0.045            & 0.027            & 0.046            & 0.044            \\
          \bottomrule
        \end{tabular}
        \par\(t\) statistics in parentheses
        \par\tnote{*} \(p < 0.10\),\quad\tnote{**} \(p < 0.05\),\quad\tnote{***} \(p < 0.01\)
      \end{threeparttable}
    \end{table}
    \end{document}
    

    EDIT. I also enclosed statistics in{...} because siunitx does not recognised brackets as part of a number.