Skip to content

brycewang-stanford/Auto-Empirical-Research-Skills / skills/14-luischanci-claude-code-research-starter/dot-claude/skills/stata

Comprehensive Stata reference for writing correct .do files, data management, econometrics, causal inference, graphics, Mata programming, and 17+ 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.

FileKindSize
SKILL.mdskill md13.1 KB
packages/asdoc.mdmarkdown8.5 KB
packages/binsreg.mdmarkdown10.0 KB
packages/coefplot.mdmarkdown9.3 KB
packages/data-manipulation.mdmarkdown10.1 KB
packages/diagnostics.mdmarkdown15.3 KB
packages/did.mdmarkdown15.4 KB
packages/estout.mdmarkdown16.9 KB
packages/event-study.mdmarkdown29.1 KB
packages/graph-schemes.mdmarkdown14.1 KB
packages/ivreg2.mdmarkdown11.1 KB
packages/nprobust.mdmarkdown12.4 KB
packages/outreg2.mdmarkdown11.2 KB
packages/package-management.mdmarkdown10.2 KB
packages/psmatch2.mdmarkdown17.8 KB
packages/rdrobust.mdmarkdown13.4 KB
packages/reghdfe.mdmarkdown10.4 KB
packages/synth.mdmarkdown22.6 KB
packages/tabout.mdmarkdown12.1 KB
packages/winsor.mdmarkdown6.7 KB
packages/xtabond2.mdmarkdown14.9 KB
references/advanced-programming.mdmarkdown10.1 KB
references/basics-getting-started.mdmarkdown5.8 KB
references/bootstrap-simulation.mdmarkdown6.7 KB
references/data-import-export.mdmarkdown7.4 KB
references/data-management.mdmarkdown9.2 KB
references/date-time-functions.mdmarkdown6.8 KB
references/descriptive-statistics.mdmarkdown6.4 KB
references/difference-in-differences.mdmarkdown18.6 KB
references/external-tools-integration.mdmarkdown19.6 KB
references/gmm-estimation.mdmarkdown8.8 KB
references/graphics.mdmarkdown8.2 KB
references/limited-dependent-variables.mdmarkdown7.8 KB
references/linear-regression.mdmarkdown11.0 KB
references/machine-learning.mdmarkdown12.4 KB
references/mata-data-access.mdmarkdown6.8 KB
references/mata-introduction.mdmarkdown6.4 KB
references/mata-matrix-operations.mdmarkdown5.8 KB
references/mata-programming.mdmarkdown8.4 KB
references/matching-methods.mdmarkdown19.2 KB
references/mathematical-functions.mdmarkdown7.0 KB
references/maximum-likelihood.mdmarkdown16.6 KB
references/missing-data-handling.mdmarkdown18.2 KB
references/nonparametric-methods.mdmarkdown11.7 KB
references/panel-data.mdmarkdown7.7 KB
references/programming-basics.mdmarkdown7.9 KB
references/regression-discontinuity.mdmarkdown13.4 KB
references/sample-selection.mdmarkdown17.7 KB
references/sem-factor-analysis.mdmarkdown14.5 KB
references/spatial-analysis.mdmarkdown19.5 KB
references/string-functions.mdmarkdown8.7 KB
references/survey-data-analysis.mdmarkdown12.8 KB
references/survival-analysis.mdmarkdown12.3 KB
references/tables-reporting.mdmarkdown24.1 KB
references/time-series.mdmarkdown7.1 KB
references/treatment-effects.mdmarkdown23.1 KB
references/variables-operators.mdmarkdown4.9 KB
references/workflow-best-practices.mdmarkdown25.2 KB

references/basics-getting-started.md

View on GitHub
# Stata Basics: Getting Started Guide

## Basic Command Syntax

### Command Structure
```stata
[by varlist:] command [varlist] [=exp] [if exp] [in range] [weight] [, options]
```

### Key Syntax Rules

1. **Commands are case-sensitive for variable names** but not for command names
   ```stata
   summarize age    // Works
   SUMMARIZE age    // Also works
   summarize Age    // Different variable!
   ```

2. **Abbreviations** - Most commands can be abbreviated
   ```stata
   summarize age    // Full command
   sum age          // Abbreviated version
   ```

3. **Variable lists** can use wildcards and ranges
   ```stata
   describe age-income     // Variables from age to income
   describe age*           // All variables starting with "age"
   ```

4. **Comments**
   ```stata
   * This is a comment line
   summarize age  // Inline comment
   /* Multi-line comment */
   ```

5. **Line continuation** using `///`
   ```stata
   regress income age education ///
           experience gender
   ```

