Differences between revisions 3 and 4
Revision 3 as of 2023-06-14 15:31:19
Size: 1695
Comment:
Revision 4 as of 2023-06-14 15:42:38
Size: 1909
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
== Usage == == Declaration ==
Line 14: Line 14:
define !foo(bar=!cmdend)
  compute baz=!bar.
define !foo(variables=!cmdend)
  compute foo=!variables.
Line 17: Line 17:

!foo variables=bar baz.
Line 22: Line 24:
||`!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||
||`!CHAREND('C')` ||Consume all tokens until `C` ||
||`!CMDEND` ||Consume all tokens until the end of the command||
||`!ECNLOSE('S','E')`||Consume all tokens starting from `S` until `E` ||
||`!TOKENS(N)` ||Consume `N` tokens ||
||`!POSITIONAL` ||Used with one of the above functions ||
Line 35: Line 38:
If an argument is bound to a name, it becomes a '''named argument'''. If the `!POSITIONAL` function is used, it becomes a '''positional argument'''.

----
Line 39: Line 46:
Arguments can be set into named variables using a `key = function` pattern. Named arguments are accessed by their name prefixed with an exclamation mark (`!`).
Line 42: Line 49:
define !generate(var1=!tokens(1)
                /var2=!tokens(1))
  compute !var1=0.
  compute !var2=0.
define !foo(bar=!tokens(1)
           /baz=!tokens(1))
  compute !bar=0.
  compute !baz=0.
Line 48: Line 55:
!generate var1=foo var2=bar.
!generate var2=bar var1=foo.
!foo bar=x baz=y.
Line 52: Line 58:
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.
In this example, the variables `x` and `y` will be created.
Line 62: Line 66:
Arguments can be set into positional variables (`!1`, `!2`, and so on) using the `!POSITIONAL` keyword. Positional arguments are set into sequential integer names prefixed with an exclamation mark (`!`) (i.e. `!1`, `!2`, and so on).
Line 65: Line 69:
define !generate(!positional !tokens(1)
                /!positional !tokens(1))
define !foo(!positional !tokens(1)
           /!positional !tokens(1))
Line 71: Line 75:
!generate foo bar. !foo x y.
Line 74: Line 78:
All positional arguments can be accessed as a blank-delimited string with `!*`. In this example also, the variables `x` and `y` will be created.

The entire series of positional arguments as a space-delimited string can be accessed as `!*`.

SPSS Macro Arguments

Arguments to SPSS macros need to be explicitly parsed on the macro definition.


Declaration

define !foo(variables=!cmdend)
  compute foo=!variables.
enddefine.

!foo variables=bar baz.

The available functions are:

Function

Behavior

!CHAREND('C')

Consume all tokens until C

!CMDEND

Consume all tokens until the end of the command

!ECNLOSE('S','E')

Consume all tokens starting from S until E

!TOKENS(N)

Consume N tokens

!POSITIONAL

Used with one of the above functions

Note that arguments are tokenized before being exposed to the macro expansion.


Usage

If an argument is bound to a name, it becomes a named argument. If the !POSITIONAL function is used, it becomes a positional argument.


Named Arguments

Named arguments are accessed by their name prefixed with an exclamation mark (!).

define !foo(bar=!tokens(1)
           /baz=!tokens(1))
  compute !bar=0.
  compute !baz=0.
!enddefine.

!foo bar=x baz=y.

In this example, the variables x and y will be created.


Positional Arguments

Positional arguments are set into sequential integer names prefixed with an exclamation mark (!) (i.e. !1, !2, and so on).

define !foo(!positional !tokens(1)
           /!positional !tokens(1))
  compute !1=0.
  compute !2=0.
!enddefine.

!foo x y.

In this example also, the variables x and y will be created.

The entire series of positional arguments as a space-delimited string can be accessed as !*.


Default Values


CategoryRicottone

SPSS/Macro/Arguments (last edited 2023-06-14 15:42:38 by DominicRicottone)