2. โœ๏ธ Macros and the Art of Memory#


๐ŸŒ€ What is a Macro?#

A macro is not just a variableโ€”itโ€™s a name-bound spell, a container of intent. It stores strings, numbers, lists, or expressions for later invocation.

local age 25
global study "Heart Disease Research"

When referenced, a macro doesnโ€™t executeโ€”it whispers its contents into the void.

  • age โ‡’ 25

  • study โ‡’ "Heart Disease Research"

Think: Macros = reusable echoes of thought.


โค๏ธ Why Are Macros Sacred?#

1. ๐Ÿ” Code Reusability#

Write once, invoke forever.

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

2. ๐Ÿ•ฐ๏ธ Dynamic Temporal Logic#

Macros adapt to the present moment.

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

3. ๐Ÿ›ก๏ธ Error Prevention#

Macros protect you from typos and repetition.

global datapath "/Users/researcher/data"
use "$datapath/study1.dta"

4. โš–๏ธ Conditional Elegance#

Even logic can be laced with grace.

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

๐Ÿง  How to Use Macros with Discipline#

Local vs Global: Scope is Philosophy#

  • Local (local name value) โ†’ Context-bound, ephemeral

  • Global (global name value) โ†’ Environment-wide, persistent

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

global project_dir "/Users/researcher/cardiology_study"

Use local unless you must go global. Treat globals like saltโ€”not sugar.


๐ŸŽฏ Macro Style: Clean Code is Clean Thought#

// โœ… Good
local baseline_controls "age gender race education"
local model1_vars "treatment `baseline_controls'"
local model2_vars "`model1_vars' comorbidities"

// โŒ Bad
local x "age gender race education"

Rule of thumb: If future-you canโ€™t read it, rewrite it now.


๐Ÿงฉ Components of a Macro#

1. Name โ€” The handle#

2. Value โ€” The contents#

3. Scope โ€” Local or global#

4. Type โ€” String, number, list, expression#

local filename "data_analysis"
local sample_size 1000
local mean_age = (25 + 30 + 35)/3
local covariates age gender income
local n_obs = _N

๐Ÿ“Œ Referencing Rules#

// Local
`macroname'

// Global
$macroname or ${macroname}

// Example
local city "Baltimore"
display "Welcome to `city'"     // Welcome to Baltimore

global state "Maryland"
display "State: $state"         // State: Maryland

๐Ÿ” Macro Origins: Where They Come From#

1. โœ๏ธ Manual Definition#

local author "Dr. Smith"
global alpha 0.05

2. ๐Ÿ“œ System Constants (c() class)#

local today "`c(current_date)'"
local version "`c(stata_version)'"

3. ๐Ÿ“ฆ Command Results (r() class)#

summarize age
local mean_age = r(mean)

4. ๐Ÿ“Š Estimation Results (e() class)#

regress income education
local rsq = e(r2)

5. ๐Ÿงฎ Computed On-the-Fly#

count if missing(income)
local missing_pct = (r(N)/_N) * 100

๐Ÿงญ The Triumvirate: return, creturn, and ereturn#

๐Ÿ” return list โ€” The Momentโ€™s Echo#

โ€œWhat just happened?โ€

tab gender, chi2
return list

Think: Transient truthโ€”r() holds the results of the last command.


๐Ÿง˜ creturn list โ€” The Introspective Core#

โ€œWho am I? Where am I?โ€

creturn list

Think: System-level constantsโ€”c() is the self-knowledge of Stata.


๐Ÿ“œ ereturn list โ€” The Archive of Estimation#

โ€œWhat did the model discover?โ€

regress income education
ereturn list

Think: e() is the historical ledger of your estimation journey.


๐Ÿ—‚๏ธ Triumvirate in Motion#

regress income education      // e()
summarize income              // r()
display "`c(username)'"      // c()

local mean = r(mean)
local rsq = e(r2)
local user = "`c(username)'"

๐Ÿง  Mnemonic: r = Recent c = Constants e = Estimation

Hierarchy of Memory:

  • r() dies with the next command

  • e() persists until the next regression

  • c() is eternal (until reboot)


๐Ÿ—บ๏ธ Macro Quick Reference โ€” The Portable Codex#

// Define
local name "value"
global name "value"

// Use
`localname'
$globalname

// Capture Results
local mean = r(mean)
local user = "`c(username)'"
local rsq = e(r2)

// Inspect
macro list

// Reset
macro drop _all

๐Ÿง˜ Final Word#

โ€œA macro is a mnemonic mirrorโ€”a reflection of the logic youโ€™ll return to tomorrow. So make it readable. Make it sing.โ€

Let this guide be both a keyboard map and a philosophical compass as you wander deeper into the forest of Stata. The language may be rigid, but your use of it doesnโ€™t have to be.