### if and in Qualifiers

```stata
summarize income if age > 30
list in 1/10              // First 10 observations
summarize income in 20/50 // Observations 20 through 50
```

---

## Data Types and Structures

### Numeric Storage Types
```stata
byte      // -127 to 100 (1 byte)
int       // -32,767 to 32,740 (2 bytes)
long      // -2,147,483,647 to 2,147,483,620 (4 bytes)
float     // ±10^38 (4 bytes, 6-7 digit precision)
double    // ±10^307 (8 bytes, 15-16 digit precision)
```

### String Variables
```stata
str1-str244  // Fixed-length strings (older format)
strL         // Variable-length strings (up to 2 billion characters)
```

### Variable Labels and Value Labels
```stata
label variable age "Age in years"
label define yesno 0 "No" 1 "Yes"
label values married yesno
```

### Dataset Structure
- **One dataset in memory** at a time (by default)
- **Rectangular format**: All observations have values (or missing) for all variables

---

## Essential Commands

### Data Input/Output
```stata
use "mydata.dta", clear
use age income using "mydata", clear  // Load specific variables
save "mydata.dta", replace
import excel "data.xlsx", firstrow clear
import delimited "data.csv", clear
export excel using "output.xlsx", firstrow(variables) replace
export delimited using "output.csv", replace
```

### Data Exploration
```stata
describe                      // All variables
describe, short               // Brief summary
summarize                     // All numeric variables
summarize age, detail         // Detailed stats (percentiles, etc.)
list in 1/10                  // First 10 observations
browse age income             // Data browser (read-only)
codebook, compact             // Compact variable info
tabulate gender               // One-way frequency table
tab gender married, row col   // Two-way cross-tabulation
```

### Data Modification
```stata
generate age_squared = age^2
generate high_income = (income > 50000)
replace income = income * 1.03
replace age = . if age < 0          // Set invalid ages to missing
rename oldname newname
rename (old1 old2) (new1 new2)      // Multiple renames
drop age                            // Drop a variable
drop if income < 0                  // Drop observations
keep age income education           // Keep only these variables
keep if age >= 18
recode age (0/17=1 "Child") (18/64=2 "Adult") (65/max=3 "Senior"), gen(age_group)
sort age
gsort -age                          // Descending order
```

### Data Management
```stata
clear all
append using "newdata.dta"
merge 1:1 id using "otherdata.dta"
merge m:1 country using "countrydata.dta"
reshape long inc, i(id) j(year)
compress                     // Optimize storage types
```

### Analysis Commands
```stata
correlate age income education
pwcorr age income education, star(0.05)
regress income age education experience
ttest income, by(gender)
```

### System and File Management
```stata
cd "/path/to/project"
pwd
dir *.dta
log using "analysis.log", replace
log close
```

---

## Working Directory Management

```stata
pwd                          // Print working directory
cd "/path/to/project"        // Change directory
cd ..                        // Go up one directory
dir *.dta                    // List Stata datasets
```

**Best Practice**: Use global macros for project paths:
```stata
global projdir "/path/to/project"
cd "$projdir"
use "$projdir/data/mydata.dta"
```

Recommended project structure:
```
MyProject/
├── data/          (raw data)
├── dofiles/       (analysis scripts)
├── results/       (output datasets)
├── logs/          (log files)
└── graphs/        (figures)
```

---

## Getting Help

```stata
help summarize               // Help for specific command
search "panel data"          // Search for topics
findit "robust regression"   // Search including user-written programs
ssc install commandname      // Install user-written commands
adoupdate, update            // Update all packages
```

---

## Common Pitfalls

### Not Using `clear` Before Loading Data
```stata
use "newdata.dta", clear    // Include clear to avoid errors
```

### Confusing = and ==
```stata
generate male = 1 if gender == 1     // Correct (= assigns, == compares)
```

### Ignoring Missing Values
```stata
* Missing values are treated as very large positive numbers
summarize age if !missing(age)  // Exclude missing
```

### Preserving Original Data
```stata
preserve
... do operations ...
restore                     // Returns to original state
```

### Missing Value Codes
| Code | Meaning |
|------|---------|
| `.` | Generic missing |
| `.a` to `.z` | Extended missing (27 types) |
| `""` | String missing |

### Operators Reference
| Operator | Meaning |
|----------|---------|
| `==` | Equal |
| `!=` or `~=` | Not equal |
| `>`, `<`, `>=`, `<=` | Comparisons |
| `&` | And |
| `|` | Or |
| `!` | Not |
| `+`, `-`, `*`, `/`, `^` | Arithmetic |
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall