Announcement

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

  • Need help creating tex table by two variables

    I would like to create a table that includes the mean and standard deviation of a group of variables by the study region and year. I can create these as individual tables per study region but cannot append them into a single table. They do append but look like separate tables and include headers in each section. When I use the fragment option I lose all the formatting. Here is the code:
    Code:
    global variables gdp def_annMB milk_harvest cattle cows 
    
    foreach x of varlist $variables {
      local t : var label `x'
      local t = "\hspace{0.2cm} `t'"
      lab var `x' "`t'"
    }
    
    estimates clear
    levelsof year, local(groups)
    local i 0
    foreach year of local groups{
        local ++i
        estpost tabstat $variables if year==`year' & studycode==1, by(year) c(stat) stat(mean sd) nototal
        eststo north`i'
    }
    
    esttab north* using "$tables/t_des_study.tex", replace  ///
        cells(mean(fmt(2)) sd(par)) nostar  nonumber unstack ///
        nomtitle nonote noobs label  booktabs ///
        eqlabels("2000" "2010" "2020" "2000-2010" "2010-2020") refcat( gdp " \emph{North Region}" , nolabel) title(Descriptive Statistics for Muncipalities\label{t-descstats-drought})
    
    estimates clear
    levelsof year, local(groups)
    local i 0
    foreach year of local groups{
        local ++i
        estpost tabstat $variables if year==`year' & studycode==2, by(year) c(stat) stat(mean sd) nototal
        eststo center`i'
    }
    esttab center* using "$tables/t_des_study.tex", append  ///
        cells(mean(fmt(2)) sd(par)) nostar  nonumber unstack ///
        nomtitle nonote noobs label  refcat( gdp " \emph{Central Region}" , nolabel) booktabs
    The data can be found here

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long codigo float(year def_annMB) double(gdp milk_harvest cattle cows) float studycode
    1100015 2000 106.90625    98.023  7.846  191.685      0 4
    1100015 2010  9.513184   262.077   3.16  364.184      0 4
    1100015 2020 37.073975   570.272  7.258   460.45      0 4
    1100023 2000  75.30444   381.793  6.391  235.069 11.932 1
    1100023 2010  4.919556  1364.694  7.083  439.355 12.678 1
    1100023 2020  36.78833  2818.049 16.374  499.067 11.311 1
    1100031 2000 22.491394    33.496   7.37   92.871  7.485 4
    1100031 2010 1.1720886    69.611   5.14  126.455  9.014 4
    1100031 2020  5.508789    167.19  6.259  121.887  5.115 4
    1100049 2000 26.621094   385.936 24.947  317.619 27.282 4
    1100049 2010 2.3450928  1186.494 12.728  417.489 20.649 4
    1100049 2020 17.419434  2519.353 18.668  427.096 12.627 4
    1100056 2000 20.024414    89.765  6.714   97.671  6.818 4
    1100056 2010   6.50354   222.021  4.438   88.031   7.03 4
    1100056 2020  38.89502    600.67  3.375   84.738  2.057 4
    1100064 2000  19.74338     83.49 11.883  156.221 10.529 4
    1100064 2010  .4353027   193.093  9.871  230.173 18.279 4
    1100064 2020  3.280594   366.931  9.942  260.077  7.444 4
    1100072 2000 26.878296    40.046  7.908  162.287  7.887 4
    1100072 2010 1.2489014   114.768 10.627  256.878 18.038 4
    1100072 2020  9.560669   268.381 11.108  309.415  9.269 4
    1100080 2000  51.56299    24.507   .462    9.861   .913 4
    1100080 2010  32.16284   107.583   2.04  158.812  3.777 4
    1100080 2020  44.01733   261.978  7.245  261.485  5.894 4
    1100098 2000  49.91675   111.948  17.07   222.72 16.638 4
    1100098 2010  5.894531   311.788 11.591  364.625 20.663 4
    1100098 2020  23.50928   666.331 20.418  386.629 15.548 4
    1100106 2000  48.32031   136.125  3.304   56.837  3.387 4
    1100106 2010 22.015625   598.167  2.565  115.725  4.496 4
    1100106 2020  50.17773   984.586  3.084  136.716  2.032 4
    1100114 2000  59.90503   253.157  18.39  285.104 44.927 4
    1100114 2010  .8766785   797.296 81.833  505.302 92.186 4
    1100114 2020  7.429749  1665.068 68.266  526.279 44.248 4
    1100122 2000  32.53369   512.578 41.894  318.748 40.832 4
    1100122 2010 2.2080078  1855.359 36.652  436.353 48.881 4
    1100122 2020  8.092285  4057.626 32.073  393.578 22.156 4
    1100130 2000 117.12256    66.506  2.852   53.976  3.799 4
    1100130 2010 24.805176   272.201 29.808  257.179   36.8 4
    1100130 2020 69.273926   700.317 71.331  407.629 46.151 4
    1100148 2000  58.46912    70.525  5.761   83.807  4.894 4
    1100148 2010  3.517517   173.651  8.015   125.81 12.889 4
    1100148 2020  6.621918    403.37 15.755  230.251  9.194 4
    1100155 2000   18.1604   181.024 53.007  259.615 43.434 2
    1100155 2010  .4201965   477.501 64.465  336.278 75.702 2
    1100155 2020 11.758057   885.193 62.256  356.886 39.704 2
    1100189 2000  54.43408   143.166  5.618  171.439  7.594 3
    1100189 2010  19.10083   540.862  5.546  238.284  9.489 3
    1100189 2020  63.92725  1361.243  5.371  312.938   4.09 3
    1100205 2000  238.6992  1469.893   5.19  160.918  6.509 4
    1100205 2010 168.67383  9093.824  4.028   609.86  6.794 4
    1100205 2020  528.4082 19448.762  48.26 1267.441 34.198 4
    1100254 2000 35.756622   101.785 21.792  191.835 21.735 4
    1100254 2010  .4766235   235.589 28.789  285.603 39.111 4
    1100254 2020 2.7267456   464.219 27.271  349.255 18.481 4
    1100262 2000 33.660828    15.428   .895    70.18  1.468 4
    1100262 2010 3.3951416    41.934    .86  131.458  1.311 4
    1100262 2020  15.41443    133.58  2.173  120.602  1.453 4
    1100288 2000  22.74811   208.769  12.77  163.234 16.731 3
    1100288 2010  .6217194   627.848 16.101  223.627 21.472 3
    1100288 2020  4.052887  1397.881  12.76  238.534 10.287 3
    1100296 2000 11.471954     43.42  5.869  114.362  7.913 3
    1100296 2010 .46084595    98.818  8.357  166.146 13.231 3
    1100296 2020   3.10231   156.789  9.761  180.501   6.59 3
    1100304 2000 64.134766   427.561  4.183   66.974  3.442 4
    1100304 2010  19.40039  1447.187  1.958   94.648  3.247 4
    1100304 2020  50.08203      3153  3.086  142.155  2.551 4
    1100320 2000   84.8667     79.61  6.753   91.243  6.834 4
    1100320 2010  9.090332   324.476 23.262  279.618 30.827 4
    1100320 2020 28.847656   730.695 17.458  258.493 13.825 4
    1100338 2000 140.63184    40.289  6.516    78.17  7.168 4
    1100338 2010  66.11426   218.431 32.263   416.24 38.583 4
    1100338 2020 170.23193    601.91 51.576  769.264 40.306 4
    1100346 2000   88.9176    68.022 11.741  136.606 16.106 4
    1100346 2010 2.2088623   160.689  30.83  230.455 36.679 4
    1100346 2020  5.574951   289.533 19.712  267.652 12.989 4
    1100379 2000 69.476074    39.891  1.294   59.025  1.712 4
    1100379 2010  5.576904   119.974  2.434  150.237  4.339 4
    1100379 2020  18.97522   324.544  4.743  218.733  3.354 4
    1100403 2000  63.12781    43.144  4.237   64.372    4.5 4
    1100403 2010  8.935181   152.235  2.957  223.768  5.475 4
    1100403 2020     50.37   396.377  8.268  342.015  4.817 4
    1100452 2000 137.26025    60.342  1.845    33.88  2.218 4
    1100452 2010  28.28772   336.265 27.249  423.659 37.372 4
    1100452 2020  46.43292   781.624  28.56  520.182  21.61 4
    1100502 2000 17.361511    38.449  5.524    84.13  6.789 4
    1100502 2010  .8047028     95.42 11.168  127.459  15.51 4
    1100502 2020 2.2640915     185.1  11.03  156.192  6.711 4
    1100601 2000  53.70764    28.957  5.121  127.622  4.774 1
    1100601 2010 1.8945923    76.701  6.132  250.628  9.853 1
    1100601 2020 16.649445   169.164  9.291  279.099  6.066 1
    1100700 2000  95.60596    27.682   3.04     36.3  2.292 4
    1100700 2010 19.982056    120.44 13.569  300.509 25.395 4
    1100700 2020 19.820557   331.933 34.762  437.296 25.811 4
    1100809 2000  58.15723    46.638  4.113   66.444  3.783 4
    1100809 2010 19.010254   228.321  4.064  190.632  7.785 4
    1100809 2020 139.69238     563.1 14.134  269.105  9.821 4
    1100908 2000 16.165771    17.637  3.585   71.531  4.099 4
    1100908 2010  .7885132    39.936  3.881  105.459  7.081 4
    1100908 2020  2.898636    80.415   5.15  110.987    3.4 4
    1100924 2000  42.56104    33.615  1.046  198.094  1.624 4
    end
    Thank you for any help.

  • #2
    estout is from SSC (FAQ Advice #12). If you want to append the regions, probably the easiest way is to manually delete the highlighted part and move \end{table} to the end.


    Code:
    \begin{table}[htbp]\centering
    
    \caption{Descriptive Statistics for Muncipalities\label{t-descstats-drought}}
    
    \begin{tabular}{l*{3}{c}}
    
    \toprule
    
    & 2000& 2010& 2020\\
    
    & mean/sd& mean/sd& mean/sd\\
    
    \midrule
    
    \emph{North Region}& & & \\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 205.38& 720.70& 1493.61\\
    
    & (249.49)& (910.75)& (1873.04)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 64.51& 3.41& 26.72\\
    
    & (15.27)& (2.14)& (14.24)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 5.76& 6.61& 12.83\\
    
    & (0.90)& (0.67)& (5.01)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 181.35& 344.99& 389.08\\
    
    & (75.98)& (133.45)& (155.54)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 8.35& 11.27& 8.69\\
    
    & (5.06)& (2.00)& (3.71)\\
    
    \bottomrule
    
    \end{tabular}
    
    \end{table}
    
    \begin{tabular}{l*{3}{c}}
    
    \toprule
    
    & 2000& 2010& 2020\\
    
    & mean/sd& mean/sd& mean/sd\\
    
    \midrule
    
    \emph{Central Region}& & & \\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 181.02& 477.50& 885.19\\
    
    & (.)& (.)& (.)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 18.16& 0.42& 11.76\\
    
    & (.)& (.)& (.)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 53.01& 64.47& 62.26\\
    
    & (.)& (.)& (.)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 259.62& 336.28& 356.89\\
    
    & (.)& (.)& (.)\\
    
    \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} \hspace{0.2cm} & 43.43& 75.70& 39.70\\
    
    & (.)& (.)& (.)\\
    
    \bottomrule
    
    \end{tabular}
    
    \end{table}
    Last edited by Andrew Musau; 24 Mar 2023, 06:29.

    Comment


    • #3
      Thanks @Andrew Musau! This will certainly work, but will require that I edit the table in Latex each time I recreate it. I wonder if there is another option. Maybe one that is like the 3-part table that is created here: https://medium.com/the-stata-guide/t...e-6e7ed5622856? I have tried this with the code below. I don’t get any errors in Stata, but do in Latex (no table).
      I am using estout from SSC in Stata 17.1. Here is the code that I tried:
      Code:
          
      // This approach is from: https://medium.com/the-stata-guide/the-stata-to-latex-guide-6e7ed5622856, which was written for regression results.    
      
      global variables gdp def_annMB milk_harvest cattle cows 
      
      ///// top table panel 
      estimates clear
      levelsof year, local(groups)
      local i 0
      foreach year of local groups{
          local ++i
          estpost tabstat $variables if year==`year' & studycode==1, by(year) c(stat) stat(mean sd) nototal
          eststo m`i'
      }
      esttab m* using $tables/t_des_study.tex, replace fragment   ///
      prehead(\begin{tabular}{l*{@M}{r}} \toprule ) label booktabs noobs nonotes collabels(none) eqlabels(none) nonumbers alignment(D{.}{.}{-1}) mtitles("2000" "2010" "2020")
      
          
      /////  center table panel
      estimates clear
      levelsof year, local(groups)
      local i 0
      foreach year of local groups{
          local ++i
          estpost tabstat $variables if year==`year' & studycode==2, by(year) c(stat) stat(mean sd) nototal
          eststo m`i'
      }
      esttab m* using $tables/t_des_study.tex, append fragment   ///
      prehead(\begin{tabular}{l*{@M}{r}} \toprule) label booktabs nodep nonum nomtitles nolines noobs nonotes collabels(none) alignment(D{.}{.}{-1}) eqlabels(none)
      
        
      ///// Bottom table panel. From https://medium.com/the-stata-guide/the-stata-to-latex-guide-6e7ed5622856 "The bottom panel is the most complicated one since here we replace coeflabel with varlabels which is a programming option that can overwrite a lot of the internal estout code (so use it carefully)."
      
      estimates clear
      levelsof year, local(groups)
      local i 0
      foreach year of local groups{
          local ++i
          estpost tabstat $variables if year==`year' & studycode==3, by(year) c(stat) stat(mean sd) nototal
          eststo m`i'
      }
      esttab m* using $tables/t_des_study.tex, append fragment postfoot(\bottomrule \end{tabular})  ///
       varlabels(cows "number of head", elist(`x`i'' \bottomrule)) label booktabs collabels(none) nomtitles nolines nonum alignment(D{.}{.}{-1}) sfmt(%6.0fc) eqlabels(none)

      Comment


      • #4
        I have not studied the linked code but it certainly would be possible to write code that automates the deletion of the lines that I have highlighted using e.g. import delimited. See

        Code:
        help import delimited
        But the problem is not interesting for me to explore, so I will leave it to others to do so or suggest alternative solutions.
        Last edited by Andrew Musau; 26 Mar 2023, 16:04.

        Comment


        • #5
          Thanks @Andrew Musau.I'll give it a try and report back here if I figure it out.

          Comment

          Working...
          X