Announcement

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

  • How to produce extended summary stat, one row per variable and then exporting to latex while replacing variables names with their labels?

    I want to have a summary stat of 30 variables with statistics p1 mean sd p99 with each row representing one variable, and I want to send the results to latex using esttab. The important thing is I want labels instead of variable names. Hence the output should look like this (but in latex):
    p1 mean sd p99
    Net Assets
    Output per Worker
    Thanks in advance.
    Last edited by John Williamss; 27 Jul 2023, 19:30.

  • #2
    I would use frames and texsave from SSC here.

    Code:
    ssc install texsave, replace
    Example:

    Code:
    frame create stats str100(varlab) float(p1 mean sd p99)
    *LOAD DATASET
    sysuse auto, clear
    foreach var in mpg weight disp length turn{
        sum `var', d
        frame post stats ("`:var lab `var''") (r(p1)) (r(mean)) (r(sd)) (r(p99))
    }
    frame change stats
    *EXPORT AS TeX FILE
    texsave * using myfile.tex, replace
    Res.:

    Code:
    \documentclass{article}
    
    \usepackage{booktabs}
    
    \usepackage{tabularx}
    
    \usepackage[margin=1in]{geometry}
    
    \begin{document}
    
    
    
    
    \begin{table}[tbp] \centering
    
    \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
    
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    
    \newcolumntype{C}{>{\centering\arraybackslash}X}
    
    
    
    
    \begin{tabularx}{\linewidth}{@{}lCCCC@{}}
    
    
    
    
    \toprule
    
    {varlab}&{p1}&{mean}&{sd}&{p99} \tabularnewline
    
    \midrule \addlinespace[\belowrulesep]
    
    Mileage (mpg)&12&21.2973&5.785503&41 \tabularnewline
    
    Weight (lbs.)&1760&3019.459&777.1935&4840 \tabularnewline
    
    Displacement (cu. in.)&79&197.2973&91.83722&425 \tabularnewline
    
    Length (in.)&142&187.9324&22.26634&233 \tabularnewline
    
    Turn Circle (ft.) &31&39.64865&4.399354&51 \tabularnewline
    
    \bottomrule
    
    
    
    
    \end{tabularx}
    
    \end{table}
    
    \end{document}
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	36.5 KB
ID:	1722156
    Last edited by Andrew Musau; 28 Jul 2023, 07:31.

    Comment


    • #3

      Andrew gives a great example if you have Stata 16 or newer.

      If you have Stata 17 or newer, you can use the re-imagined table
      command with the new collect suite.

      Here is a twist on Andrew's example, using table in Stata 17.

      Code:
      sysuse auto
      
      * build your variables list
      unab vlist : mpg weight disp length turn
      
      * compute stats for each variable
      table (var) (result), ///
          stat(p1 `vlist') ///
          stat(mean `vlist') ///
          stat(sd `vlist') ///
          stat(p99 `vlist')
      
      * change column headers to use the stat names instead of their labels
      collect style header result, level(value)
      collect preview
      
      * export to LaTeX
      collect export table.tex, replace
      Here is the exported 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}{p1} &
        \multicolumn{1}{r}{mean} &
        \multicolumn{1}{r}{sd} &
        \multicolumn{1}{r}{p99} \\
      \cline{1-5}
      \multicolumn{1}{l}{Mileage (mpg)} &
        \multicolumn{1}{|r}{12} &
        \multicolumn{1}{r}{21.30} &
        \multicolumn{1}{r}{5.79} &
        \multicolumn{1}{r}{41} \\
      \multicolumn{1}{l}{Weight (lbs.)} &
        \multicolumn{1}{|r}{1760} &
        \multicolumn{1}{r}{3019.46} &
        \multicolumn{1}{r}{777.19} &
        \multicolumn{1}{r}{4840} \\
      \multicolumn{1}{l}{Displacement (cu. in.)} &
        \multicolumn{1}{|r}{79} &
        \multicolumn{1}{r}{197.30} &
        \multicolumn{1}{r}{91.84} &
        \multicolumn{1}{r}{425} \\
      \multicolumn{1}{l}{Length (in.)} &
        \multicolumn{1}{|r}{142} &
        \multicolumn{1}{r}{187.93} &
        \multicolumn{1}{r}{22.27} &
        \multicolumn{1}{r}{233} \\
      \multicolumn{1}{l}{Turn circle (ft.)} &
        \multicolumn{1}{|r}{31} &
        \multicolumn{1}{r}{39.65} &
        \multicolumn{1}{r}{4.40} &
        \multicolumn{1}{r}{51} \\
      \cline{1-5}
      \end{tabular}
      \end{table}
      \end{document}
      Click image for larger version

Name:	Screenshot 2023-07-28 at 10.21.41 AM.png
Views:	1
Size:	54.2 KB
ID:	1722169

      Comment

      Working...
      X