Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2023-01-13 20:55:24
Size: 1687
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 2: Line 2:

'''Arguments''' to SPSS macros need to be explicitly parsed on the macro definition.
Line 9: Line 11:
== Macro Definition ==

Arguments are passed using a macro function that is included on the macro definition itself.
== 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 27: Line 30:
Note that arguments are tokenized before being exposed to the macro expansion. Note that arguments are tokenized ''before'' being exposed to the macro expansion.
Line 33: Line 36:
== Named Arguments == == Usage ==
Line 35: Line 38:
Arguments can be set into named variables using a `key = function` pattern.

{{{
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.
}}}

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.
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 56: Line 44:
== Positional Arguments == === Named Arguments ===
Line 58: Line 46:
Arguments can be set into positional variables (`!1`, `!2`, and so on) using the `!POSITIONAL` keyword. Named arguments are accessed by their name prefixed with an exclamation mark (`!`).
Line 61: Line 49:
define !generate(!positional !tokens(1)
                /!positional !tokens(1))
  compute !1=0.
  compute !2=0.
define !foo(bar=!tokens(1)
           /baz=!tokens(1))
  compute !bar=0.
  compute !baz=0.
Line 67: Line 55:
!generate foo bar. !foo bar=x baz=y.
Line 70: Line 58:
All positional arguments can be accessed as a blank-delimited string with `!*`. In this example, the variables `x` and `y` will be created.
Line 76: Line 64:
== Default Values == === 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 ===

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)