Size: 3537
Comment:
|
Size: 652
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
## page was renamed from SPSS/Macros = SPSS Macros = |
= SPSS Macro Language = |
Line 4: | Line 3: |
[[SPSS]] macros are fundamentally a form of string manipulation. | '''The SPSS Macro Language''' is a programmatic interface to string manipulation. |
Line 6: | Line 5: |
See also [[SPSS/MacroStringFunctions|Macro String Functions]] and [[SPSS/MacroArithmetic|Macro Arithmetic]] for documentation on specialized use of macros. | See also and for documentation on specialized use of macros. |
Line 14: | Line 13: |
== Arguments == Arguments can be passed into a macro. Be mindful that they will first be tokenized before being exposed to the macro expansion. ||'''Function''' ||'''Behavior''' || ||`!tokens(N)` ||Consume N tokens || ||`!charend('C')` ||Consume all tokens until C || ||`!enclose('S','E')`||Consume all tokens starting from S until E || ||`!cmdend` ||Consume all tokens until the end of the command|| === Named Arguments === Tokens can be set into named arguments using a `key = function` pattern. |
== Example == |
Line 31: | Line 16: |
define !generate(var1=!tokens(1) /var2=!tokens(1)) compute !var1=0. compute !var2=0. !enddefine. !generate var1=foo var2=bar. !generate var2=bar var1=foo. |
define !HelloWorld() string HelloWorld (A13). compute HelloWorld="Hello, World!". list HelloWorld. enddefine. |
Line 40: | Line 22: |
Note that the arguments are accessed in the program by prepending the name with an exclamation mark (`!`). Also note that arguments can be specified in any order. === Positional Arguments === Tokens can also be set into positional indices (`!1`, `!2`, and so on) using the `!POSITIONAL` keyword. {{{ define !generate(!positional !tokens(1) /!positional !tokens(1)) compute !1=0. compute !2=0. !enddefine. !generate foo bar. }}} All positional arguments can be accessed as a blank-delimited string with `!*`. === Defaults === |
|
Line 71: | Line 27: |
== Conditional Processing == | == Language == |
Line 73: | Line 29: |
The `!IF` ... `!THEN` ... `!IFEND` syntax is used for conditional processing inside a macro. Additional conditions can be expressed with `!IF !ELSE` or `!ELSE`. {{{ define !export(type = !tokens(1)) !IF (!type !EQ "xlsx") !THEN save translate /outfile="export.xlsx" /type=xlsx /version=12 /map /replace /fieldnames /cells=values. !ELSE !IF (!type !EQ "dta") !THEN save translate /outfile="export.dta" /type=stata /version=8 /edition=se /map /replace. !ELSE save outfile="export.sav". !IFEND !enddefine }}} Inside the `!IF` construct, string literals ''can'' be left unquoted. This comes with the unexpected side-effect of case-sensitivity. === Operators === The following operators are valid inside conditions. ||'''Operator''' || ||`!EQ`, `=` || ||`!NE`, `~=` || ||`!GT`, `>` || ||`!GE`, `>=` || ||`!LT`, `<` || ||`!LE`, `<=` || ||`!OR`, `|` || ||`!AND`, `&` || ||`!NOT`, `~` || ---- == Loops == === Ranges === {{{ define !per_year(var = !tokens(1) /timevar = !tokens(1) /start = !tokens(1) /end = !tokens(1)) !DO !y = !start !TO !end !BY 1 temporary. select if !timevar=!y. frequencies /variables=!var. execute. !DOEND !enddefine !per_year var=population timevar=year start=2000 end=2008. }}} === Iteration === {{{ define !each_year(var = !tokens(1) /timevar = !tokens(1) /years = !cmdend) !DO !y !IN (!years) temporary. select if !timevar=!y. frequencies /variables=!var. execute. !DOEND !enddefine !each_year var=population timevar=year years=2000 2002 2008. }}} |
* [[SPSS/Macro/Arguments|Macro Arguments]] * [[SPSS/Macro/Arithmetic|Macro Arithmetic]] * [[SPSS/Macro/Logic|Macro Logic]] * [[SPSS/Macro/Looping|Macro Looping]] * [[SPSS/Macro/StringFunctions|Macro String Functions]] |
SPSS Macro Language
The SPSS Macro Language is a programmatic interface to string manipulation.
See also and for documentation on specialized use of macros.
Contents
Example
define !HelloWorld() string HelloWorld (A13). compute HelloWorld="Hello, World!". list HelloWorld. enddefine.