Stata Skill
You have access to comprehensive Stata reference files. Do not load all files.
Read only the 1-3 files relevant to the user's current task using the routing table below.
Critical Gotchas
These are Stata-specific pitfalls that lead to silent bugs. Internalize these before writing any code.
Missing Values Sort to +Infinity
Stata's . (and .a-.z) are greater than all numbers.
* WRONG — includes observations where income is missing!
gen high_income = (income > 50000)
* RIGHT
gen high_income = (income > 50000) if !missing(income)
* WRONG — missing ages appear in this list
list if age > 60
* RIGHT
list if age > 60 & !missing(age)
= vs ==
= is assignment; == is comparison. Mixing them up is a syntax error or silent bug.
* WRONG — syntax error
gen employed = 1 if status = 1
* RIGHT
gen employed = 1 if status == 1
Local Macro Syntax
Locals use `name' (backtick + single-quote). Globals use $name or ${name}.
Forgetting the closing quote is the #1 macro bug.
local controls "age education income"
regress wage `controls' // correct
regress wage `controls // WRONG — missing closing quote
regress wage 'controls' // WRONG — wrong quote characters
by Requires Prior Sort (Use bysort)
* WRONG — error if data not sorted by id
by id: gen first = (_n == 1)
* RIGHT — bysort sorts automatically
bysort id: gen first = (_n == 1)
* Also RIGHT — explicit sort
sort id
by id: gen first = (_n == 1)
Factor Variable Notation (i. and c.)
Use i. for categorical, c. for continuous. Omitting i. treats categories as continuous.
* WRONG — treats race as continuous (e.g., race=3 has 3x effect of race=1)
regress wage race education
* RIGHT — creates dummies automatically
regress wage i.race education
* Interactions
regress wage i.race##c.education // full interaction
regress wage i.race#c.education // interaction only (no main effects)
generate vs replace
generate creates new variables; replace modifies existing ones. Using generate on an existing variable name is an error.
gen x = 1
gen x = 2 // ERROR: x already defined
replace x = 2 // correct
String Comparison Is Case-Sensitive
* May miss "Male", "MALE", etc.
keep if gender == "male"
* Safer
keep if lower(gender) == "male"
merge Always Check _merge
Never skip tab _merge — it costs nothing and is the only diagnostic you get when assert fails.
merge 1:1 id using other.dta
tab _merge // ALWAYS tab before assert
assert _merge == 3 // fails silently without tab output
drop _merge
preserve / restore + tempfile for Collapse-Merge-Back
The standard pattern for computing group stats and merging them onto the original data:
tempfile stats
preserve
collapse (mean) avg_x=x, by(group)
save `stats'
restore
merge m:1 group using `stats'
tab _merge
assert _merge == 3
drop _merge
For simple group means, bysort group: egen avg_x = mean(x) avoids the round-trip entirely.
Weights Are Not Interchangeable
fweight — frequency weights (replication)
aweight — analytic/regression weights (inverse variance)
pweight — probability/sampling weights (survey data, implies robust SE)
iweight — importance weights (rarely used)
capture Swallows Errors
capture some_command
if _rc != 0 {
di as error "Failed with code: " _rc
exit _rc
}
Line Continuation Uses ///
regress y x1 x2 x3 ///
x4 x5 x6, ///
vce(robust)
Stored Results: r() vs e() vs s()
r() — r-class commands (summarize, tabulate, etc.)
e() — e-class commands (estimation: regress, logit, etc.)
s() — s-class commands (parsing)
A new estimation command overwrites previous e() results. Store them first:
regress y x1 x2
estimates store model1
Running Stata from the Command Line
Claude can execute Stata code by running .do files in batch mode from the terminal. This is how to run Stata non-interactively.
Finding the Stata Binary
Stata on macOS is a .app bundle. The actual binary is inside it. Common locations:
# Stata 18 / StataNow (most common)
/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp
/Applications/StataNow/StataMP.app/Contents/MacOS/stata-mp
# Other editions (SE, BE)
/Applications/Stata/StataSE.app/Contents/MacOS/stata-se
/Applications/Stata/StataBE.app/Contents/MacOS/stata-be
If Stata isn't on $PATH, find it with: mdfind -name "stata-mp" | grep MacOS
Batch Mode (-b)
# Run a .do file in batch mode — output goes to <filename>.log
/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b do analysis.do
# If stata-mp is on PATH (e.g., via symlink or alias):
stata-mp -b do analysis.do
-b = batch mode (non-interactive, no GUI)
- Output (everything Stata would display) is written to
analysis.log in the working directory
- Exit code is 0 on success, non-zero on error
- The log file contains all output, including error messages — check it after execution
Running Inline Stata Code
To run a quick Stata snippet without creating a .do file:
# Write a temp .do file and run it
cat > /tmp/stata_run.do << 'EOF'
sysuse auto, clear
summarize price mpg
EOF
stata-mp -b do /tmp/stata_run.do
cat /tmp/stata_run.log
Checking Results
# Check if it succeeded
stata-mp -b do tests/run_tests.do && echo "SUCCESS" || echo "FAILED"
# Search the log for pass/fail
grep -E "PASS|FAIL|error|r\([0-9]+\)" run_tests.log
Tips
clear all at the top of batch scripts — batch mode starts with a fresh Stata session, but clear all ensures no stale state from prior runs in the same session.
set more off — prevents Stata from pausing for --more-- prompts (fatal in batch mode).
- Log files overwrite silently —
analysis.do always writes to analysis.log in the current directory. If you run multiple .do files, check the right log.
- Working directory — Stata's working directory is wherever you run the command from, not where the
.do file lives. Use cd in the .do file or absolute paths if needed.
Routing Table
Read only the files relevant to the user's task. Paths are relative to this SKILL.md file.
Data Operations
| File |
Topics & Key Commands |
references/basics-getting-started.md |
use, save, describe, browse, sysuse, basic workflow |
references/data-import-export.md |
import delimited, import excel, ODBC, export, web data |
references/data-management.md |
generate, replace, merge, append, reshape, collapse, recode, egen, encode/decode |
references/variables-operators.md |
Variable types, byte/int/long/float/double, operators, missing values (.<.a), if/in qualifiers |
references/string-functions.md |
substr(), regexm(), strtrim(), split, ustrlen(), regex, Unicode |
references/date-time-functions.md |
date(), clock(), %td/%tc formats, mdy(), dofm(), business calendars |
references/mathematical-functions.md |
round(), log(), exp(), abs(), mod(), cond(), distributions, random numbers |
Statistics & Econometrics
| File |
Topics & Key Commands |
references/descriptive-statistics.md |
summarize, tabulate, correlate, tabstat, codebook, weighted stats |
references/linear-regression.md |
regress, vce(robust), vce(cluster), test, lincom, margins, predict, ivregress |
references/panel-data.md |
xtset, xtreg fe/re, Hausman test, xtabond, dynamic panels |
references/time-series.md |
tsset, ARIMA, VAR, dfuller, pperron, irf, forecasting |
references/limited-dependent-variables.md |
logit, probit, tobit, poisson, nbreg, mlogit, ologit, margins for nonlinear |
references/bootstrap-simulation.md |
bootstrap, simulate, permute, Monte Carlo |
references/survey-data-analysis.md |
svyset, svy:, subpop(), complex survey design, replicate weights |
references/missing-data-handling.md |
mi impute, mi estimate, FIML, misstable, diagnostics |
references/maximum-likelihood.md |
ml model, custom likelihood functions, ml init, gradient-based optimization |
references/gmm-estimation.md |
gmm, moment conditions, estat overid, J-test |
Causal Inference
| File |
Topics & Key Commands |
references/treatment-effects.md |
teffects ra/ipw/ipwra/aipw, stteffects, ATE/ATT/ATET |
references/difference-in-differences.md |
DiD, parallel trends, event studies, staggered adoption |
references/regression-discontinuity.md |
Sharp/fuzzy RD, bandwidth selection, rdplot |
references/matching-methods.md |
PSM, nearest neighbor, kernel matching, teffects nnmatch |
references/sample-selection.md |
heckman, heckprobit, treatment models, exclusion restrictions |
Advanced Methods
| File |
Topics & Key Commands |
references/survival-analysis.md |
stset, stcox, streg, Kaplan-Meier, parametric models |
references/sem-factor-analysis.md |
sem, gsem, CFA, path analysis, alpha, reliability |
references/nonparametric-methods.md |
kdensity, rank tests, qreg, npregress |
references/spatial-analysis.md |
spmatrix, spregress, spatial weights, Moran's I |
references/machine-learning.md |
lasso, elasticnet, cvlasso, cross-validation |
Graphics
| File |
Topics & Key Commands |
references/graphics.md |
twoway, scatter, line, bar, histogram, graph combine, graph export, schemes |
Programming
| File |
Topics & Key Commands |
references/programming-basics.md |
local, global, foreach, forvalues, program define, syntax, return |
references/advanced-programming.md |
syntax, mata, classes, _prefix, dialog boxes, tempfile/tempvar |
references/mata-introduction.md |
Mata basics, when to use Mata vs ado, data types |
references/mata-programming.md |
Mata functions, flow control, structures, pointers |
references/mata-matrix-operations.md |
Matrix creation, decompositions, solvers, st_matrix() |
references/mata-data-access.md |
st_data(), st_view(), st_store(), performance tips |
Output & Workflow
| File |
Topics & Key Commands |
references/tables-reporting.md |
putexcel, putdocx, putpdf, LaTeX integration, collect |
references/workflow-best-practices.md |
Project structure, master do-files, version control, debugging, common mistakes |
references/external-tools-integration.md |
Python via python:, R via rsource, shell commands, Git |
references/filing-issues.md |
User wants to report a Stata skill documentation gap or error to the repository |
Community Packages
| File |
What It Does |
packages/reghdfe.md |
High-dimensional fixed effects OLS (absorbs multiple FE sets efficiently) |
packages/estout.md |
esttab/estout: publication-quality regression tables |
packages/outreg2.md |
Alternative regression table exporter (Word, Excel, TeX) |
packages/asdoc.md |
One-command Word document creation for any Stata output |
packages/tabout.md |
Cross-tabulations and summary tables to file |
packages/coefplot.md |
Coefficient plots from stored estimates |
packages/graph-schemes.md |
grstyle, schemepack, plotplain — better graph themes |
packages/did.md |
Modern DiD: csdid, did_multiplegt, did_imputation (Callaway-Sant'Anna, de Chaisemartin-D'Haultfoeuille, Borusyak-Jaravel-Spiess) |
packages/event-study.md |
eventstudyinteract, eventdd — event study estimators |
packages/rdrobust.md |
Robust RD estimation with optimal bandwidth (rdrobust, rdplot, rdbwselect) |
packages/psmatch2.md |
Propensity score matching (nearest neighbor, kernel, radius) |
packages/synth.md |
Synthetic control method (synth, synth_runner) |
packages/ivreg2.md |
Enhanced IV/2SLS: ivreg2, xtivreg2 with additional diagnostics |
packages/xtabond2.md |
Dynamic panel GMM (Arellano-Bond/Blundell-Bond) |
packages/binsreg.md |
Binned scatter plots with CI (binsreg, binstest) |
packages/nprobust.md |
Nonparametric kernel estimation and inference |
packages/diagnostics.md |
bacondecomp, xttest3, collinearity, heteroskedasticity tests |
packages/winsor.md |
Winsorizing and trimming: winsor2, winsor |
packages/data-manipulation.md |
gtools (fast collapse/egen), rangestat, egenmore |
packages/package-management.md |
ssc install, net install, ado update, finding packages |
Common Patterns
Regression Table Workflow
* Estimate models
eststo clear
eststo: regress y x1 x2, vce(robust)
eststo: regress y x1 x2 x3, vce(robust)
eststo: regress y x1 x2 x3 x4, vce(cluster id)
* Export table
esttab using "results.tex", replace ///
se star(* 0.10 ** 0.05 *** 0.01) ///
label booktabs ///
title("Main Results") ///
mtitles("(1)" "(2)" "(3)")
Panel Data Setup
xtset panelid timevar // declare panel structure
xtdescribe // check balance
xtsum outcome // within/between variation
* Fixed effects
xtreg y x1 x2, fe vce(cluster panelid)
* Or with reghdfe (preferred for multiple FE)
reghdfe y x1 x2, absorb(panelid timevar) vce(cluster panelid)
Difference-in-Differences
* Classic 2x2 DiD
gen post = (year >= treatment_year)
gen treat_post = treated * post
regress y treated post treat_post, vce(cluster id)
* Event study (uniform timing — must interact with treatment group)
reghdfe y ib(-1).rel_time#1.treated, absorb(id year) vce(cluster id)
testparm *.rel_time#1.treated // pre-trend test
* Modern staggered DiD (Callaway & Sant'Anna)
csdid y x1 x2, ivar(id) time(year) gvar(first_treat) agg(event)
csdid_plot
Graph Export
* Publication-quality scatter with fit line
twoway (scatter y x, mcolor(navy%50) msize(small)) ///
(lfit y x, lcolor(cranberry) lwidth(medthick)), ///
title("Title Here") ///
xtitle("X Label") ytitle("Y Label") ///
legend(off) scheme(s2color)
graph export "figure1.pdf", replace as(pdf)
graph export "figure1.png", replace as(png) width(2400)
Data Cleaning Pipeline
* Load and inspect
import delimited "raw_data.csv", clear varnames(1)
describe
codebook, compact
* Clean
rename *, lower // lowercase all varnames
destring income, replace force // convert string to numeric
replace income = . if income < 0
* Label
label variable income "Annual household income (USD)"
label define yesno 0 "No" 1 "Yes"
label values employed yesno
* Save
compress
save "clean_data.dta", replace
Multiple Imputation
mi set mlong
mi register imputed income education
mi impute chained (regress) income (ologit) education = age i.gender, add(20) rseed(12345)
mi estimate: regress wage income education age i.gender
Help Improve This Skill
If you produce Stata code with a significant error — wrong syntax, incorrect command usage, or a gotcha you failed to catch — and the issue seems to stem from a gap in these reference files rather than a one-off mistake, consider suggesting to the user that they file an issue on the skill repository. This helps future users.
When to raise this: Only after you've already corrected the error and the user has working code. Frame it as optional: "I made an error with [X] that I think comes from a gap in the Stata skill documentation. If you'd like, I can help you file an issue or a PR so it gets fixed for everyone."
When NOT to raise this: If the user is on Claude Haiku, the error is more likely a model capability issue than a documentation gap. In that case, suggest they try Sonnet or Opus for complex Stata work instead of filing an issue.
If the user agrees, read references/filing-issues.md for instructions on writing a good issue report.
1---2name: stata-23description: Comprehensive Stata reference for writing correct .do files, data management, econometrics, causal inference, graphics, Mata programming, and 20 community packages (reghdfe, estout, did, rdrobust, etc.). Covers syntax, options, gotchas, and idiomatic patterns. Use this skill whenever the user asks you to write, debug, or explain Stata code.4---5
6# Stata Skill
7
8You have access to comprehensive Stata reference files. **Do not load all files.**
9Read only the 1-3 files relevant to the user's current task using the routing table below.
10
11---
12
13## Critical Gotchas
14
15These are Stata-specific pitfalls that lead to silent bugs. Internalize these before writing any code.
16
17### Missing Values Sort to +Infinity
18Stata's `.` (and `.a`-`.z`) are **greater than all numbers**.
19```stata
20* WRONG — includes observations where income is missing!
21gen high_income = (income > 50000)
22
23* RIGHT
24gen high_income = (income > 50000) if !missing(income)
25
26* WRONG — missing ages appear in this list
27list if age > 60
28
29* RIGHT
30list if age > 60 & !missing(age)
31```
32
33### `=` vs `==`
34`=` is assignment; `==` is comparison. Mixing them up is a syntax error or silent bug.
35```stata
36* WRONG — syntax error
37gen employed = 1 if status = 1
38
39* RIGHT
40gen employed = 1 if status == 1
41```
42
43### Local Macro Syntax
44Locals use `` `name' `` (backtick + single-quote). Globals use `$name` or `${name}`.
45Forgetting the closing quote is the #1 macro bug.
46```stata
47local controls "age education income"
48regress wage `controls' // correct
49regress wage `controls // WRONG — missing closing quote
50regress wage 'controls' // WRONG — wrong quote characters
51```
52
53### `by` Requires Prior Sort (Use `bysort`)
54```stata
55* WRONG — error if data not sorted by id
56by id: gen first = (_n == 1)
57
58* RIGHT — bysort sorts automatically
59bysort id: gen first = (_n == 1)
60
61* Also RIGHT — explicit sort
62sort id
63by id: gen first = (_n == 1)
64```
65
66### Factor Variable Notation (`i.` and `c.`)
67Use `i.` for categorical, `c.` for continuous. Omitting `i.` treats categories as continuous.
68```stata
69* WRONG — treats race as continuous (e.g., race=3 has 3x effect of race=1)
70regress wage race education
71
72* RIGHT — creates dummies automatically
73regress wage i.race education
74
75* Interactions
76regress wage i.race##c.education // full interaction
77regress wage i.race#c.education // interaction only (no main effects)
78```
79
80### `generate` vs `replace`
81`generate` creates new variables; `replace` modifies existing ones. Using `generate` on an existing variable name is an error.
82```stata
83gen x = 1
84gen x = 2 // ERROR: x already defined
85replace x = 2 // correct
86```
87
88### String Comparison Is Case-Sensitive
89```stata
90* May miss "Male", "MALE", etc.
91keep if gender == "male"
92
93* Safer
94keep if lower(gender) == "male"
95```
96
97### `merge` Always Check `_merge`
98Never skip `tab _merge` — it costs nothing and is the only diagnostic you get when `assert` fails.
99```stata
100merge 1:1 id using other.dta
101tab _merge // ALWAYS tab before assert
102assert _merge == 3 // fails silently without tab output
103drop _merge
104```
105
106### `preserve` / `restore` + `tempfile` for Collapse-Merge-Back
107The standard pattern for computing group stats and merging them onto the original data:
108```stata
109tempfile stats
110preserve
111collapse (mean) avg_x=x, by(group)
112save `stats'
113restore
114merge m:1 group using `stats'
115tab _merge
116assert _merge == 3
117drop _merge
118```
119For simple group means, `bysort group: egen avg_x = mean(x)` avoids the round-trip entirely.
120
121### Weights Are Not Interchangeable
122- `fweight` — frequency weights (replication)
123- `aweight` — analytic/regression weights (inverse variance)
124- `pweight` — probability/sampling weights (survey data, implies robust SE)
125- `iweight` — importance weights (rarely used)
126
127### `capture` Swallows Errors
128```stata
129capture some_command
130if _rc != 0 {
131 di as error "Failed with code: " _rc
132 exit _rc
133}
134```
135
136### Line Continuation Uses `///`
137```stata
138regress y x1 x2 x3 ///
139 x4 x5 x6, ///
140 vce(robust)
141```
142
143### Stored Results: `r()` vs `e()` vs `s()`
144- `r()` — r-class commands (summarize, tabulate, etc.)
145- `e()` — e-class commands (estimation: regress, logit, etc.)
146- `s()` — s-class commands (parsing)
147
148A new estimation command **overwrites** previous `e()` results. Store them first:
149```stata
150regress y x1 x2
151estimates store model1
152```
153
154---
155
156## Running Stata from the Command Line
157
158Claude can execute Stata code by running `.do` files in batch mode from the terminal. This is how to run Stata non-interactively.
159
160### Finding the Stata Binary
161
162Stata on macOS is a `.app` bundle. The actual binary is inside it. Common locations:
163
164```
165# Stata 18 / StataNow (most common)
166/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp
167/Applications/StataNow/StataMP.app/Contents/MacOS/stata-mp
168
169# Other editions (SE, BE)
170/Applications/Stata/StataSE.app/Contents/MacOS/stata-se
171/Applications/Stata/StataBE.app/Contents/MacOS/stata-be
172```
173
174If Stata isn't on `$PATH`, find it with: `mdfind -name "stata-mp" | grep MacOS`
175
176### Batch Mode (`-b`)
177
178```bash
179# Run a .do file in batch mode — output goes to <filename>.log
180/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b do analysis.do
181
182# If stata-mp is on PATH (e.g., via symlink or alias):
183stata-mp -b do analysis.do
184```
185
186- `-b` = batch mode (non-interactive, no GUI)
187- Output (everything Stata would display) is written to `analysis.log` in the working directory
188- Exit code is 0 on success, non-zero on error
189- The log file contains all output, including error messages — check it after execution
190
191### Running Inline Stata Code
192
193To run a quick Stata snippet without creating a `.do` file:
194
195```bash
196# Write a temp .do file and run it
197cat > /tmp/stata_run.do << 'EOF'
198sysuse auto, clear
199summarize price mpg
200EOF
201stata-mp -b do /tmp/stata_run.do
202cat /tmp/stata_run.log
203```
204
205### Checking Results
206
207```bash
208# Check if it succeeded
209stata-mp -b do tests/run_tests.do && echo "SUCCESS" || echo "FAILED"
210
211# Search the log for pass/fail
212grep -E "PASS|FAIL|error|r\([0-9]+\)" run_tests.log
213```
214
215### Tips
216
217- **`clear all` at the top of batch scripts** — batch mode starts with a fresh Stata session, but `clear all` ensures no stale state from prior runs in the same session.
218- **`set more off`** — prevents Stata from pausing for `--more--` prompts (fatal in batch mode).
219- **Log files overwrite silently** — `analysis.do` always writes to `analysis.log` in the current directory. If you run multiple `.do` files, check the right log.
220- **Working directory** — Stata's working directory is wherever you run the command from, not where the `.do` file lives. Use `cd` in the `.do` file or absolute paths if needed.
221
222---
223
224## Routing Table
225
226Read only the files relevant to the user's task. Paths are relative to this SKILL.md file.
227
228### Data Operations
229| File | Topics & Key Commands |
230|------|----------------------|
231| `references/basics-getting-started.md` | `use`, `save`, `describe`, `browse`, `sysuse`, basic workflow |
232| `references/data-import-export.md` | `import delimited`, `import excel`, ODBC, `export`, web data |
233| `references/data-management.md` | `generate`, `replace`, `merge`, `append`, `reshape`, `collapse`, `recode`, `egen`, `encode`/`decode` |
234| `references/variables-operators.md` | Variable types, `byte`/`int`/`long`/`float`/`double`, operators, missing values (`.<.a`), `if`/`in` qualifiers |
235| `references/string-functions.md` | `substr()`, `regexm()`, `strtrim()`, `split`, `ustrlen()`, regex, Unicode |
236| `references/date-time-functions.md` | `date()`, `clock()`, `%td`/`%tc` formats, `mdy()`, `dofm()`, business calendars |
237| `references/mathematical-functions.md` | `round()`, `log()`, `exp()`, `abs()`, `mod()`, `cond()`, distributions, random numbers |
238
239### Statistics & Econometrics
240| File | Topics & Key Commands |
241|------|----------------------|
242| `references/descriptive-statistics.md` | `summarize`, `tabulate`, `correlate`, `tabstat`, `codebook`, weighted stats |
243| `references/linear-regression.md` | `regress`, `vce(robust)`, `vce(cluster)`, `test`, `lincom`, `margins`, `predict`, `ivregress` |
244| `references/panel-data.md` | `xtset`, `xtreg fe`/`re`, Hausman test, `xtabond`, dynamic panels |
245| `references/time-series.md` | `tsset`, ARIMA, VAR, `dfuller`, `pperron`, `irf`, forecasting |
246| `references/limited-dependent-variables.md` | `logit`, `probit`, `tobit`, `poisson`, `nbreg`, `mlogit`, `ologit`, `margins` for nonlinear |
247| `references/bootstrap-simulation.md` | `bootstrap`, `simulate`, `permute`, Monte Carlo |
248| `references/survey-data-analysis.md` | `svyset`, `svy:`, `subpop()`, complex survey design, replicate weights |
249| `references/missing-data-handling.md` | `mi impute`, `mi estimate`, FIML, `misstable`, diagnostics |
250| `references/maximum-likelihood.md` | `ml model`, custom likelihood functions, `ml init`, gradient-based optimization |
251| `references/gmm-estimation.md` | `gmm`, moment conditions, `estat overid`, J-test |
252
253### Causal Inference
254| File | Topics & Key Commands |
255|------|----------------------|
256| `references/treatment-effects.md` | `teffects ra/ipw/ipwra/aipw`, `stteffects`, ATE/ATT/ATET |
257| `references/difference-in-differences.md` | DiD, parallel trends, event studies, staggered adoption |
258| `references/regression-discontinuity.md` | Sharp/fuzzy RD, bandwidth selection, `rdplot` |
259| `references/matching-methods.md` | PSM, nearest neighbor, kernel matching, `teffects nnmatch` |
260| `references/sample-selection.md` | `heckman`, `heckprobit`, treatment models, exclusion restrictions |
261
262### Advanced Methods
263| File | Topics & Key Commands |
264|------|----------------------|
265| `references/survival-analysis.md` | `stset`, `stcox`, `streg`, Kaplan-Meier, parametric models |
266| `references/sem-factor-analysis.md` | `sem`, `gsem`, CFA, path analysis, `alpha`, reliability |
267| `references/nonparametric-methods.md` | `kdensity`, rank tests, `qreg`, `npregress` |
268| `references/spatial-analysis.md` | `spmatrix`, `spregress`, spatial weights, Moran's I |
269| `references/machine-learning.md` | `lasso`, `elasticnet`, `cvlasso`, cross-validation |
270
271### Graphics
272| File | Topics & Key Commands |
273|------|----------------------|
274| `references/graphics.md` | `twoway`, `scatter`, `line`, `bar`, `histogram`, `graph combine`, `graph export`, schemes |
275
276### Programming
277| File | Topics & Key Commands |
278|------|----------------------|
279| `references/programming-basics.md` | `local`, `global`, `foreach`, `forvalues`, `program define`, `syntax`, `return` |
280| `references/advanced-programming.md` | `syntax`, `mata`, classes, `_prefix`, dialog boxes, `tempfile`/`tempvar` |
281| `references/mata-introduction.md` | Mata basics, when to use Mata vs ado, data types |
282| `references/mata-programming.md` | Mata functions, flow control, structures, pointers |
283| `references/mata-matrix-operations.md` | Matrix creation, decompositions, solvers, `st_matrix()` |
284| `references/mata-data-access.md` | `st_data()`, `st_view()`, `st_store()`, performance tips |
285
286### Output & Workflow
287| File | Topics & Key Commands |
288|------|----------------------|
289| `references/tables-reporting.md` | `putexcel`, `putdocx`, `putpdf`, LaTeX integration, `collect` |
290| `references/workflow-best-practices.md` | Project structure, master do-files, version control, debugging, common mistakes |
291| `references/external-tools-integration.md` | Python via `python:`, R via `rsource`, shell commands, Git |
292| `references/filing-issues.md` | User wants to report a Stata skill documentation gap or error to the repository |
293
294### Community Packages
295| File | What It Does |
296|------|-------------|
297| `packages/reghdfe.md` | High-dimensional fixed effects OLS (absorbs multiple FE sets efficiently) |
298| `packages/estout.md` | `esttab`/`estout`: publication-quality regression tables |
299| `packages/outreg2.md` | Alternative regression table exporter (Word, Excel, TeX) |
300| `packages/asdoc.md` | One-command Word document creation for any Stata output |
301| `packages/tabout.md` | Cross-tabulations and summary tables to file |
302| `packages/coefplot.md` | Coefficient plots from stored estimates |
303| `packages/graph-schemes.md` | `grstyle`, `schemepack`, `plotplain` — better graph themes |
304| `packages/did.md` | Modern DiD: `csdid`, `did_multiplegt`, `did_imputation` (Callaway-Sant'Anna, de Chaisemartin-D'Haultfoeuille, Borusyak-Jaravel-Spiess) |
305| `packages/event-study.md` | `eventstudyinteract`, `eventdd` — event study estimators |
306| `packages/rdrobust.md` | Robust RD estimation with optimal bandwidth (`rdrobust`, `rdplot`, `rdbwselect`) |
307| `packages/psmatch2.md` | Propensity score matching (nearest neighbor, kernel, radius) |
308| `packages/synth.md` | Synthetic control method (`synth`, `synth_runner`) |
309| `packages/ivreg2.md` | Enhanced IV/2SLS: `ivreg2`, `xtivreg2` with additional diagnostics |
310| `packages/xtabond2.md` | Dynamic panel GMM (Arellano-Bond/Blundell-Bond) |
311| `packages/binsreg.md` | Binned scatter plots with CI (`binsreg`, `binstest`) |
312| `packages/nprobust.md` | Nonparametric kernel estimation and inference |
313| `packages/diagnostics.md` | `bacondecomp`, `xttest3`, collinearity, heteroskedasticity tests |
314| `packages/winsor.md` | Winsorizing and trimming: `winsor2`, `winsor` |
315| `packages/data-manipulation.md` | `gtools` (fast collapse/egen), `rangestat`, `egenmore` |
316| `packages/package-management.md` | `ssc install`, `net install`, `ado update`, finding packages |
317
318---
319
320## Common Patterns
321
322### Regression Table Workflow
323```stata
324* Estimate models
325eststo clear
326eststo: regress y x1 x2, vce(robust)
327eststo: regress y x1 x2 x3, vce(robust)
328eststo: regress y x1 x2 x3 x4, vce(cluster id)
329
330* Export table
331esttab using "results.tex", replace ///
332 se star(* 0.10 ** 0.05 *** 0.01) ///
333 label booktabs ///
334 title("Main Results") ///
335 mtitles("(1)" "(2)" "(3)")
336```
337
338### Panel Data Setup
339```stata
340xtset panelid timevar // declare panel structure
341xtdescribe // check balance
342xtsum outcome // within/between variation
343
344* Fixed effects
345xtreg y x1 x2, fe vce(cluster panelid)
346* Or with reghdfe (preferred for multiple FE)
347reghdfe y x1 x2, absorb(panelid timevar) vce(cluster panelid)
348```
349
350### Difference-in-Differences
351```stata
352* Classic 2x2 DiD
353gen post = (year >= treatment_year)
354gen treat_post = treated * post
355regress y treated post treat_post, vce(cluster id)
356
357* Event study (uniform timing — must interact with treatment group)
358reghdfe y ib(-1).rel_time#1.treated, absorb(id year) vce(cluster id)
359testparm *.rel_time#1.treated // pre-trend test
360
361* Modern staggered DiD (Callaway & Sant'Anna)
362csdid y x1 x2, ivar(id) time(year) gvar(first_treat) agg(event)
363csdid_plot
364```
365
366### Graph Export
367```stata
368* Publication-quality scatter with fit line
369twoway (scatter y x, mcolor(navy%50) msize(small)) ///
370 (lfit y x, lcolor(cranberry) lwidth(medthick)), ///
371 title("Title Here") ///
372 xtitle("X Label") ytitle("Y Label") ///
373 legend(off) scheme(s2color)
374graph export "figure1.pdf", replace as(pdf)
375graph export "figure1.png", replace as(png) width(2400)
376```
377
378### Data Cleaning Pipeline
379```stata
380* Load and inspect
381import delimited "raw_data.csv", clear varnames(1)
382describe
383codebook, compact
384
385* Clean
386rename *, lower // lowercase all varnames
387destring income, replace force // convert string to numeric
388replace income = . if income < 0
389
390* Label
391label variable income "Annual household income (USD)"
392label define yesno 0 "No" 1 "Yes"
393label values employed yesno
394
395* Save
396compress
397save "clean_data.dta", replace
398```
399
400### Multiple Imputation
401```stata
402mi set mlong
403mi register imputed income education
404mi impute chained (regress) income (ologit) education = age i.gender, add(20) rseed(12345)
405mi estimate: regress wage income education age i.gender
406```
407
408---
409
410## Help Improve This Skill
411
412If you produce Stata code with a significant error — wrong syntax, incorrect command usage, or a gotcha you failed to catch — and the issue seems to stem from a gap in these reference files rather than a one-off mistake, consider suggesting to the user that they file an issue on the skill repository. This helps future users.
413
414**When to raise this:** Only after you've already corrected the error and the user has working code. Frame it as optional: *"I made an error with [X] that I think comes from a gap in the Stata skill documentation. If you'd like, I can help you file an issue or a PR so it gets fixed for everyone."*
415
416**When NOT to raise this:** If the user is on Claude Haiku, the error is more likely a model capability issue than a documentation gap. In that case, suggest they try Sonnet or Opus for complex Stata work instead of filing an issue.
417
418If the user agrees, read `references/filing-issues.md` for instructions on writing a good issue report.