Skip to content
Module 11 of 1250 min readIntermediate

Reporting and reproducibility

esttab and outreg2 for publication tables, putexcel for custom output, locals and globals for parameterised do-files, and the loop syntax that scales analyses.

92%

Listen along

Read “Reporting and reproducibility” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Use locals and globals to parameterise do-files for reproducibility
  • 02Write foreach and forvalues loops for repeated analyses
  • 03Generate publication-ready tables with esttab/estout
  • 04Write custom Excel output with putexcel for tailored reports

Reproducibility in Stata is built around the do-file (your script), the log file (the run record), and the export tools that turn estimates into publication tables.

Logging a run

stata
capture log close
log using analysis_$(today).log, replace
* your analysis here
log close

Locals and globals — parameterising do-files

stata
local controls deposit_rate i.year
regress lending_rate `controls'
global CONTROLS deposit_rate i.year
regress lending_rate $CONTROLS

Loops — foreach and forvalues

stata
foreach var of varlist lending_rate deposit_rate spread {
summarize `var'
histogram `var', name(g_`var', replace)
}
forvalues y = 2020/2024 {
summarize lending_rate if year == `y'
}

esttab and outreg2 — publication tables

stata
ssc install estout, replace
regress lending_rate deposit_rate
estimates store m1
regress lending_rate deposit_rate i.year
estimates store m2
xtreg lending_rate deposit_rate, fe
estimates store m3
esttab m1 m2 m3 using regressions.tex, ///
cells(b(star fmt(3)) se(par fmt(3))) ///
stats(N r2 r2_a, fmt(0 3 3) labels("Observations" "R-squared" "Adj R-squared")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
label replace

putexcel — custom Excel output

stata
putexcel set output.xlsx, replace
putexcel A1 = "Bank" B1 = "Mean rate"
levelsof bank_id, local(banks)
local row = 2
foreach b of local banks {
summarize lending_rate if bank_id == `b', meanonly
putexcel A`row' = `b' B`row' = `r(mean)'
local row = `row' + 1
}

Reproducibility checklist

(1) Everything in a do-file. (2) Log every run. (3) Locals/globals at the top for paths and parameters. (4) esttab for tables, graph export for figures, putexcel for custom outputs. (5) Save intermediate datasets at each major step. With those five, you can rerun a year-old analysis in five minutes.

Exercise

Define a local 'controls' equal to deposit_rate i.year, then use it in a regress.

Key takeaways

  • Locals (`x') exist only inside one do-file run; globals ($x) persist across runs
  • foreach var of varlist x y z { ... } and forvalues i = 1/10 { ... } are the standard loops
  • esttab is the workhorse for regression tables; outreg2 is the predecessor still in use
  • Reproducibility checklist: do-file + log + locals/globals + esttab + saved intermediates

Further reading

  1. 01

    The Workflow of Data Analysis Using Stata

    J. Scott Long · Stata Press · 2008

  2. 02
  3. 03
Loading progress…
LeadAfrikPublic Economics Hub