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, ephemeralGlobal (
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 commande()
persists until the next regressionc()
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.