Announcement

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

  • append tables created with esttab for Latex

    I am hoping that someone has advice on how to append tables created with esttab for Latex. The code that I have right now can be used to create two separate tables one with the descriptive stats and one with the number of observations. What I would like to do is append these two tables (see below) so that the number of observations is included as the bottom row of the descriptive stats table (and the number of observations only shows up once).
    Click image for larger version

Name:	des stats.png
Views:	1
Size:	49.7 KB
ID:	1699245


    This is the code I am using (when I add the fragment option the file will not compile in Latex):
    /// set the variables
    global variables elecyear2_dum elecyear2 incometot inc_cattle inc_crops incoff inc_gov durables_elec origin_south yearmove good_soil distopo settime

    /// number of observations set equal to treatment.
    gen num_obs=elecyear2_dum
    label var num_obs "Obs."

    /// begin table 1: des stats for treatment, outcomes, controls
    est clear
    estpost tabstat $variables, by(year) c(stat) stat(mean sd) nototal
    esttab using "$tables/t_descstats_general.tex", replace ///
    cells(mean(fmt(2)) sd(par)) nostar nonumber unstack ///
    nomtitle nonote noobs label booktabs ///
    refcat(elecyear2_dum "\emph{Treatment}" incometot " \emph{Outcomes}" origin_south " \emph{Controls}", nolabel) ///
    eqlabels("1996" "2000" "2005" "2009") title(Descriptive Statistics\label{t-descstats-general})
    bysort year: eststo: qui estpost summarize num_obs, listwise

    /// begin table 2: number of observations by year
    esttab using "$tables/t_descstats_general2.tex", replace cells("count") nodepvar label nonum collab(none) nonumber nomtitle noobs booktabs title(Descriptive Statistics2\label{t-descstats-general2})

    And finally, a sample of the data is here:

    year yearmove incoff distopo incometot durables_elec good_soil elecyear2 elecyear2_dum origin_south inc_gov inc_crops inc_cattle settime
    1996 1991 0 42.55465 4284.35 2 1 1992 1 0 0 4284.35 0 1970
    2000 1990 5976 42.55465 11266.71 0 1 1992 1 1 1812 140 1526.715 1970
    2005 2001 1891.018 39.86766 3536.414 0 0 2008 0 1 419.9328 805.53 0 1970
    1996 1994 0 27.8091 1427.728 0 0 1994 1 1 0 1251.35 176.3782 1970
    2000 1990 0 27.8091 4551.696 2 0 1994 1 1 0 0 4551.696 1970
    2005 2004 0 27.8091 11769.94 6 0 1994 1 1 0 0 5167.221 1970
    2009 2003 0 27.8091 34497.43 9 0 1994 1 1 0 0 34497.43 1970
    1996 1993 0 50.87988 42813.14 0 0 1998 0 1 0 37169.04 5644.103 1970
    2000 1981 0 50.87988 6532.028 13 0 1998 1 0 3624 0 2908.028 1970
    2005 1981 0 50.87988 16951.16 2 0 1998 1 0 4252.15 0 8374.227 1970
    2009 1981 0 50.87988 15918.97 6 0 1998 1 0 6165.746 0 2758.751 1970


    Thanks for any help.
    Attached Files

  • #2
    To clarify, I do try and append these with the append option for the second table (as below), but this does not work:

    /// begin table 2: number of observations by year
    esttab using "$tables/t_descstats_general.tex", append cells("count") nodepvar label nonum collab(none) nonumber nomtitle noobs booktabs title(Descriptive Statistics2\label{t-descstats-general})

    Comment


    • #3
      Please redo your dataex example such that it appears in the code delimiters. Also, please, give a reproducible example that illustrates the problem


      EDIT: Oh, and please no screenshots. They don't help us help you. An effective reproducible example along with a dataset to illustrate the problem is worth your weight in pure gold. Screenshots, not so much.
      Last edited by Jared Greathouse; 28 Jan 2023, 11:32.

      Comment


      • #4
        Thanks Jared Greathouse. Here's the code that does not work. There are no errors according to Stata, it's just that the exported txt file does not compile in Latex.
        Code:
        global variables elecyear2_dum elecyear2 incometot inc_cattle inc_crops incoff inc_gov durables_elec  origin_south yearmove good_soil  distopo settime
        gen num_obs=elecyear2_dum
        label var num_obs "Obs."  
        
        est clear
        estpost tabstat $variables, by(year)  c(stat) stat(mean sd) nototal
        esttab using "$tables/t_descstats_general.tex", replace ///
            cells(mean(fmt(2)) sd(par)) nostar  nonumber unstack ///
            nomtitle nonote noobs label  booktabs f ///
            refcat(elecyear2_dum "\emph{Treatment}" incometot " \emph{Outcomes}" origin_south " \emph{Controls}", nolabel) ///
            eqlabels("1996" "2000" "2005" "2009")  title(Descriptive Statistics\label{t-descstats-general}) 
        
        bysort year: eststo: qui estpost summarize num_obs, listwise
        esttab using "$tables/t_descstats_general2.tex", append cells("count") nodepvar label nonum collab(none) nonumber nomtitle noobs booktabs f
        and here are the data:
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(year yearmove) double incoff float(distopo incometot durables_elec good_soil elecyear2 elecyear2_dum origin_south inc_gov inc_crops inc_cattle settime num_obs)
        1996 1984                  0  25.93404  22514.72 1 1      1997 0 1 3333.12  12985.62  3018.027 1970 0
        1996 1971                  0  9.495729  9700.016 2 1      1995 0 1 3333.12   4955.87 1411.0258 1970 0
        1996 1987  446.0199890136719 27.530115 3008.3726 0 0      1997 0 1       0   1288.51 1273.8425 1970 0
        1996 1993 1338.0799560546875  79.76019   1338.08 2 0 1996.4475 0 1       0         0         0 1992 0
        1996 1984 1665.1700439453125  70.69954  4527.211 0 0      1994 1 1       0   2199.15  367.4012 1992 1
        1996 1992                  0 31.878616  4297.816 0 1      1997 0 1       0    1796.5 1411.0258 1970 0
        1996 1984                  0 37.691948  3461.506 0 1      1996 0 1       0   2050.48 1411.0258 1970 0
        1996 1988                  0         .  3948.575 2 . 1991.0364 1 1       0    185.84  3762.735 1970 1
        1996 1976                  0  18.26473 20192.496 2 1  1989.207 1 0 3333.12   6938.22  6036.055 1970 1
        1996 1994                  0   77.2119   2100.19 0 0 1997.4275 0 1 1666.56    433.63         0 1992 0
        1996 1982 1115.0699462890625  40.20391 4681.8296 2 1 1995.9468 0 1       0         0   3566.76 1970 0
        1996 1987    13232.169921875  43.61212 18208.242 0 0      1997 0 0       0 4348.9497  627.1225 1981 0
        1996 1993 1665.1700439453125         .   4651.07 1 .      2002 0 1       0   1499.14         0    . 0
        1996 1976  3568.219970703125 23.041565  7095.784 0 1      1997 0 1       0         0 3527.5645 1970 0
        1996 1985                  0 36.130985  2730.329 0 0  1999.642 0 1 1666.56 569.91003   493.859 1981 0
        1996 1985                  0   11.1549 10873.615 8 0      1993 1 1 1666.56     740.9  8466.154 1970 1
        1996 1983                  0  57.27206  6443.283 0 0 1997.8068 0 1       0 4608.9497 1834.3336 1992 0
        1996 1977                  0  17.81378 10725.455 4 1 1994.3994 1 1 1666.56   2787.67  6271.225 1970 1
        1996 1984                  0 10.982212  6445.541 1 1 1995.4594 0 0 1666.56   1734.55 1371.8306 1970 0
        1996 1979                  0 12.953523 13066.642 5 0      1995 0 1 3333.12  9106.399  627.1225 1970 0
        1996 1994                  0  80.46941         0 0 0 1995.3597 0 1       0         0         0 1992 0
        1996 1971                  0  25.65318  9865.078 0 0      1992 1 1 3333.12    5637.3  823.0984 1970 1
        1996 1992 1665.1700439453125  67.59905 4487.2217 1 0  1996.793 0 0       0         0 2822.0515 1992 0
        1996 1977      21558.0390625  47.67817  21741.74 1 0      1995 0 1       0         0  183.7006 1970 0
        1996 1994                  0   27.8091 1427.7283 0 0      1994 1 1       0   1251.35 176.37823 1970 1
        1996 1990                  0  41.36072  4290.714 0 0 1997.1013 0 1 1666.56     19.69  940.6838 1981 0
        1996 1986 1665.1700439453125  60.49899   4428.06 0 0      1994 1 1       0   1090.29         0 1992 1
        1996 1986                  0   58.0178 3356.6895 0 0      2000 0 1 1666.56    631.86 1058.2693 1992 0
        1996 1993                  0  50.87988  42813.14 0 0      1998 0 1       0  37169.04  5644.103 1970 0
        1996 1974                  0  40.79566 1646.1968 0 1      1997 0 1       0         0 1646.1968 1970 0
        1996 1983                  0 23.506197 13274.857 0 0      2005 0 1 3333.12   5104.54  3174.808 1970 0
        1996 1991                  0  42.55465   4284.35 2 1      1992 1 0       0   4284.35         0 1970 1
        1996 1980                  0  44.53066  4442.978 1 0      1995 0 0       0   1245.16  2038.148 1970 0
        1996 1993                  0  71.79128  768.2251 0 0 1997.1177 0 1       0         0  768.2251 1992 0
        1996 1995     8028.509765625 74.257645   8028.51 0 0      1997 0 1       0         0         0 1992 0
        1996 1987                  0 27.672495  8698.596 1 1      1996 0 1       0   5711.63 2057.7458 1970 0
        1996 1978                  0  45.12514  6531.701 1 1      1995 0 0       0   2651.38  3880.321 1970 0
        1996 1978   2973.52001953125  45.33876  5787.929 1 1      1995 0 1       0    619.48  2194.929 1970 0
        1996 1984  713.6400146484375    37.278 1733.2362 0 0      1997 0 1       0    745.23 274.36612 1981 0
        1996 1974                  0 17.554424  8349.014 1 0 1995.5745 0 1 1666.56    724.79  5957.664 1970 0
        1996 1981                  0  44.59882 2311.6445 0 0      2001 0 1       0 1214.1799 1097.4645 1981 0
        1996 1982                  0  29.18524  3519.331 3 0      1998 0 1 1666.56         0  367.4012 1970 0
        1996 1989                  0  15.20471 2407.6006 2 1      2001 0 1       0   1035.77 1371.8306 1970 0
        1996 1977                  0  26.09073  10404.63 1 0 1996.2023 0 1 1666.56   3041.66  3950.872 1970 0
        1996 1980                  0 23.657415  12999.24 1 1 1993.0214 1 1       0  11760.28         0 1970 1
        1996 1986                  0  68.82019  9135.471 1 0      1997 0 0       0   5234.63 1224.6705 1992 0
        1996 1987                  0  77.11226    501.78 0 0 1997.3745 0 0       0    501.78         0 1992 0
        1996 1977                  0  39.46072  7580.378 0 1      1996 0 1       0   5463.84 2116.5386 1970 0
        1996 1995                  0  59.78569 3390.7314 0 0      1999 0 1       0    568.68 2822.0515 1992 0
        1996 1984                  0  30.81992  9295.334 2 0      2007 0 1 1666.56   4336.38 3292.3936 1970 0
        1996 1994                  0   73.5876    148.67 0 0 1996.1724 0 1       0    148.67         0 1992 0
        1996 1978                  0 32.423714 1883.2046 0 0  1999.115 0 1       0  785.7401 1097.4645 1970 0
        1996 1994  743.3800048828125  35.40706 4055.3706 1 0      1990 1 1       0         0  3311.991 1970 1
        1996 1980                  0  58.21935 4479.2114 1 0 1993.4774 1 1 3333.12    778.69  367.4012 1992 1
        1996 1976                  0 26.908785 22860.574 3 1      1997 0 1       0  14080.86  8779.715 1970 0
        1996 1987                  0  48.88761  8258.197 0 0 2003.0608 0 1 3333.12   3803.62  752.5471 1981 0
        1996 1995                  0  51.90626  764.3055 0 0      2001 0 1       0         0  764.3055 1981 0
        1996 1979                  0  27.14353    3952.6 1 0      2006 0 1 3333.12    619.48         0 1970 0
        1996 1971                  0  5.754002  5542.098 1 0      1984 1 1       0   1152.24 4389.8574 1970 1
        1996 1988                  0  63.10547  912.8939 0 0      1999 0 1       0    701.24 211.65387 1992 0
        1996 1987                  0  45.16247  3768.503 1 0      1999 0 0 1666.56    881.51  627.1225 1981 0
        1996 1984                  0  47.64696 2118.1519 0 0      1997 0 1       0   1647.81  470.3419 1981 0
        1996 1992                  0 27.756475  8963.279 2 0      1996 0 1 1666.56    241.59  7055.129 1970 0
        1996 1985                  0  43.30359  7139.251 0 0      2005 0 0 1666.56   4993.03  183.7006 1981 0
        1996 1984 1665.1700439453125  52.27645  7377.316 2 0      1994 1 0       0   4144.34 1567.8063 1992 1
        1996 1984                  0  46.82588  7055.129 0 0 1997.4443 0 0       0         0  7055.129 1970 0
        1996 1976                  0 31.535046  6271.225 0 1 1996.3667 0 1       0         0  6271.225 1970 0
        1996 1994                  0  48.77951 2116.5386 2 0  1996.438 0 1       0         0 2116.5386 1970 0
        1996 1980                  0 35.792274  5259.171 0 0 1996.6262 0 1       0   4708.07 551.10175 1970 0
        1996 1977                  0  33.75587  5650.344 4 1      2006 0 1       0   3425.74 1834.3336 1996 0
        1996 1984                  0  39.65242 2350.8662 0 0 2000.3846 0 0       0    2076.5 274.36612 1981 0
        1996 1983                  0 11.391752 10728.127 4 0      1989 1 1 3333.12   5748.81 1646.1968 1970 1
        1996 1991     59767.80859375   31.8272  67998.79 0 0   2000.83 0 1       0         0  8230.983 1981 0
        1996 1994 1338.0799560546875         .   1338.08 0 .      2000 0 1       0         0         0    . 0
        1996 1984                  0 36.654068  1019.074 0 0      2000 0 0       0         0  1019.074 1981 0
        1996 1975                  0 26.788977  11463.51 2 0      1997 0 1       0   8719.85  2743.661 1970 0
        1996 1976                  0  15.46688  7291.199 9 0      1989 1 1       0     79.29  7211.909 1970 1
        1996 1976                  0  10.49281 12446.395 1 1      1996 0 1       0   10667.5 1097.4645 1970 0
        1996 1991  1486.760009765625 15.168434   1486.76 2 0  1993.876 1 1       0         0         0 1970 1
        1996 1970                  0  17.17132  5167.454 0 1      1994 1 1 3333.12         0 1834.3336 1970 1
        1996 1984      4341.33984375  75.04902 17515.373 0 0 1996.0653 0 1       0  12439.23  734.8024 1992 0
        1996 1993  1486.760009765625 19.073135   3271.72 0 0      1988 1 1       0    569.91 1215.0499 1970 1
        1996 1982                  0  43.05307  7755.003 0 0      1996 0 1       0   6814.32  940.6838 1981 0
        1996 1988                  0  39.37064    724.79 0 1 1995.1013 0 1       0    724.79         0 1970 0
        1996 1971                381 20.463886 13735.912 3 0      1980 1 0 3333.12   3809.82 1763.7822 1970 1
        1996 1989                  0  21.52888  7326.847 1 0      1994 1 0       0   5680.65 1646.1968 1970 1
        1996 1995                  0 20.866604  7588.183 2 0      1995 0 1       0         0  7588.183 1970 0
        1996 1994                  0  40.54411 1685.9645 0 1      1998 0 1       0     588.5 1097.4645 1970 0
        1996 1985                  0  11.60094  5020.223 2 1      2001 0 1 1666.56    218.05 3135.6125 1970 0
        1996 1989                  0  19.08276   2508.49 0 1 1994.1514 1 1       0         0   2508.49 1970 1
        1996 1990                  0  35.78536   2748.79 0 0 1999.7672 0 1 1666.56   1082.23         0 1981 0
        1996 1976                  0 35.547474   11990.4 0 1      1998 0 1       0   9168.35 2822.0515 1970 0
        1996 1992                  0  6.021822  5758.535 2 0      1995 0 1 1666.56         0  4091.975 1970 0
        1996 1989    19506.310546875 20.370676  19506.31 1 0 1995.7822 0 0       0         0         0 1970 0
        1996 1993            4014.25   82.1297   4213.72 1 0      2002 0 1       0    199.47         0 1992 0
        1996 1973  743.3800048828125  27.08323 4594.8867 2 0      1989 1 0       0    911.87  2939.637 1970 1
        1996 1973                  0 28.481186 10240.834 3 0      1999 0 1 3333.12   2775.28  2645.673 1970 0
        1996 1978  743.3800048828125 29.208574  3551.681 0 0      2000 0 0       0   1115.07  1693.231 1970 0
        1996 1981                  0   65.6518  2641.618 0 0      1995 0 1       0   2335.45  306.1676 1992 0
        1996 1986                  0 31.605764  6460.551 0 0      1997 0 0       0   3716.89  2743.661 1981 0
        end

        Comment


        • #5
          • Ugh, actully there's a typo in the above, the table in the second should be the same
            Code:
            global variables elecyear2_dum elecyear2 incometot inc_cattle inc_crops incoff inc_gov durables_elec  origin_south yearmove good_soil  distopo settime
            	gen num_obs=elecyear2_dum
            	label var num_obs "Obs."  
            	
            	est clear
            	estpost tabstat $variables, by(year)  c(stat) stat(mean sd) nototal
            	esttab using "$tables/t_descstats_general.tex", replace ///
            	    cells(mean(fmt(2)) sd(par)) nostar  nonumber unstack ///
            	    nomtitle nonote noobs label  booktabs f ///
            	    refcat(elecyear2_dum "\emph{Treatment}" incometot " \emph{Outcomes}" origin_south " \emph{Controls}", nolabel) ///
            	    eqlabels("1996" "2000" "2005" "2009")  title(Descriptive Statistics\label{t-descstats-general}) 
            	
            	bysort year: eststo: qui estpost summarize num_obs, listwise
            	esttab using "$tables/t_descstats_general.tex", append cells("count") nodepvar label nonum collab(none) nonumber nomtitle noobs booktabs f

          Comment


          • #6
            estout is from SSC, as you are asked to explain (FAQ Advice #12). Why not include the number of observations as a statistic, as is usually done?

            Code:
            esttab using "$tables/t_descstats_general.tex", replace stats(N) ///
            cells(mean(fmt(2)) sd(par)) nostar nonumber unstack ///
            nomtitle nonote noobs label booktabs ///
            refcat(elecyear2_dum "\emph{Treatment}" incometot " \emph{Outcomes}" origin_south " \emph{Controls}", nolabel) ///
            eqlabels("1996" "2000" "2005" "2009") title(Descriptive Statistics\label{t-descstats-general})
            bysort year: eststo: qui estpost summarize num_obs, listwi

            Comment


            • #7
              @Andrew Musau Thank you for your response. The problem with adding stats(N) is that this calculates the sample across the 4 years. What I need is the sample by year. The bysort year: that follows does create the obs. in the correct format but I can't get that table to append to the other.

              Comment


              • #8
                Code:
                estimates clear
                levelsof year, local(years)
                local i 0
                foreach year of local years{
                    local ++i
                    estpost tabstat $variables if year==`year', by(year) c(stat) stat(mean sd) nototal
                    eststo m`i'
                }
                
                esttab m* using "$tables/t_descstats_general.tex", replace stats(N) ///
                    cells(mean(fmt(2)) sd(par)) nostar  nonumber unstack ///
                    nomtitle nonote noobs label  booktabs f ///
                    refcat(elecyear2_dum "\emph{Treatment}" incometot " \emph{Outcomes}" origin_south " \emph{Controls}", nolabel) ///
                    eqlabels("1996" "2000" "2005" "2009")  title(Descriptive Statistics\label{t-descstats-general})

                Comment


                • #9
                  Thank you Andrew Musau! Works like a charm!

                  Comment

                  Working...
                  X