Differences between revisions 1 and 7 (spanning 6 versions)
Revision 1 as of 2021-03-16 02:09:28
Size: 1486
Comment:
Revision 7 as of 2023-06-09 18:04:54
Size: 3010
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Stata offers a set of macro features, each with slightly different behavior. Stata supports a '''macro''' programming language.
Line 11: Line 11:
== 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 my-vars var1 var2
display "$my-vars appears as 'var1 var2'"
display "${my-vars} appears the same"
}}}



== Locals ==
== Local Macros ==
Line 28: Line 16:
local my-vars var1 var2
display "`my-vars' appears as 'var1 var2'"
local stringvars var1 var2

destring `stringvars', force
Line 31: Line 20:

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 35:
=== Evaluation === == 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 [[Stata/Looping#Global_Macro_List|here]].

----




== Expressions ==
Line 44: Line 62:
100 vs. _N 100 vs. 100
Line 47: Line 65:
----
Line 48: Line 67:
=== Loops ===
Line 50: Line 68:
Loops implicitly create local macros, which are accessed the same way.
== Functions ==

If a macro definition includes a colon (`:`), then what follows is a macro function.
Line 53: Line 74:
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'"
local cohorttype: type cohort
if (strpos("`cohorttype'","str")>0) {
  destring cohort, replace
Line 62: Line 80:
||'''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 64: Line 138:
== Scalars ==
Line 66: Line 139:
Scalars are ''essentially'' (and often ''used as'') a global macro. The distinguishing behavior is seen in arithmetic. == See also ==
Line 68: Line 141:
{{{
. local i=-2
. display `i'^2
-4
. display (`i')^2
4
. scalar j=-2
. display j^2
4
}}}
[[https://www.stata.com/manuals/pmacro.pdf|Stata manual for macros]]

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


See also

Stata manual for macros


CategoryRicottone

Stata/Macros (last edited 2023-07-28 21:22:48 by DominicRicottone)