Announcement

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

  • Export table to latex using tabstat

    Dear Statalists, I am again in need of you.

    I have a series of observations for some variables for four categories. The following is a sample of my data

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long category float(date assets_s liabilities_s networth_s)
    1 176  6.8 30.3  2.4
    1 177  6.9   30  2.5
    1 178  6.9   30  2.5
    1 179  6.9 30.4  2.4
    1 180  6.9 31.1  2.3
    1 181  6.9 31.5  2.2
    1 182    7 32.1  2.2
    1 183  7.1 32.5  2.3
    1 184  7.1 32.7  2.2
    1 185  7.3 33.2  2.2
    1 186  7.4 33.7  2.1
    1 187  7.4   34  2.2
    1 188  7.5 34.2  2.2
    1 189  7.5 34.5  2.1
    1 190  7.5 35.1  1.9
    1 191  7.5 35.3  1.7
    1 192  7.7 35.4  1.7
    2 176 36.6 46.7 34.8
    2 177 36.6 46.7 34.6
    2 178 36.2 46.9 34.2
    2 179 36.1 46.3 34.2
    2 180 35.9 45.9   34
    2 181 35.7 45.4 33.8
    2 182 35.5 44.9 33.7
    2 183 35.4 44.5 33.7
    2 184 34.5   44 32.7
    2 185 34.4 43.5 32.6
    2 186 34.3   43 32.5
    2 187 34.1 42.6 32.4
    2 188 33.3 42.3 31.5
    2 189 32.9 41.9 31.1
    2 190 32.8 41.5   31
    2 191 32.9 41.3 31.2
    2 192 32.7   41 30.9
    3 176 32.6 16.9 35.5
    3 177 32.7 17.1 35.7
    3 178 32.8 17.1 35.8
    3 179 32.7 17.5 35.6
    3 180 32.7 17.4 35.6
    3 181 32.7 17.5 35.6
    3 182 32.7 17.6 35.5
    3 183 32.6 17.8 35.5
    3 184   33 18.2 35.8
    3 185 33.2 18.3 36.2
    3 186 33.3 18.4 36.3
    3 187 33.3 18.6 36.3
    3 188 33.8 18.8 36.8
    3 189 34.2 19.1 37.3
    3 190 34.5   19 37.6
    3 191 34.6   19 37.9
    3 192 34.9 19.1 38.4
    4 176   24  6.1 27.3
    4 177 23.9  6.1 27.2
    4 178 24.1  6.1 27.5
    4 179 24.3  5.8 27.9
    4 180 24.5  5.7   28
    4 181 24.7  5.5 28.3
    4 182 24.8  5.4 28.5
    4 183 24.8  5.3 28.6
    4 184 25.3  5.1 29.2
    4 185 25.1  5.1   29
    4 186   25  4.9   29
    4 187 25.2  4.8 29.2
    4 188 25.4  4.7 29.5
    4 189 25.3  4.5 29.5
    4 190 25.3  4.4 29.5
    4 191 24.9  4.4 29.2
    4 192 24.7  4.4 29.1
    end
    format %tq date
    label values category category_temporary
    label def category_temporary 1 "Bottom50", modify
    label def category_temporary 2 "Next40", modify
    label def category_temporary 3 "Next9", modify
    label def category_temporary 4 "Top1", modify

    I am trying to summarize the mean values of these variables (in the data example I show only assets_s liabilities_s networth_s), and I use the following code, that does what I need:

    Code:
     estpost tabstat assets_s liabilities_s networth_s, by(category) stat(mean) nototal
    
    Summary statistics: Mean
         for variables: assets_s liabilities_s networth_s
      by categories of: category
    
        category | e(asset~)  e(liabi~)  e(netwo~) 
    -------------+---------------------------------
        Bottom50 |  7.194118   32.70588   2.182353 
          Next40 |      34.7   44.02353   32.87647 
           Next9 |  33.31176   18.08235   36.31765 
            Top1 |  24.78235   5.194118   28.61765
    I now need to export the outcome of the following table into latex, any suggestion?

    Many thanks,
    Alessandro

  • #2
    Thanks for the data example. You can use estout (SSC) which you have installed as you have used estpost above.

    Code:
    esttab using myfile.tex, cells("assets_s liabilities_s networth_s") nonumb noobs

    Comment


    • #3
      Great, many thanks Andrew. I have other two questions:

      1) Is there a way to export the table already settled to run in latex, i.e. without the need to writing \begin{document}, etc.?
      2) I would need the following table to be one (bottom50, next40, next9, top1) next to each other, rather than the following:

      Code:
         
      
       estpost tabstat assets_s liabilities_s networth_s, by(category) stat(mean) nototal columns(statistics)
      
      Summary statistics: Mean
           for variables: assets_s liabilities_s networth_s
        by categories of: category
      
      category |   e(Mean) 
      -------------+-----------
      Bottom50     |           
          assets_s |  6.957143 
      liabilitie~s |  33.26587 
        networth_s |  2.365079 
      -------------+-----------
      Next40       |           
          assets_s |  34.41429 
      liabilitie~s |  43.66905 
        networth_s |  32.83492 
      -------------+-----------
      Next9        |           
          assets_s |  33.90476 
      liabilitie~s |  18.18333 
        networth_s |  36.64603 
      -------------+-----------
      Top1         |           
          assets_s |  24.72937 
      liabilitie~s |   4.88254 
        networth_s |  28.15159

      Comment


      • #4
        1) Is there a way to export the table already settled to run in latex, i.e. without the need to writing \begin{document}, etc.?
        This is something that you would normally do in your text editor, but below is a way to do this in Stata. After running the code, you look for the file "mytable.tex" in your current directory and it should generate the table once you compile the file.

        2) I would need the following table to be one (bottom50, next40, next9, top1) next to each other, rather than the following:
        You then need to tabulate the categories separately.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long category float(date assets_s liabilities_s networth_s)
        1 176  6.8 30.3  2.4
        1 177  6.9   30  2.5
        1 178  6.9   30  2.5
        1 179  6.9 30.4  2.4
        1 180  6.9 31.1  2.3
        1 181  6.9 31.5  2.2
        1 182    7 32.1  2.2
        1 183  7.1 32.5  2.3
        1 184  7.1 32.7  2.2
        1 185  7.3 33.2  2.2
        1 186  7.4 33.7  2.1
        1 187  7.4   34  2.2
        1 188  7.5 34.2  2.2
        1 189  7.5 34.5  2.1
        1 190  7.5 35.1  1.9
        1 191  7.5 35.3  1.7
        1 192  7.7 35.4  1.7
        2 176 36.6 46.7 34.8
        2 177 36.6 46.7 34.6
        2 178 36.2 46.9 34.2
        2 179 36.1 46.3 34.2
        2 180 35.9 45.9   34
        2 181 35.7 45.4 33.8
        2 182 35.5 44.9 33.7
        2 183 35.4 44.5 33.7
        2 184 34.5   44 32.7
        2 185 34.4 43.5 32.6
        2 186 34.3   43 32.5
        2 187 34.1 42.6 32.4
        2 188 33.3 42.3 31.5
        2 189 32.9 41.9 31.1
        2 190 32.8 41.5   31
        2 191 32.9 41.3 31.2
        2 192 32.7   41 30.9
        3 176 32.6 16.9 35.5
        3 177 32.7 17.1 35.7
        3 178 32.8 17.1 35.8
        3 179 32.7 17.5 35.6
        3 180 32.7 17.4 35.6
        3 181 32.7 17.5 35.6
        3 182 32.7 17.6 35.5
        3 183 32.6 17.8 35.5
        3 184   33 18.2 35.8
        3 185 33.2 18.3 36.2
        3 186 33.3 18.4 36.3
        3 187 33.3 18.6 36.3
        3 188 33.8 18.8 36.8
        3 189 34.2 19.1 37.3
        3 190 34.5   19 37.6
        3 191 34.6   19 37.9
        3 192 34.9 19.1 38.4
        4 176   24  6.1 27.3
        4 177 23.9  6.1 27.2
        4 178 24.1  6.1 27.5
        4 179 24.3  5.8 27.9
        4 180 24.5  5.7   28
        4 181 24.7  5.5 28.3
        4 182 24.8  5.4 28.5
        4 183 24.8  5.3 28.6
        4 184 25.3  5.1 29.2
        4 185 25.1  5.1   29
        4 186   25  4.9   29
        4 187 25.2  4.8 29.2
        4 188 25.4  4.7 29.5
        4 189 25.3  4.5 29.5
        4 190 25.3  4.4 29.5
        4 191 24.9  4.4 29.2
        4 192 24.7  4.4 29.1
        end
        format %tq date
        label values category category_temporary
        label def category_temporary 1 "Bottom50", modify
        label def category_temporary 2 "Next40", modify
        label def category_temporary 3 "Next9", modify
        label def category_temporary 4 "Top1", modify
        
        eststo clear
        levelsof category, local(categories)
        foreach cat of local categories{
            eststo `:lab (category) `cat'': estpost tabstat assets_s liabilities_s networth_s if ///
            category==`cat', by(category) stat(mean) nototal columns(statistics)
        }
        esttab * using mytable.tex, replace cells("mean") nonumb noobs collab(none) eqlab(none) mtitles
        
        
        // backup table and open new file
        copy mytable.tex tmp.tex
        file open fh using mytable.tex, write replace
        // write top lines
        file write fh "\documentclass{article}" _n
        file write fh "\usepackage{multirow}" _n
        file write fh "\usepackage{amsmath}" _n
        file write fh "\usepackage{ulem}" _n
        file write fh "\usepackage[table]{xcolor}" _n
        file write fh "\begin{document}" _n
        file write fh "\begin{table}[ht]" _n
        file write fh "\centering" _n
        // now insert the table
        file open fh2 using tmp.tex, read
        file read fh2 line
        while r(eof)==0 {
            file write fh `"`line'"' _n
            file read fh2 line
        }
        file close fh2
        // write bottom lines
        file write fh _n "\end{table}" _n
        file write fh _n "\end{document}" _n
        // clean up
        file close fh
        erase tmp.tex
        Res.:

        Click image for larger version

