Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Comprehensive Stata reference covering syntax, econometrics, and 20+ packages
.claude/skills/brycewang-stanford-stata-reference-guide/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 86% | 0% |
| case-05 | ✗→✓ | ▲ Improved | 74% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 95% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 117% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 289% | 0% |
Stata is the dominant statistical software in economics, political science, public health, and sociology research. This guide provides a comprehensive reference covering core syntax, data management, estimation commands, causal inference methods, graphics, Mata programming, and 20+ community-contributed packages. It is designed as a progressive-disclosure reference: use the section relevant to your current task rather than reading end-to-end.
stata* Import CSV with variable names in first row import delimited "data.csv", clear varnames(1) * Import Excel (specific sheet and cell range) import excel "workbook.xlsx", sheet("Sheet1") cellrange(A1:Z1000) firstrow clear * Import Stata format use "dataset.dta", clear * Export to CSV export delimited "output.csv", replace * Save as Stata format save "cleaned_data.dta", replace
stata* Generate new variables gen log_income = ln(income) gen age_sq = age^2 gen treatment_post = treatment * post * Recode and label recode education (1/12 = 1 "HS or less") (13/16 = 2 "College") (17/20 = 3 "Graduate"), gen(edu_cat) label variable edu_cat "Education Category" * String operations gen first_name = word(full_name, 1) gen year_str = string(year) destring price_str, gen(price) force * Date handling gen date = date(date_str, "YMD") format date %td gen year = year(date) gen quarter = quarter(date)
stata* Identify and handle duplicates duplicates report id year duplicates tag id year, gen(dup_flag) duplicates drop id year, force * Missing values misstable summarize misstable patterns replace income = . if income < 0 // recode impossible values * Merge datasets merge 1:1 id year using "panel_data.dta", keep(match master) nogen merge m:1 state year using "state_controls.dta", keep(match master) nogen * Reshape between wide and long reshape long income_, i(id) j(year) reshape wide income, i(id) j(year) * Collapse to group level collapse (mean) avg_income=income (sd) sd_income=income (count) n=income, by(state year)
stata* OLS with robust standard errors reg y x1 x2 x3, robust * Clustered standard errors reg y x1 x2 x3, cluster(firm_id) * Fixed effects (within estimator) xtreg y x1 x2 x3, fe cluster(firm_id) xtset firm_id year // must declare panel structure first * Absorbing high-dimensional FE (reghdfe) reghdfe y x1 x2 x3, absorb(firm_id year) cluster(firm_id) * Instrumental variables (2SLS) ivregress 2sls y x1 x2 (endog_var = instrument1 instrument2), robust estat firststage estat overid
stata* Panel setup xtset firm_id year * Hausman test (FE vs RE) quietly xtreg y x1 x2, fe estimates store fe quietly xtreg y x1 x2, re estimates store re hausman fe re * Dynamic panel GMM (xtabond2) xtabond2 y L.y x1 x2, gmm(L.y, lag(2 4)) iv(x1 x2) robust twostep * Test for serial correlation and overidentification estat abond // Arellano-Bond test estat sargan // Sargan/Hansen test
stata* Difference-in-Differences gen did = treatment * post reg y did treatment post controls, cluster(state) * Modern DiD with staggered treatment (csdid) csdid y x1 x2, ivar(id) time(year) gvar(first_treat) method(dripw) csdid_plot // event study plot * Regression Discontinuity (rdrobust) rdrobust y running_var, c(0) p(1) kernel(triangular) rdplot y running_var, c(0) p(1) * Propensity Score Matching (psmatch2) psmatch2 treatment x1 x2 x3, outcome(y) logit caliper(0.05) common pstest x1 x2 x3 // balance check * Synthetic Control (synth) synth y x1 x2 x3 y(1990) y(1991) y(1992), trunit(1) trperiod(1993) fig
stata* Logit/Probit logit binary_y x1 x2, robust margins, dydx(*) // average marginal effects probit binary_y x1 x2, robust margins, dydx(*) * Ordered logit ologit ordered_y x1 x2, robust margins, predict(outcome(3)) dydx(x1) * Tobit (censored regression) tobit y x1 x2, ll(0) * Poisson and Negative Binomial poisson count_y x1 x2, robust nbreg count_y x1 x2, robust
stata* Install from SSC (Statistical Software Components) ssc install reghdfe ssc install estout ssc install coefplot ssc install csdid ssc install rdrobust ssc install psmatch2 ssc install synth ssc install ivreg2 ssc install xtabond2 ssc install winsor2 ssc install gtools ssc install ftools ssc install binscatter ssc install binsreg ssc install grstyle * Install from GitHub net install did_multiplegt, from("https://raw.githubusercontent.com/chaisemartinDehejia/did_multiplegt/main")
stata* estout / esttab — formatted regression tables eststo clear eststo: reg y x1 x2, robust eststo: reg y x1 x2 x3, robust eststo: reg y x1 x2 x3, cluster(firm_id) esttab, se star(* 0.10 ** 0.05 *** 0.01) /// title("Main Results") label replace /// scalars("r2 R-squared" "N Observations") * Export to LaTeX esttab using "table1.tex", replace booktabs /// se star(* 0.10 ** 0.05 *** 0.01) label * Export to CSV/Excel esttab using "table1.csv", replace se * coefplot — coefficient visualization coefplot est1 est2 est3, drop(_cons) xline(0) /// title("Coefficient Estimates") legend(order(1 "Model 1" 2 "Model 2" 3 "Model 3"))
stata* Scatter with fit line twoway (scatter y x) (lfit y x), title("Y vs X") /// xtitle("X Variable") ytitle("Y Variable") * Event study plot coefplot, vertical drop(_cons) yline(0) /// title("Event Study") xtitle("Periods Relative to Treatment") * Binned scatter (binscatter) binscatter y x, controls(z1 z2) nquantiles(20) /// title("Binned Scatter") xtitle("X") ytitle("Y") * Kernel density kdensity income if year==2020, normal /// title("Income Distribution") xtitle("Income") * Graph styling (grstyle) grstyle init grstyle set plain, horizontal grid grstyle color background white grstyle set color economist
stata* Basic Mata usage mata: // Matrix operations X = st_data(., ("x1", "x2", "x3")) y = st_data(., "y") n = rows(X) // OLS by hand X = X, J(n, 1, 1) // add constant beta = invsym(X'X) * X'y e = y - X * beta sigma2 = (e'e) / (n - cols(X)) V = sigma2 * invsym(X'X) se = sqrt(diagonal(V)) beta, se end
set seed 12345preserve/restore for temporary data manipulations within a do-filelog using "analysis_log.smcl", replaceversion 17 (or your version) for reproducibilitytempfile merged then save merged'timer on 1 / timer off 1 / timer list for long-running operationsgtools (greshape, gcollapse, gegen) for 5-10x speedups on large datasets| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 10,718 | 13,931 | +30% | 1 | 1 | 0% | 2,043 | 3,797 | +86% | 0 | 0 | — |
case-02 | pass→pass | 4,498 | 3,956 | -12% | 1 | 1 | 0% | 794 | 3,091 | +289% | 0 | 0 | — |
case-03 | pass→pass | 7,672 | 7,615 | -1% | 1 | 1 | 0% | 1,505 | 3,733 | +148% | 0 | 0 | — |
case-04 | pass→pass | 5,965 | 5,443 | -9% | 1 | 1 | 0% | 1,131 | 3,329 | +194% | 0 | 0 | — |
case-05 | fail→pass | 17,341 | 17,241 | -1% | 1 | 1 | 0% | 3,144 | 5,459 | +74% | 0 | 0 | — |
case-06 | pass→pass | 9,897 | 5,729 | -42% | 1 | 1 | 0% | 1,815 | 3,429 | +89% | 0 | 0 | — |
case-07 | pass→pass | 11,445 | 8,030 | -30% | 1 | 1 | 0% | 2,181 | 3,803 | +74% | 0 | 0 | — |
case-08 | fail→pass | 10,069 | 6,012 | -40% | 1 | 1 | 0% | 1,840 | 3,594 | +95% | 0 | 0 | — |
case-09 | pass→pass | 11,172 | 6,451 | -42% | 1 | 1 | 0% | 2,121 | 3,645 | +72% | 0 | 0 | — |
case-10 | pass→pass | 13,864 | 13,049 | -6% | 1 | 1 | 0% | 2,585 | 4,864 | +88% | 0 | 0 | — |
case-11 | pass→pass | 15,587 | 11,679 | -25% | 1 | 1 | 0% | 2,655 | 4,350 | +64% | 0 | 0 | — |
case-12 | pass→pass | 4,880 | 4,228 | -13% | 1 | 1 | 0% | 858 | 3,080 | +259% | 0 | 0 | — |
case-13 | pass→pass | 9,200 | 4,716 | -49% | 1 | 1 | 0% | 1,686 | 3,283 | +95% | 0 | 0 | — |
case-14 | fail→pass | 9,764 | 6,637 | -32% | 1 | 1 | 0% | 1,674 | 3,625 | +117% | 0 | 0 | — |
case-15 | pass→pass | 3,438 | 3,774 | +10% | 1 | 1 | 0% | 558 | 2,931 | +425% | 0 | 0 | — |
case-16 | pass→pass | 9,741 | 8,546 | -12% | 1 | 1 | 0% | 1,735 | 3,894 | +124% | 0 | 0 | — |
case-17 | pass→pass | 14,422 | 5,989 | -58% | 1 | 1 | 0% | 2,397 | 3,353 | +40% | 0 | 0 | — |
case-18 | pass→pass | 9,296 | 7,708 | -17% | 1 | 1 | 0% | 1,521 | 3,649 | +140% | 0 | 0 | — |
case-19 | pass→pass | 2,540 | 2,541 | +0% | 1 | 1 | 0% | 450 | 2,860 | +536% | 0 | 0 | — |
case-20 | pass→pass | 7,156 | 4,464 | -38% | 1 | 1 | 0% | 1,365 | 3,289 | +141% | 0 | 0 | — |
case-21 | pass→pass | 9,015 | 10,251 | +14% | 1 | 1 | 0% | 1,790 | 3,955 | +121% | 0 | 0 | — |
case-22 | pass→pass | 4,972 | 3,795 | -24% | 1 | 1 | 0% | 898 | 3,025 | +237% | 0 | 0 | — |
case-23 | pass→pass | 4,007 | 5,243 | +31% | 1 | 1 | 0% | 722 | 3,263 | +352% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 23 cases were attempted. The headline lift of +17 percentage points is the difference between those two pass rates over the 23 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.