Announcement

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

  • Export -table- from Stata to .tex, .docx or .csv

    I have used both outreg2 and esttab for regression output.
    However, with respect to the -table- command (which does not produce a regression), it is unclear to me how to best export this table. I have seen examples of putdocx, but there seems to not be a unique good solution to LaTeX export. In the past I have worked with esttab for summary statistics, but would like to make use of -table- (Stata17 (Stata 18 is not yet available to me)).

    HTML Code:
    sysuse auto, clear 
    table (var) (foreign), stat(mean price mpg rep78) stat(sd price mpg rep78)
    Is an example for a table I would like to export, flexibly, to .tex, .csv and .docx


  • #2
    After you create your table as shown, use the -collect export- command. See -help collect export- for the details of how to specifically target each of those three file formats.

    Comment


    • #3
      Hi...

      I personally prefer putdocx, but "collect export" should also work. Here, a short example of both variants.
      Maybe it is of any use.

      Code:
      clear all
      
      sysuse auto, clear
      table (var) (foreign), stat(mean price mpg rep78) stat(sd price mpg rep78)
      
      
      putdocx begin, header(header)                    ///
                     footer(footer)                        ///
                     font(courir, 9)                        ///
                     margin(left,2cm) margin(right,1cm)                
      
      * header
      putdocx paragraph, toheader(header) halign(left)
      putdocx text ("header1"), font("", "9", "") linebreak
      putdocx text ("header2"), font("", "9", "")
      
      * footer
      putdocx paragraph, tofooter(footer) halign(left)
      putdocx text ("footer"), font("", "9", "")
      
      putdocx collect, name(Table)                      
      
      * 1) putdocx
      putdocx save example, replace
      
      * 2) collect export
      collect export myfile.docx, replace
      collect export myfile.xlsx, replace
      collect export myfile.tex, replace

      Comment


      • #4
        Hi Simon and Clyde. #1 works, thanks for #2.

        A followup - Is it possible to somehow add a title to each table for .tex? If I create a table for each subsample, I want to add a table title. The documentation on -table- has no such info.

        Comment


        • #5
          Hi Castor,

          I am not using latex, but maybe this works for you. You could either use "collect title" (#1). Or you add a extra row (#2) in word.

          Code:
          clear all
          
          sysuse auto, clear
          table (var) (foreign), stat(mean price mpg rep78) stat(sd price mpg rep78)
          
          * #1
                  collect title "Text 1"
          
          putdocx begin, header(header)             ///
                         footer(footer)                        ///
                         font(courir, 9)                       ///
                         margin(left,2cm) margin(right,1cm)                
          
          * header
          putdocx paragraph, toheader(header) halign(left)
          putdocx text ("header1"), font("", "9", "") linebreak
          putdocx text ("header2"), font("", "9", "")
          
          * footer
          putdocx paragraph, tofooter(footer) halign(left)
          putdocx text ("footer"), font("", "9", "")
          
          putdocx collect, name(Table)                      
          
          * #2
                      putdocx table tbl1(1,.), addrows(1, before) /// /
                                               border(left,nil)                ///    
                                               border(top,nil)                ///
                                               border(right,nil) nosplit
                      
                  
                      putdocx table tbl1(1,1)= ("Text 2"),     ///
                      colspan(6) halign(left)                       ///
                      font("courir", "9", "")
          
          * 1) putdocx
          putdocx save example, replace
          
          * 2) collect export
          collect export myfile.docx, replace
          collect export myfile.xlsx, replace
          collect export myfile.tex, replace

          Comment

          Working...
          X