Announcement

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

  • Generating a panel summary statistics table from Stata to LaTex

    Hello,

    I would like to know how to generate a panel summary statistics table from Stata to LaTex. I have this code in Stata for the summary statistics: xtsum lwage educ black hisp exper expersq married union
    I want to know the code that's going to automatically generate a LaTex table through Stata.

    Thanks


  • #2
    Hi Adib,

    This might be what you're looking for

    Code:
    *Run a Regression on the variables you want summary statistics for
    *The regression results don't actually matter, just run it so that you can use outreg2 to pull summary stats
    reg lwage educ black hisp exper expersq married union
    
    *Run outreg2 *Specify "sum" to get summary statistics.
    outreg2 sum using myfile, tex replace

    Comment


    • #3
      Cross-posted in two places:

      http://stackoverflow.com/questions/2...stata-to-latex

      http://stats.stackexchange.com/quest...stata-to-latex

      Please see the FAQ Advice -- which you were asked to read before posting -- on our cross-posting policy, which is that you should tell us about it.


      Comment


      • #4
        Those links don't work anymore, unfortunately. I have been looking for a long time now, including Statalist, but am unable to find a solution for exporting -xtsum- output into LaTeX. The only solution that I could find is Martin Weiss' solution here. Is there a simpler way?

        Thanks,
        Mihir

        Comment


        • #5
          I've got a program that might help. It's still in beta (that is, it's not on SSC and probably has plenty of bugs and missing features) but I'll post it below in case it's what you are looking for.

          I've used some combination of SSC packages like -sutex- and -texsave- to make summary datasets in LaTeX (like xtsum), but as you've encountered, you need to be able to access the saved results from each variable in the -xtsum- command separately. Therefore, this program goes through each variable in a loop and then appends the saved results to a temp dta file which then can be exported to LaTeX (if you have -texsave- installed) or can save/replace the data in memory (from which you can export into any format you'd like, eg excel).

          So, the program's existing options for exporting the results are to save the file as a stata dataset ("dta", you dont want this) or as "tex" file.
          The table this program creates with code like:
          Code:
          xtsumout person day calories test* using test2.tex, tex     replace ///
              title(latex title here) varlabels align(llCCCCr)
          is below/attached:
          Click image for larger version

Name:	Screen Shot 2017-12-22 at 7.08.52 AM.png
Views:	1
Size:	131.4 KB
ID:	1423422

          Other options include:
          If you specify "frag" then only the table will be created, not an entire TeX document (which is useful if you want to \input or \include this table in an existing TeX document). You can also specify the formats for the min and max vars and whether to use variable labels or not. You can add other options relevant to -texsave- and they will pass through for producing the TeX table (in a sense this is just a wrapper for -texsave-).

          This program needs some polishing and I'll update here whenever I eventually get around to putting it up on SSC or my website.

          Hope this helps:

          Code:
          sysuse xtline1, clear
          xtset person day
          
          **sutex output:
          su *
           sutex, labels file(test.tex)   replace  minmax
          
           
           
           
          cap program drop xtsumout
          program def xtsumout, rclass
          syntax varlist [if] using/  [, FRAG TEX DTA VARLABels   REPLACE FORMAT(str asis)   *]
          //need better error checking here:
           if `"`frag'"' == "" & `"`tex'"' == "" loc type "dta"
           loc i = 1
           tempfile out
           foreach j in `varlist' {
              qui tempfile a
              *di `"`j'"'
              xtsum  `j' `if'
              **create stata resultsset
              *get label
              loc labb ""
              loc labb `"`:var lab `j''"'
              preserve
              clear
              qui set obs 3
              if `"`varlabels'"' == "" qui g Variable = `"`j'"' in 1
              if `"`varlabels'"' != ""  & `"`labb'"' != "" qui g Variable = `"`labb'"' in 1
              if `"`varlabels'"' != ""  & `"`labb'"' == "" {
                  qui g Variable = `"`j'"' in 1
                  di as error `"Warning: variable label for `j' missing but {opt varlabels} option specified"'
                  }
              qui g Panel = "Overall" in 1
              qui replace  Panel = "Between" in 2
              qui replace  Panel = "Within" in 3
              qui g Mean = `"`r(mean)'"' in 1
              foreach m in sd min max {
              qui g `m' = `"`r(`m')'"' in 1
              qui replace  `m' = `"`r(`m'_b)'"' in 2
              qui replace  `m' = `"`r(`m'_w)'"' in 3
              }
              qui g Observations = `"N = `r(N)'"' in 1
              qui replace  Observations = `"n = `r(n)'"' in 2
              qui replace  Observations = `"T = `r(Tbar)'"' in 3
              //labels
              foreach l of varlist * {
                  cap lab var `l' `"`=proper(`"`l'"')'"'
                  }
              if `"`i'"' == "1" qui sa `out',  replace
              if `"`i'"' != "1" {
              qui sa `a',  replace
               qui u `out', clear
               qui append using `a'
               qui sa `out',  replace
               }
              restore
              
              loc i `++i'
              }
              **export stata version
              if !mi(`"`dta'"') {
              preserve
              qui u `out', clear
              qui sa `using', `replace'
              restore
              }
              **export to TEX**
              if !mi(`"`tex'"') {
              cap which texsave
              if _rc {
              noi di `"Please {stata ssc install texsave, replace :install texsave} to run this program"'
              exit
              }
              preserve
              qui u `out', clear
              **format numeric/calculated vars?  could add Min and Max below**  
              qui cap destring, replace
              if mi(`"`format'"')  format Mean sd %3.2f
              if !mi(`"`format'"') format Mean sd `format'
              qui texsave * using `using', `replace' `varlabels' `frag' `options'
              **note: use other texsave options here
              restore
              
              }
          end
          
          
          
          **examples for how to run this::
          
          sysuse xtline1, clear
          xtset person day
          xtsum person day
          
          
          xtsumout person day using test.dta, dta   replace
          xtsumout person day using test1.tex, tex frag   replace
          for N in numlist 1/3: g testN = runiform()   //just to help test the table
          xtsumout person day calories test* using test2.tex, tex     replace ///
              title(latex title here \vspace{1cm}) varlabels align(llCCCCr)  nofix format(%4.2f)
          Last edited by eric_a_booth; 22 Dec 2017, 06:31.
          Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

          Comment


          • #6
            Many thanks, Eric!

            Comment

            Working...
            X