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.
| Principle | Description | |-----------|-------------| | Reproducible | 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 |
* ==============================================================================
* 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
* 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
| 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 |
| Safe to abbreviate | Never abbreviate |
| ------------------- | ------------------ |
| gen, reg, lab, sum, tab | local, global, save, merge |
| bys, qui, noi, cap, forv | append, sort, drop, keep |
* 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 >= .
regress income ///
age i.education i.region ///
if (sample == 1), ///
vce(cluster village_id)
import delimited "$data/raw/survey.csv", clear varnames(1)
describe
codebook, compact
duplicates report respondent_id
duplicates tag respondent_id, gen(dup_flag)
* Investigate and resolve duplicates
isid respondent_id // Assert uniqueness
* Rename to convention
rename (q1 q2 q3) (resp_age resp_gender resp_education)
* Validate ranges
assert inrange(age, 0, 120) if !missing(age)
* Clean strings
replace name = strtrim(strproper(name))
label var resp_age "Respondent age in years"
label define gender_lbl 1 "Male" 2 "Female"
label values resp_gender gender_lbl
notes _dta: "Cleaned on `c(current_date)'"
compress
save "$data/clean/survey_clean.dta", replace
| Raw Code | Stata | Meaning |
| -------- | ------- | --------- |
| -99 | .d | Don't know |
| -98 | .r | Refused |
| -97 | .n | Not applicable |
| -96 | .s | Skipped |
| -95 | .o | Other missing |
* Using mvdecode (efficient)
mvdecode _all, mv(-99=.d \ -98=.r \ -97=.n \ -96=.s)
* Check missing patterns
misstable summarize
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
use "$data/clean/baseline.dta", clear
gen wave = 1
append using "$data/clean/endline.dta"
replace wave = 2 if missing(wave)
* Wide to long
reshape long income_, i(hhid) j(year)
rename income_ income
* Long to wide
reshape wide income, i(hhid) j(year)
* 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
list if !(condition)_merge distribution with tab _mergelist if _merge == 1 or _merge == 2use var1 var2 using "data.dta"quietly to suppress output in loopsmaxvar only when necessaryjust lint-stata # Lint all do-files
just lint-stata-file scripts/01.do # Lint specific file
ssc install ietoolkit // DIME tools
ssc install estout // Tables
ssc install fre // Frequencies
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer