Announcement

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

  • Help creating a nicely formatted table

    Hi everyone,

    I'm trying to make a nicely labeled, comprehensive table that outputs from Stata into tex format. Rather than describing everything in words, I'm attaching a screen shot of a simplified version of the table.

    Just for notational ease, lets suppose I have the following variables:
    - dummies for different major occupations (occ1,occ2, ..., occN)
    - indicator for PPJ ("pay for performance"; 1 = PPJ, 0 = FWJ)
    - different variables (e.g., income, age, etc)

    I tried making a table using a program that Johannes Schmeider created (see https://sites.google.com/site/johannesschmieder/stata), but didn't seem to get it to work -- the program runs without creating anything (obviously after I adjusted it to my context). I copied it below FYI.

    Thank you in advance for your help -- I know this is not a new type of question, lots written about tabulation, but esttab doesn't seem to incorporate all the features I'd like. If anyone has reference code that can produce the type of tables you'd see in a QJE-type article, that would be great!

    capture program drop sumtableref
    program define sumtableref
    syntax , [file(str)]
    local size "\footnotesize"

    use "$psid\psidfull_clean.dta", clear

    gen age_pp=age if pp==1
    gen age_fw=age if pp==0
    gen male_pp=male if pp==1
    gen male_fw=male if pp==0
    gen tenure_pp=tenure if pp==1
    gen tenure_fw=tenure if pp==0


    local allvars age_* male_* tenure_*

    local binary "male"

    local ppj age_pp male_pp tenure_pp
    local fwj age_fw male_fw tenure_fw

    gen occ_exec=cond(occname=="Executive, Admin, Managers",1,0)
    gen occ_pro=cond(occname=="Professional/Speciality",1,0)
    gen occ_tech=cond(occname=="Technical, Sales, Admin/Support",1,0)
    gen occ_sales=cond(occname=="Sales",1,0)


    local conditions occ_exec occ_pro occ_tech occ_sales // full

    local colhead0 & (1) & (2) & (3) & (4) // & (6)
    local colhead1 & Executives & Professionals & Technical and Support & Sales // & All

    local cols : word count "`conditions'"
    local cols = `cols'+1

    writeln `file' "\begin{table}[p]\centering "
    writeln `file' "\caption{Summary Table of PSID Data}`size'"
    writeln `file' "\begin{tabular}{l *{`=`cols'-1'}{c}} "
    writeln `file' "\toprule "
    writeln `file' " `colhead0' \"
    writeln `file' " `colhead1' \"
    local paneltitles ""Panel A: Pay for Performance"
    local panels ppj fwj
    foreach panel in `panels' {
    local ppos: list posof "`panel'" in local(panels)
    local paneltit: word `ppos' of `paneltitles'
    writeln `file' "\midrule"
    writeln `file' "\multicolumn{`cols'}{l}{\textbf{`size'{`paneltit' }}} \\ "
    foreach v in ``panel'' {
    local vl : variable label `v'
    local linemean `vl'
    local linesd
    foreach c in "`conditions'" {
    if "`c'"=="full" & (("`panel'"=="dwscontrols") | ("`=substr("`v'",1,2)'"=="lj")) {
    local linemean `linemean' &
    local linesd `linesd' &
    continue
    }
    qui sum `v' if `c'
    sigdigits a2 `=r(mean)'
    local mean: disp `fmt' `=r(mean)'
    local linemean `linemean' & `mean'
    sigdigits a2 `=r(sd)'
    local sd: disp `fmt' `=r(sd)'
    local linesd `linesd' & `=cond(`sd'!=.,"[`=ltrim("`sd'")']","`sd'")' // ,`=r(N)'
    }
    writeln `file' "`linemean' \"
    if !inlist("`v'","`binary'") writeln `file' "`linesd' \"
    }
    }
    writeln `file' "\midrule "
    local j 1
    local countline
    foreach c in "`conditions'" {
    count if `c'
    local count`j' = r(N)
    local countline `countline' & `count`j++''
    }
    writeln `file' "Number of observations `countline' \\ "

    writeln `file' "\bottomrule "
    writeln `file' "\multicolumn{`cols'}{l}{`size' Standard deviation in [brackets]. } \\ "
    writeln `file' "\end{tabular}"
    writeln `file' "\end{table}"
    writeln `file' "\pagebreak \clearpage "

    end // sumtableref
    Attached Files

  • #2
    Dear Christos,
    I didn't follow all of your code, but just notice that these three lines will return zero, so take a closer look about your adjustments to the original command
    Code:
    gen occ_exec=cond(occname=="Executive, Admin, Managers",1,0)
    gen occ_pro=cond(occname=="Professional/Speciality",1,0)
    gen occ_tech=cond(occname=="Technical, Sales, Admin/Support",1,0)
    type -help f_cond- for more details about this error.
    You may also find the tabout command (SSC) very helpful.

    Comment


    • #3
      Oded: Statements like

      Code:
        
      gen occ_exec=cond(occname=="Executive, Admin, Managers",1,0)
      could be simplified to

      Code:
          
      gen occ_exec = occname=="Executive, Admin, Managers"
      Either way, the variable concerned will be 1 or 0 depending on whether the equality is or is not satisfied.

      If the variable doesn't exist, there will be an obvious error.

      As Christos says nothing about this variable, I don't see that this can be declared an error.
      Last edited by Nick Cox; 08 Feb 2015, 03:51.

      Comment


      • #4
        Thanks Nick, you're absolutely right as I do not know whether this variable is numeric or string. My assumption was that he is using the labels instead of the values.

        Comment


        • #5
          Thank you for the reply! That turns out not to be an error -- variable creates alright. And thank you Nick for the suggestion on making the code more compact!

          One alternative to making tables is to stuff the relevant values into a matrix, but my impression is that there's not a ton of flexibility with how the matrix is filled up with non-numeric values (e.g., titles). Would be great if someone whose made these tables automatically from stata has some related code to suggest!

          Comment


          • #6
            Bump, just checking!

            Comment


            • #7
              I did not read your code in great detail, but certainly share the same goal of having Stata outputs automatically typeset in LaTeX.

              I have a reproducible example in which I run some regression and generate some figures in a do file, have them typeset, and the resulting pdf open in (Mac) Preview. All from within Stata. I hope you will find it helpful: https://github.com/felixleungsc/stataworkflow.

              Jörg Weber has three blog posts on how to format tables generated with the (user-written) estout command in a LaTeX document: http://www.jwe.cc/2012/03/stata-latex-tables-estout/. I used the code he provided there.

              You will see that the latex code used for each table has the same structure, and is therefore quite natural to have it generated from within Stata. To do so, you could make use of a short program.

              To illustrate:
              Code:
              capture program drop writetable
              * a program for writing the latex code for a table
              
              program writetable
              syntax name(name = filehandle), ///
                  filename(string) ///
                  tablename(string) ///
                  ncol(string) ///
                  tformat(string) ///
                  cwidth(string) ///
                  caption(string) ///
                  figsource(string) ///
                  fignote(string) ///
                  texlabel(string)
                  
              file open `filehandle' using `filename', write text replace
              
              file write `filehandle' _n ///
                  "\begin{table}[h!]" _n ///
                  "\centering" _n ///
                  _skip(2) "\begin{threeparttable}" _n ///
                  _skip(4) "\caption{`caption'}\label{`texlabel'}" _n ///
                  _skip(4) "\estauto{`tablename'}{`ncol'}{S[table-format=`tformat',table-column-width=`cwidth']}" _n ///
                  _skip(4) "\Fignote{`fignote'}" _n ///
                  _skip(4) "\Figsource{`figsource'}" _n ///
                  _skip(4) "\Starnote{}" _n ///
                  _skip(2) "\end{threeparttable}" _n ///
                  "\end{table}" _n
                  
              file close `filehandle'
              
              end
              You can then use -estout- to create a LaTeX table (say table1.tex) as illustrated in Jörg's posts (or the many more examples from Ben Jann http://repec.org/bocode/e/estout/examples.html), and have Stata write the LaTeX code with e.g.:
              Code:
              writetable table1, ///
                  filename(includetable1.tex) ///
                  tablename(table1.tex) ///
                  ncol(2) ///
                  tformat(1.3) ///
                  cwidth(13mm) ///
                  caption(OLS of wage) ///
                  figsource(nlsw88.dta) ///
                  fignote(This is a table note.) ///
                  texlabel(T:wage1)
              where includetable1.tex has the following lines:

              Code:
               
               \begin{table}[h!]  
               \centering  
               \begin{threeparttable}  
               \caption{OLS of wage}\label{T:wage1}  
               \estauto{table1.tex}{2}{S[table-format=1.3,table-column-width=13mm]}  
               \Fignote{This is a table note.}  
               \Figsource{nlsw88.dta}  
               \Starnote{}  
               \end{threeparttable}  
               \end{table}

              Comment

              Working...
              X