Size: 1486
Comment:
|
Size: 1185
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
Stata offers a set of macro features, each with slightly different behavior. |
|
Line 16: | Line 14: |
global my-vars var1 var2 display "$my-vars appears as 'var1 var2'" display "${my-vars} appears the same" |
global COHORT 1 generate number=${COHORT} generate string="${COHORT}" |
Line 20: | Line 19: |
To loop over words in a global macro, see [[Stata/Looping#Global_Macro_List|here]]. ---- |
|
Line 28: | Line 31: |
local my-vars var1 var2 display "`my-vars' appears as 'var1 var2'" |
local stringvars var1 var2 destring `stringvars', force |
Line 31: | Line 35: |
To loop over words in a local macro, see [[Stata/Looping#Local_Macro_List|here]]. Note that looping creates local macros to store the current word. These local macros are accessed in the same manner {{{ foreach v of local stringvars { destring `v', force } }}} ---- |
|
Line 34: | Line 50: |
=== Evaluation === | == Evaluation == |
Line 44: | Line 60: |
100 vs. _N }}} === Loops === Loops implicitly create local macros, which are accessed the same way. {{{ foreach var of varlist var* { display "I found `var' while looping through all variables matching 'var*'" } foreach var of local my-vars { display "I found `var' while looping though `my-vars'" } }}} == Scalars == Scalars are ''essentially'' (and often ''used as'') a global macro. The distinguishing behavior is seen in arithmetic. {{{ . local i=-2 . display `i'^2 -4 . display (`i')^2 4 . scalar j=-2 . display j^2 4 |
100 vs. 100 |
Macros
Contents
Globals
Global macros are declared with the global command. Generally these are discouraged, because their very nature (i.e. globally available) creates side-effects.
global COHORT 1 generate number=${COHORT} generate string="${COHORT}"
To loop over words in a global macro, see here.
Locals
Local macros are declared with the local command, and only exist in the local scope.
local stringvars var1 var2 destring `stringvars', force
To loop over words in a local macro, see here.
Note that looping creates local macros to store the current word. These local macros are accessed in the same manner
foreach v of local stringvars { destring `v', force }
Evaluation
If a macro definition includes an equals sign (=), then what follows is evaluated as an expression.
. local count1 = _N . local count2 _N . display "`count1' vs. `count2'" 100 vs. _N . display "`count1' vs. " `count2' 100 vs. 100