Stata Macros: local, global, and Extended Functions Explained
Macros are Stata's variable system for code. local vs global, backtick-apostrophe syntax, extended functions, and the patterns that make do-files readable.
Your do-file works in one section and fails in another because macros silently change scope.
You will control macro scope deliberately and stop losing hours to invisible expansion bugs.
All examples tested in Stata 18 SE. Compatible with Stata 15+.
Quick Answer
- Use local macros as default; reserve globals for explicit cross-script constants.
- Always print key macros during debugging.
- Use extended functions for robust varlist parsing.
- Avoid reusing macro names in nested scopes.
Use Macro Scope as a Reliability Tool
Define local macros for safe reusable templates
Locals reduce side effects by limiting visibility to the current do-file block or program. This makes scripts easier to reason about and review.
Pair locals with loops for concise, high-signal code.
If you are extending this pipeline, also review Export Regression Tables in Stata and Clustered Standard Errors in Stata.
1clear all2set obs 10003gen wage = 20 + rnormal(0,3)4gen education = 8 + floor(runiform()*10)5gen experience = 18 + floor(runiform()*20)67local controls "education experience"8local depvar "wage"910display "Running model: regress `depvar' `controls'"11regress `depvar' `controls'Running model: regress wage education experience
Use extended macro functions for dynamic varlists
Extended macro functions help build robust automation where variable sets change by sample or wave.
They are especially useful in large panel projects with evolving schemas.
1clear all2set obs 10003gen wage = 20 + rnormal(0,3)4gen education = 8 + floor(runiform()*10)5gen experience = 18 + floor(runiform()*20)67local controls "education experience"8local depvar "wage"910display "Running model: regress `depvar' `controls'"11regress `depvar' `controls'1213* ---- Section-specific continuation ----14ds wage education experience15local rawvars `r(varlist)'16local nvars : word count `rawvars'17display "Number of selected variables: `nvars'"1819* Build interaction-ready list20local rhs ""21foreach v of local rawvars {22 local rhs "`rhs' c.`v'"23}24display "RHS spec: `rhs'"RHS spec: c.wage c.education c.experience
Common Errors and Fixes
"invalid syntax"
Macro delimiters are malformed or expansion produced an empty fragment.
Display macro contents and verify backtick-apostrophe usage around macro names.
invalid syntax r(198);
display "'controls'"display "`controls'"1macro list2local controls "education experience"3display "`controls'"locals: controls: education experience
Command Reference
local / global macro
Stata docs โStores reusable text fragments for parameterized Stata commands.
localScope-limited macro definitionglobalSession-wide macro definitionmacro listDisplays defined macros: word countExtended macro function exampleHow Sytra Handles This
Sytra can scaffold macro-safe do-files with local scoping defaults and explicit debugging printouts.
A direct natural-language prompt for this exact workflow:
Refactor my do-file to use local macros for dependent variable, controls, and fixed-effects spec, then print macro expansions and run regressions with safe scope.Sytra catches these errors before you run.
Sytra can scaffold macro-safe do-files with local scoping defaults and explicit debugging printouts.
Join the Waitlist โFAQ
When should I use local instead of global macros?
Use local macros by default because they are scoped and safer. Use globals only for intentionally shared project-wide settings.
Why does my macro expand to empty text?
Likely scope mismatch or quoting error. Check backtick-apostrophe delimiters and whether macro exists in current scope.
Are extended macro functions worth using?
Yes for robust string manipulation of varlists and filenames, especially in reusable programs.
Related Guides
- Stata Loops: foreach and forvalues Tutorial with 20 Practical Examples
- Stata Factor Variables: i., c., ibn., and # Notation Explained
- Stata preserve/restore and tempvar: Safe Data Manipulation Patterns
- How to Structure a Stata Project: Directory Layout, Naming, and Automation
- Explore the workflow pillar page
- Open the full workflow guide index
- Browse all Stata & R guides on the blog index
- Browse all Stata pillars
We build practical, reproducible workflows for Stata and R teams working on real empirical research pipelines.