Size: 1966
Comment:
|
Size: 3010
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
Stata supports a '''macro''' programming language. |
|
Line 71: | Line 73: |
{{{ local cohorttype: type cohort if (strpos("`cohorttype'","str")>0) { destring cohort, replace } }}} ||'''Name''' ||'''Returns'''|| ||`properties` || || ||`results` || || ||`type` ||[[Stata/DataTypes|data type]] of a variable|| ||`format` || || ||`value label` || || ||`variable label`|| || ||`data label` || || ||`sortedby` || || ||`label` || || ||`constraint` || || ||`char` || || ||`permname` || || ||`adosubdir` || || ||`dir` || || ||`sysdir` || || ||`environment` || || ||`e()` || || ||`r()` || || ||`s()` || || ||`all globals` || || ||`all scalars` || || ||`all matrices` || || ||`display` || || ||`list` || || ||`rownames` || || ||`colnames` || || ||`rowfullnames` || || ||`colfullnames` || || ||`roweq` || || ||`coleq` || || ||`rownumb` || || ||`colnumb` || || ||`roweqnumb` || || ||`coleqnumb` || || ||`rownfreeparms` || || ||`colnfreeparms` || || ||`rownlfs` || || ||`colnlfs` || || ||`rowsof` || || ||`colsof` || || ||`rowvarlist` || || ||`colvarlist` || || ||`rowlfnames` || || ||`collfnames` || || ||`tsnorm` || || ||`copy local` || || ||`copy global` || || ||`word` || || ||`piece` || || ||`strlen` || || ||`ustrlen` || || ||`udstrlen` || || ||`subinstr` || || ---- |
|
Line 73: | Line 138: |
properties | |
Line 75: | Line 139: |
results | == See also == |
Line 77: | Line 141: |
type | [[https://www.stata.com/manuals/pmacro.pdf|Stata manual for macros]] |
Line 79: | Line 143: |
format | |
Line 81: | Line 144: |
value label variable label data label sortedby label constraint char permname adosubdir dir sysdir environment e() r() s() all globals all scalars all matrices display list rownames colnames rowfullnames colfullnames roweq coleq rownumb colnumb roweqnumb coleqnumb rownfreeparms colnfreeparms rownlfs colnlfs rowsof colsof rowvarlist colvarlist rowlfnames collfnames tsnorm copy local copy global word piece strlen ustrlen udstrlen subinstr |
Macros
Stata supports a macro programming language.
Local Macros
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 }
Global Macros
Global macros are declared with the global command. Otherwise, the grammar and rules are exactly the same as local macros.
global COHORT 1
Generally these are discouraged, because their very nature (i.e. globally available) creates side-effects.
To loop over words in a global macro, see here.
Expressions
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
Functions
If a macro definition includes a colon (:), then what follows is a macro function.
local cohorttype: type cohort if (strpos("`cohorttype'","str")>0) { destring cohort, replace }
Name |
Returns |
properties |
|
results |
|
type |
data type of a variable |
format |
|
value label |
|
variable label |
|
data label |
|
sortedby |
|
label |
|
constraint |
|
char |
|
permname |
|
adosubdir |
|
dir |
|
sysdir |
|
environment |
|
e() |
|
r() |
|
s() |
|
all globals |
|
all scalars |
|
all matrices |
|
display |
|
list |
|
rownames |
|
colnames |
|
rowfullnames |
|
colfullnames |
|
roweq |
|
coleq |
|
rownumb |
|
colnumb |
|
roweqnumb |
|
coleqnumb |
|
rownfreeparms |
|
colnfreeparms |
|
rownlfs |
|
colnlfs |
|
rowsof |
|
colsof |
|
rowvarlist |
|
colvarlist |
|
rowlfnames |
|
collfnames |
|
tsnorm |
|
copy local |
|
copy global |
|
word |
|
piece |
|
strlen |
|
ustrlen |
|
udstrlen |
|
subinstr |
|