name: stata
description: This skill should be used when users need to write, review, or debug Stata code for data cleaning and analysis. Use this skill for tasks involving data import, variable management, data documentation, merging/appending datasets, creating analysis variables, and following IPA/DIME Analytics coding standards. This skill should be invoked when working with .do files, .dta files, or any Stata-related data processing tasks.
Code produces identical outputs when run multiple times
Defensive
Assert statements verify data meets expected conditions
Documented
Comments explain why decisions were made, not just what
No PII
Never process personally identifiable information with AI tools
Four-Stage Data Flow
Import - Combine data into Stata format, apply corrections, remove duplicates
Deidentify - Remove PII as early as possible
Clean - Standardize formats, verify consistency
Construct - Build analysis variables through merging/appending
Project Configuration
Do-File Header
* ==============================================================================
* Project: [Project Name]
* Purpose: [Brief description]
* Author: [Name]
* Created: [Date]
* ==============================================================================
clear all
set more off
version 17.0
set maxvar 5000 // Increase only if genuinely needed
Path Setup
* Define paths in master do-file (use forward slashes)
global data "$root/data"
global output "$root/output"
* Usage - always use globals, never cd
use "$data/raw/survey.dta", clear
save "$data/clean/survey_clean.dta", replace
Coding Standards Quick Reference
Variable Naming
Prefix
Meaning
Example
hh_
Household
hh_income
ind_
Individual
ind_age
bl_/el_
Baseline/Endline
bl_score
d_
Dummy/indicator
d_employed
n_
Count
n_children
Command Abbreviations
Safe to abbreviate
Never abbreviate
gen, reg, lab, sum, tab
local, global, save, merge
bys, qui, noi, cap, forv
append, sort, drop, keep
Conditionals
* Good - explicit and clear
replace status = 1 if (employed == 1) & !missing(income)
drop if missing(respondent_id)
* Bad - implicit or unclear
replace status = 1 if employed & income
drop if respondent_id >= .
Line Breaking
regress income ///
age i.education i.region ///
if (sample == 1), ///
vce(cluster village_id)
use "$data/clean/household.dta", clear
count
local pre_merge = r(N)
merge 1:1 hhid using "$data/admin/treatment.dta"
tab _merge
assert _merge != 2 // No unmatched using expected
keep if _merge == 3
drop _merge
Appending
use "$data/clean/baseline.dta", clear
gen wave = 1
append using "$data/clean/endline.dta"
replace wave = 2 if missing(wave)
Reshaping
* Wide to long
reshape long income_, i(hhid) j(year)
rename income_ income
* Long to wide
reshape wide income, i(hhid) j(year)
Quality Checks
* Summary statistics
summarize, detail
tabstat income expenditure, stats(n mean sd min max)
* Outlier detection
egen income_std = std(income)
list hhid income if abs(income_std) > 3
* Cross-tabulation consistency
tab gender pregnant, missing
assert pregnant == . | pregnant == 0 if gender == 1
Troubleshooting
Assert Failures
Examine failing observations: list if !(condition)
Check for unexpected missing values
Verify data source and transformations
Document exceptions if valid
Merge Issues
Check _merge distribution with tab _merge
Investigate unmatched: list if _merge == 1 or _merge == 2
Verify key variable types match (string vs numeric)
Check for leading/trailing spaces in string keys
Performance
Load only needed variables: use var1 var2 using "data.dta"