Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Export summary stats table by group variable to LaTex

    Dear Statalisters,

    Is there a way to output a summary stats table (like the one below) to LaTex using Stata 17? Essentially, the table must summarize a list of variables by a group variable (i.e. Female dummy). So far, I've tried using
    Code:
    tabstat
    with no luck.

    I apologize for not pasting any state code that I've tried. Nothing has come close to producing such a table.

    Thanks in advance!

    Code:
    {
    \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
    \begin{tabular}{l*{1}{rrrr}} \hline \hline
                                                    &        Mean&   Std. Dev.&         Min&         Max\\ \hline
    Female                                  &            \\[.25cm]
    Earnings, in thousands of US dollars            &      243.89&      293.30&          25&        3000\\
    \hspace{.2in} Female                         &      196.23&      209.93&          25&        3000\\
    \hspace{.2in} Male                         &      263.87&      319.80&          25&        3000\\ [.25cm]
    Age                                             &       46.73&        8.45&          29&          75\\
    \hspace{.2in} Female                         &       45.89&        9.17&          29&          75\\
    \hspace{.2in} Male                         &       47.08&        8.11&          29&          68\\[.25cm]
    Average number of hours worked per week    &       50.20&       11.79&          10&         100\\
    \hspace{.2in} Female                     &       47.47&       11.13&          10&         100\\
    \hspace{.2in} Male                         &       51.35&       11.87&          10&         100\\[.25cm]
    At least one child                          &        0.78&        0.41&           0&           1\\
    \hspace{.2in} Female                     &        0.69&        0.46&           0&           1\\
    \hspace{.2in} Male                         &        0.82&        0.38&           0&           1\\[.25cm]
    At least one child below the age of 3            &        0.13&        0.33&           0&           1\\
    \hspace{.2in} Female                     &        0.12&        0.33&           0&           1\\
    \hspace{.2in} Male                         &        0.13&        0.34&           0&           1\\[.25cm]
    Married at time of survey (2022)            &        0.78&        0.42&           0&           1\\
    \hspace{.2in} Female                      &        0.68&        0.47&           0&           1\\
    \hspace{.2in} Male                         &        0.82&        0.39&           0&           1\\
    \hline \hline
    \end{tabular}
    }

  • #2
    Yes, in Stata 17 table was re-implemented using the new collect commands.
    Here is an example using the auto data.
    Code:
    sysuse auto
    
    * list of variables to summarize
    local vlist mpg turn trunk displ
    
    * compute overall statistics and store them in a collection named 'overall'
    table (var) (result), ///
        stat(mean `vlist') ///
        stat(sd `vlist') ///
        stat(min `vlist') ///
        stat(max `vlist') ///
        name(overall) ///
        nototal
    collect addtags foreign[_hide]
    
    * compute statistics within levels of variable 'foreign' and store them in a 
    * collection named 'by'
    table (var foreign) (result), ///
        stat(mean `vlist') ///
        stat(sd `vlist') ///
        stat(min `vlist') ///
        stat(max `vlist') ///
        name(by) ///
        nototal
    
    * get levels of the 'by' variable 'foreign' and create a macro to later use
    * to set the automatic levels for 'foreign'
    collect levels foreign
    local auto _hide `s(levels)'
    di "`auto'"
    
    * combine the collections
    collect combine both = by overall
    
    * fix the autolevels of 'foreign'
    collect style autolevels foreign `auto', clear
    
    * change the result labels to match the original LaTeX example
    collect label levels result sd "Std. Dev." min "Min" max "Max", modify
    
    * other style changes
    collect style cell result[mean sd], nformat(%18.2fc)
    collect style header foreign, title(hide)
    
    * set the layout and verify the table look like we want
    collect layout (var#foreign) (result)
    
    * export to LaTeX file
    collect export table.tex, replace
    Here is the resulting table.
    Code:
    . collect layout (var#foreign) (result)
    
    Collection: both
          Rows: var#foreign
       Columns: result
       Table 1: 12 x 4
    
    --------------------------------------------------------
                           |    Mean   Std. Dev.   Min   Max
    -----------------------+--------------------------------
    Mileage (mpg)          |   21.30        5.79    12    41
      Domestic             |   19.83        4.74    12    34
      Foreign              |   24.77        6.61    14    41
    Turn circle (ft.)      |   39.65        4.40    31    51
      Domestic             |   41.44        3.97    31    51
      Foreign              |   35.41        1.50    32    38
    Trunk space (cu. ft.)  |   13.76        4.28     5    23
      Domestic             |   14.75        4.31     7    23
      Foreign              |   11.41        3.22     5    16
    Displacement (cu. in.) |  197.30       91.84    79   425
      Domestic             |  233.71       85.26    86   425
      Foreign              |  111.23       24.88    79   163
    --------------------------------------------------------
    Here is the resulting LaTeX file
    Code:
    \documentclass{article}
    \usepackage{multirow}
    \usepackage{amsmath}
    \usepackage{ulem}
    \usepackage[table]{xcolor}
    \begin{document}
    \begin{table}[!h]
    \centering
    \begin{tabular}{lllll}
    \cline{1-5}
    \multicolumn{1}{c}{} &
      \multicolumn{1}{|r}{Mean} &
      \multicolumn{1}{r}{Std. Dev.} &
      \multicolumn{1}{r}{Min} &
      \multicolumn{1}{r}{Max} \\
    \cline{1-5}
    \multicolumn{1}{l}{Mileage (mpg)} &
      \multicolumn{1}{|r}{21.30} &
      \multicolumn{1}{r}{5.79} &
      \multicolumn{1}{r}{12} &
      \multicolumn{1}{r}{41} \\
    \multicolumn{1}{l}{\hspace{1em}Domestic} &
      \multicolumn{1}{|r}{19.83} &
      \multicolumn{1}{r}{4.74} &
      \multicolumn{1}{r}{12} &
      \multicolumn{1}{r}{34} \\
    \multicolumn{1}{l}{\hspace{1em}Foreign} &
      \multicolumn{1}{|r}{24.77} &
      \multicolumn{1}{r}{6.61} &
      \multicolumn{1}{r}{14} &
      \multicolumn{1}{r}{41} \\
    \multicolumn{1}{l}{Turn circle (ft.)} &
      \multicolumn{1}{|r}{39.65} &
      \multicolumn{1}{r}{4.40} &
      \multicolumn{1}{r}{31} &
      \multicolumn{1}{r}{51} \\
    \multicolumn{1}{l}{\hspace{1em}Domestic} &
      \multicolumn{1}{|r}{41.44} &
      \multicolumn{1}{r}{3.97} &
      \multicolumn{1}{r}{31} &
      \multicolumn{1}{r}{51} \\
    \multicolumn{1}{l}{\hspace{1em}Foreign} &
      \multicolumn{1}{|r}{35.41} &
      \multicolumn{1}{r}{1.50} &
      \multicolumn{1}{r}{32} &
      \multicolumn{1}{r}{38} \\
    \multicolumn{1}{l}{Trunk space (cu. ft.)} &
      \multicolumn{1}{|r}{13.76} &
      \multicolumn{1}{r}{4.28} &
      \multicolumn{1}{r}{5} &
      \multicolumn{1}{r}{23} \\
    \multicolumn{1}{l}{\hspace{1em}Domestic} &
      \multicolumn{1}{|r}{14.75} &
      \multicolumn{1}{r}{4.31} &
      \multicolumn{1}{r}{7} &
      \multicolumn{1}{r}{23} \\
    \multicolumn{1}{l}{\hspace{1em}Foreign} &
      \multicolumn{1}{|r}{11.41} &
      \multicolumn{1}{r}{3.22} &
      \multicolumn{1}{r}{5} &
      \multicolumn{1}{r}{16} \\
    \multicolumn{1}{l}{Displacement (cu. in.)} &
      \multicolumn{1}{|r}{197.30} &
      \multicolumn{1}{r}{91.84} &
      \multicolumn{1}{r}{79} &
      \multicolumn{1}{r}{425} \\
    \multicolumn{1}{l}{\hspace{1em}Domestic} &
      \multicolumn{1}{|r}{233.71} &
      \multicolumn{1}{r}{85.26} &
      \multicolumn{1}{r}{86} &
      \multicolumn{1}{r}{425} \\
    \multicolumn{1}{l}{\hspace{1em}Foreign} &
      \multicolumn{1}{|r}{111.23} &
      \multicolumn{1}{r}{24.88} &
      \multicolumn{1}{r}{79} &
      \multicolumn{1}{r}{163} \\
    \cline{1-5}
    \end{tabular}
    \end{table}
    \end{document}
    Here is a screenshot of the resulting PDF.

    Click image for larger version

Name:	Screenshot 2023-08-05 at 8.16.19 PM.png
Views:	1
Size:	94.4 KB
ID:	1723001

    Comment


    • #3
      That felt like too much work. I was trying to reproduce the original example layout. You can also do this with a single call to table.

      The following reproduces the above table, with a little less effort.
      Code:
      sysuse auto
      
      local vlist mpg turn trunk displ
      
      * compute summary statistics
      table (var foreign) (result), ///
          stat(mean `vlist') ///
          stat(sd `vlist') ///
          stat(min `vlist') ///
          stat(max `vlist') ///
          name(by)
      
      * reorder the levels of 'foreign' and hide the 'Total' level
      collect levelsof foreign
      collect style autolevels foreign .m `s(levels)', clear
      collect style header foreign[.m], level(hide)
      
      * change the result labels to match the original LaTeX example
      collect label levels result sd "Std. Dev." min "Min" max "Max", modify
      
      * other style changes
      collect style cell result[mean sd], nformat(%18.2fc)
      collect style header foreign, title(hide)
      
      * review table look
      collect preview
      
      * export to LaTeX file
      collect export table.tex, replace

      Comment


      • #4
        Beautiful! Thank you, Jeff.

        I cannot believe I didn't know about this set of commands- game changer. I'm pasting your code with the modifications I made in bold to get the precise output I wanted.

        Code:
        sysuse auto
        local vlist mpg turn trunk displ  
        
        * compute summary statistics
        table (var foreign) (result), ///
        stat(mean `vlist') ///    
        stat(sd `vlist') ///    
        stat(min `vlist') ///    
        stat(max `vlist') ///    
        name(by)  
        
        * reorder the levels of 'foreign' and hide the 'Total' level
        collect levelsof foreign
        collect style autolevels foreign .m `s(levels)', clear
        collect style header foreign[.m], level(hide)
         
        * change the result labels to match the original LaTeX example
        collect label levels result sd "Std. Dev." min "Min" max "Max", modify  
        
        * other style changes
        collect style cell result[mean sd], nformat(%18.2fc)
        collect style header foreign, title(hide)
        collect style row stack, nobinder spacer // add space between each variable being summarized
        collect style cell border_block, border(right, pattern(nil)) // remove the border that appeared to the right of the row labels
        
        * review table look
        collect preview  
        
        * export to LaTeX file
        collect title "Descriptive Statistics" // add title
        collect export table.tex, tableonly replace // leave out Latex preamble in export
        Last edited by Noor Sethi; 06 Aug 2023, 12:39.

        Comment

        Working...
        X