Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example Stata code to create nice descriptives and balance tables
*ssc install outreg
*ssc install estout
set seed 2299308
set obs 100
gen age = ceil(99*uniform())
gen male = rbinomial(1,0.8)
gen income = ceil(5000*uniform())
gen villagecode = ceil(5*uniform())
gen treated = rbinomial(1,0.5)
label var age "Age (years)"
label var male "Gender (1=male)"
label var income "Monthly Income (dollars)"
***Part 1: summary stats using esttab
eststo clear
estpost summarize age male income
esttab using "tables/table1.rtf", replace ///
cells("count(fmt(a2)) mean sd min max") label ///
title("Table 1: Descriptive Statistics") nomtitle nonumber noobs
*For latex
eststo clear
estpost summarize age male income
esttab using "tables/table1.tex", replace label substitute(# \#) ///
fragment nomtitle nonumber noobs cells("count(fmt(a2)) mean sd min max")
***Part 2: using outreg
global DESCVARS age male income
mata: mata clear
* First test of differences
local i = 1
foreach var in $DESCVARS {
reg `var' treated, vce(cluster villagecode)
outreg, keep(treated) rtitle("`: var label `var''") stats(b) ///
noautosumm store(row`i') starlevels(10 5 1) starloc(1)
outreg, replay(diff) append(row`i') ctitles("",Difference ) ///
store(diff) note("")
local ++i
outreg, replay(diff)
* Then Summary statistics
local count: word count $DESCVARS
mat sumstat = J(`count',6,.)
local i = 1
foreach var in $DESCVARS {
quietly: summarize `var' if treated==0
mat sumstat[`i',1] = r(N)
mat sumstat[`i',2] = r(mean)
mat sumstat[`i',3] = r(sd)
quietly: summarize `var' if treated==1
mat sumstat[`i',4] = r(N)
mat sumstat[`i',5] = r(mean)
mat sumstat[`i',6] = r(sd)
local i = `i' + 1
frmttable, statmat(sumstat) store(sumstat) sfmt(g,f,f,g,f,f)
*And export
outreg using "tables/balance.tex", ///
replay(sumstat) merge(diff) tex nocenter note("") fragment plain replace ///
ctitles("", Control, "", "", Treatment, "", "", "" \ "", n, mean, sd, n, mean, sd, Diff) ///
***Part 3: using postfiles
tempname memhold
tempfile stats_icc
postfile `memhold' str60 Variable N Mean SD ICC using "`stats_icc'"
foreach var in age male income {
scalar varlabel = `"`: var label `var''"'
quietly: su `var'
scalar N =`r(N)'
scalar Mean = `r(mean)'
scalar SD = `r(sd)'
quietly: loneway `var' villagecode
scalar ICC = `r(rho)'
post `memhold' (varlabel) (N) (Mean) (SD) (ICC)
scalar drop _all
postclose `memhold'
use "`stats_icc'", clear
*Export to csv.
export delimited using "tables/descriptives_icc.csv", replace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment