Content is user-generated and unverified.

Stata Macros: The Essential Guide

What is a Macro?

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.

stata
local age 25
global study "Heart Disease Research"

When referenced, macros substitute their stored values: age becomes 25, study becomes "Heart Disease Research".


Why Are They Important?

1. Code Reusability

stata
local varlist "age gender income education"
summarize `varlist'
regress outcome `varlist'

2. Dynamic Programming

stata
local today = date("`c(current_date)'", "DMY")
local filename "analysis_`today'.dta"
save "`filename'"

3. Error Prevention

stata
global datapath "/Users/researcher/data"
use "$datapath/study1.dta"  // No typos in long paths

4. Conditional Logic

stata
local n = _N
if `n' < 100 {
    display "Warning: Small sample size (`n')"
}

How Should We Use Macros?

Local vs Global Strategy

Local macros (local name value) - Use for temporary, program-specific values:

stata
local controls "age gender income"
local method "robust"
regress outcome treatment `controls', `method'

Global macros (global name value) - Use sparingly for project-wide constants:

stata
global project_dir "/Users/researcher/cardiology_study"
global outcome_vars "systolic diastolic cholesterol"

Best Practices

stata
// 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"

Components of a Macro

1. Name - The identifier

2. Value - What it contains

3. Scope - Where it's accessible (local vs global)

4. Type - How it's interpreted

stata
// 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

Reference Syntax

stata
// 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: Maryland

Where Do We Get These Macros?

1. Manual Definition

stata
local author "Dr. Smith"
global significance_level 0.05

2. System-Generated (c-class)

stata
local today "`c(current_date)'"
local stata_version "`c(stata_version)'"
local username "`c(username)'"

3. Command Results (r-class)

stata
summarize age
local mean_age = r(mean)
local sd_age = r(sd)

4. Estimation Results (e-class)

stata
regress income education experience
local r_squared = e(r2)
local n_obs = e(N)

5. Computed Values

stata
count if missing(income)
local missing_income = r(N)
local pct_missing = (`missing_income' / _N) * 100

The Triumvirate: A Clever-by-Half Overview

return list - The Immediate Gratification

"What did my last command just tell me?"

stata
tabulate gender, chi2
return list

Returns: Fresh results from the most recent command

  • r(N) - Sample size
  • r(chi2) - Test statistic
  • r(p) - P-value

Think: Your command's immediate confession booth.

creturn list - The System Philosopher

"What does Stata know about itself and the world?"

stata
creturn list

Returns: Stata's existential constants

  • c(current_date) - Today's date
  • c(username) - Who you are
  • c(pwd) - Where you are
  • c(stata_version) - What version of enlightenment you're running

Think: Stata's introspective diary of universal truths.

ereturn list - The Statistical Historian

"What did my last estimation procedure discover?"

stata
regress income education experience
ereturn list

Returns: The statistical archaeological record

  • e(N) - Observations used
  • e(r2) - R-squared
  • e(b) - Coefficient vector
  • e(V) - Variance-covariance matrix

Think: Your model's permanent statistical DNA.

The Memory Hierarchy

stata
// 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 system

Mnemonic:

  • r = Recent (last command)
  • c = Constants (system info)
  • e = Estimation (model results)

Pro Tip: Like Russian nesting dolls, each new command overwrites r(), but e() persists until the next estimation, and c() is eternal (within session).


Quick Reference Card

stata
// 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
Content is user-generated and unverified.
    Stata Macros: Terse Graduate Notes | Claude