A macro is a name that holds a value. Think of it as a container that stores text, numbers, or expressions that can be reused throughout your code.
local age 25
global study "Heart Disease Research"When referenced, macros substitute their stored values: age becomes 25, study becomes "Heart Disease Research".
1. Code Reusability
local varlist "age gender income education"
summarize `varlist'
regress outcome `varlist'2. Dynamic Programming
local today = date("`c(current_date)'", "DMY")
local filename "analysis_`today'.dta"
save "`filename'"3. Error Prevention
global datapath "/Users/researcher/data"
use "$datapath/study1.dta" // No typos in long paths4. Conditional Logic
local n = _N
if `n' < 100 {
display "Warning: Small sample size (`n')"
}Local macros (local name value) - Use for temporary, program-specific values:
local controls "age gender income"
local method "robust"
regress outcome treatment `controls', `method'Global macros (global name value) - Use sparingly for project-wide constants:
global project_dir "/Users/researcher/cardiology_study"
global outcome_vars "systolic diastolic cholesterol"// Good: Descriptive names
local baseline_controls "age gender race education"
// Bad: Cryptic names
local x "age gender race education"
// Good: Clear structure
local model1_vars "treatment `baseline_controls'"
local model2_vars "`model1_vars' comorbidities"// String macro
local filename "data_analysis"
// Numeric macro
local sample_size 1000
// Expression macro
local mean_age = (25 + 30 + 35) / 3
// List macro
local covariates age gender income education
// Command result macro
local observations = _N// Local macro reference
`macroname'
// Global macro reference
$macroname or ${macroname}
// Example usage
local city "Baltimore"
display "Welcome to `city'" // Output: Welcome to Baltimore
global state "Maryland"
display "State: $state" // Output: State: Marylandlocal author "Dr. Smith"
global significance_level 0.05local today "`c(current_date)'"
local stata_version "`c(stata_version)'"
local username "`c(username)'"summarize age
local mean_age = r(mean)
local sd_age = r(sd)regress income education experience
local r_squared = e(r2)
local n_obs = e(N)count if missing(income)
local missing_income = r(N)
local pct_missing = (`missing_income' / _N) * 100return list - The Immediate Gratification"What did my last command just tell me?"
tabulate gender, chi2
return listReturns: Fresh results from the most recent command
r(N) - Sample sizer(chi2) - Test statisticr(p) - P-valueThink: Your command's immediate confession booth.
creturn list - The System Philosopher"What does Stata know about itself and the world?"
creturn listReturns: Stata's existential constants
c(current_date) - Today's datec(username) - Who you arec(pwd) - Where you arec(stata_version) - What version of enlightenment you're runningThink: Stata's introspective diary of universal truths.
ereturn list - The Statistical Historian"What did my last estimation procedure discover?"
regress income education experience
ereturn listReturns: The statistical archaeological record
e(N) - Observations usede(r2) - R-squarede(b) - Coefficient vectore(V) - Variance-covariance matrixThink: Your model's permanent statistical DNA.
// The Trinity in Action
regress income education // Creates e() results
summarize income // Creates r() results
display "`c(username)'" // Accesses c() constants
// Each serves different masters:
local recent_mean = r(mean) // From summarize
local model_rsq = e(r2) // From regress
local analyst = "`c(username)'" // From systemMnemonic:
Pro Tip: Like Russian nesting dolls, each new command overwrites r(), but e() persists until the next estimation, and c() is eternal (within session).
// Define
local name "value"
global name "value"
// Reference
`localname'
$globalname
// Capture results
local n = r(N) // From return list
local user "`c(username)'" // From creturn list
local rsq = e(r2) // From ereturn list
// Display
macro list // Show all macros
macro drop _all // Clear all macros