Name:	Screenshot 2021-07-30 094650.png
Views:	1
Size:	43.9 KB
ID:	1621310


        Comment


        • #5
          Thanks Andrew. When I run your code I get a table without assets_s, liabilities_s, and networth_s, but only the first row. Any idea why?

          Comment


          • #6
            If your LaTeX file is exactly as follows after running the code in #4, then the problem is in the compilation.

            Code:
            \documentclass{article}
            \usepackage{multirow}
            \usepackage{amsmath}
            \usepackage{ulem}
            \usepackage[table]{xcolor}
            \begin{document}
            \begin{table}[ht]
            \centering
            {
            \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
            \begin{tabular}{l*{4}{c}}
            \hline\hline
                        &\multicolumn{1}{c}{Bottom50}&\multicolumn{1}{c}{Next40}&\multicolumn{1}{c}{Next9}&\multicolumn{1}{c}{Top1}\\
            \hline
            assets\_s    &    7.194118&        34.7&    33.31176&    24.78235\\
            liabilities\_s&    32.70588&    44.02353&    18.08235&    5.194118\\
            networth\_s  &    2.182353&    32.87647&    36.31765&    28.61765\\
            \hline\hline
            \end{tabular}
            }
            
            \end{table}
            
            \end{document}
            If not, update estout, rerun the code in a do file and see if it fixes it.

            Code:
            ssc install estout, replace

            Comment


            • #7
              I tried, but it does not work.. Thanks for the support anyway

              Comment


              • #8
                You need to present more details to push this forward. Start by copying and pasting the result you get from running the code below.

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input long category float(date assets_s liabilities_s networth_s)
                1 176  6.8 30.3  2.4
                1 177  6.9   30  2.5
                1 178  6.9   30  2.5
                1 179  6.9 30.4  2.4
                1 180  6.9 31.1  2.3
                1 181  6.9 31.5  2.2
                1 182    7 32.1  2.2
                1 183  7.1 32.5  2.3
                1 184  7.1 32.7  2.2
                1 185  7.3 33.2  2.2
                1 186  7.4 33.7  2.1
                1 187  7.4   34  2.2
                1 188  7.5 34.2  2.2
                1 189  7.5 34.5  2.1
                1 190  7.5 35.1  1.9
                1 191  7.5 35.3  1.7
                1 192  7.7 35.4  1.7
                2 176 36.6 46.7 34.8
                2 177 36.6 46.7 34.6
                2 178 36.2 46.9 34.2
                2 179 36.1 46.3 34.2
                2 180 35.9 45.9   34
                2 181 35.7 45.4 33.8
                2 182 35.5 44.9 33.7
                2 183 35.4 44.5 33.7
                2 184 34.5   44 32.7
                2 185 34.4 43.5 32.6
                2 186 34.3   43 32.5
                2 187 34.1 42.6 32.4
                2 188 33.3 42.3 31.5
                2 189 32.9 41.9 31.1
                2 190 32.8 41.5   31
                2 191 32.9 41.3 31.2
                2 192 32.7   41 30.9
                3 176 32.6 16.9 35.5
                3 177 32.7 17.1 35.7
                3 178 32.8 17.1 35.8
                3 179 32.7 17.5 35.6
                3 180 32.7 17.4 35.6
                3 181 32.7 17.5 35.6
                3 182 32.7 17.6 35.5
                3 183 32.6 17.8 35.5
                3 184   33 18.2 35.8
                3 185 33.2 18.3 36.2
                3 186 33.3 18.4 36.3
                3 187 33.3 18.6 36.3
                3 188 33.8 18.8 36.8
                3 189 34.2 19.1 37.3
                3 190 34.5   19 37.6
                3 191 34.6   19 37.9
                3 192 34.9 19.1 38.4
                4 176   24  6.1 27.3
                4 177 23.9  6.1 27.2
                4 178 24.1  6.1 27.5
                4 179 24.3  5.8 27.9
                4 180 24.5  5.7   28
                4 181 24.7  5.5 28.3
                4 182 24.8  5.4 28.5
                4 183 24.8  5.3 28.6
                4 184 25.3  5.1 29.2
                4 185 25.1  5.1   29
                4 186   25  4.9   29
                4 187 25.2  4.8 29.2
                4 188 25.4  4.7 29.5
                4 189 25.3  4.5 29.5
                4 190 25.3  4.4 29.5
                4 191 24.9  4.4 29.2
                4 192 24.7  4.4 29.1
                end
                format %tq date
                label values category category_temporary
                label def category_temporary 1 "Bottom50", modify
                label def category_temporary 2 "Next40", modify
                label def category_temporary 3 "Next9", modify
                label def category_temporary 4 "Top1", modify
                
                eststo clear
                levelsof category, local(categories)
                foreach cat of local categories{
                    eststo `:lab (category) `cat'': estpost tabstat assets_s liabilities_s networth_s if ///
                    category==`cat', by(category) stat(mean) nototal columns(statistics)
                }
                esttab *, replace cells("mean") nonumb noobs collab(none) eqlab(none) mtitles

                Comment


                • #9
                  Thank you!

                  I get the following:

                  Code:
                  Summary statistics: Mean
                       for variables: assets_s liabilities_s networth_s
                    by categories of: category
                  
                      category |   e(Mean) 
                  -------------+-----------
                  Bottom50     |           
                      assets_s |  7.194118 
                  liabilitie~s |  32.70588 
                    networth_s |  2.182353 
                  
                  Summary statistics: Mean
                       for variables: assets_s liabilities_s networth_s
                    by categories of: category
                  
                      category |   e(Mean) 
                  -------------+-----------
                  Next40       |           
                      assets_s |      34.7 
                  liabilitie~s |  44.02353 
                    networth_s |  32.87647 
                  
                  Summary statistics: Mean
                       for variables: assets_s liabilities_s networth_s
                    by categories of: category
                  
                      category |   e(Mean) 
                  -------------+-----------
                  Next9        |           
                      assets_s |  33.31176 
                  liabilitie~s |  18.08235 
                    networth_s |  36.31765 
                  
                  Summary statistics: Mean
                       for variables: assets_s liabilities_s networth_s
                    by categories of: category
                  
                      category |   e(Mean) 
                  -------------+-----------
                  Top1         |           
                      assets_s |  24.78235 
                  liabilitie~s |  5.194118 
                    networth_s |  28.61765 
                  
                  . esttab *, replace cells("mean") nonumb noobs collab(none) eqlab(none) mtitle
                  > s
                  
                  ----------------------------------------------------------------
                                   Bottom50       Next40        Next9         Top1
                  ----------------------------------------------------------------
                  ----------------------------------------------------------------
                  
                  . 
                  end of do-file

                  Comment


                  • #10
                    It is clear where your problem comes from. Your statistics are stored as e(Mean) instead of e(mean). This is strange as you suggest that you have the latest version of estout in #7. Can you show the result of

                    Code:
                    which estout
                    As of today, the latest version from SSC is

                    *! version 3.24 30apr2021 Ben Jann
                    With your current settings, replacing

                    esttab *, replace cells("mean") nonumb noobs collab(none) eqlab(none) mtitles
                    with

                    Code:
                    esttab *, replace cells("Mean") nonumb noobs collab(none) eqlab(none) mtitles
                    in #4 should fix the issue.

                    Comment


                    • #11
                      I have indeed the latest version:
                      *! version 3.24 30apr2021 Ben Jann
                      But you're right, with cells("Mean") I solve the issue, thanks a lot!

                      Comment


                      • #12
                        Originally posted by Andrew Musau View Post
                        It is clear where your problem comes from. Your statistics are stored as e(Mean) instead of e(mean). This is strange as you suggest that you have the latest version of estout in #7. Can you show the result of

                        Code:
                        which estout
                        As of today, the latest version from SSC is



                        With your current settings, replacing



                        with

                        Code:
                        esttab *, replace cells("Mean") nonumb noobs collab(none) eqlab(none) mtitles
                        in #4 should fix the issue.
                        Thanks Andrew Musau, it helped me a lot

                        Comment

                        Working...
                        X