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
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
Comment