Overview (DEFINE-!ENDDEFINE command)

DEFINE—!ENDDEFINE defines a program macro, which can then be used within a command sequence. A macro can be useful in several different contexts. For example, it can be used to:

A macro is defined by specifying any part of a valid command and giving it a macro name. This name is then specified in a macro call within a command sequence. When the program encounters the macro name, it expands the macro.

In the examples of macro definition throughout this reference, the macro name, body, and arguments are shown in lowercase for readability. Macro keywords, which are always preceded by an exclamation point (!), are shown in uppercase.

Options

Macro Arguments. You can declare and use arguments in the macro definition and then assign specific values to these arguments in the macro call. You can define defaults for the arguments and indicate whether an argument should be expanded when the macro is called. See the topic Macro Arguments (DEFINE-!ENDDEFINE command) for more information.

Macro Directives. You can turn macro expansion on and off. See the topic Macro Directives (DEFINE-!ENDDEFINE command) for more information.

String Manipulation Functions. You can process one or more character strings and produce either a new character string or a character representation of a numeric result. See the topic String Manipulation Functions (DEFINE-!ENDDEFINE command) for more information.

Conditional Processing. You can build conditional and looping constructs. See the topic Conditional Processing (DEFINE-!ENDDEFINE command) for more information.

Macro Variables. You can directly assign values to macro variables See the topic Direct Assignment of Macro Variables (DEFINE-!ENDDEFINE command) for more information.

Basic Specification

All macros must start with DEFINE and end with !ENDDEFINE. These commands identify the beginning and end of a macro definition and are used to separate the macro definition from the rest of the command sequence.

To invoke the macro, issue a macro call in the command sequence. To call a macro, specify the macro name and any necessary arguments. If there are no arguments, only the macro name is required.

Operations

Syntax Rules

Just like other commands, expanded macros must adhere to the rules of the processing mode under which they are run. While it is desirable to create macro syntax that will run in both interactive and batch modes, this may sometimes add a layer of complexity that you may want to avoid. So we recommend that you write macro syntax that adheres to interactive syntax rules and structure your jobs to execute macro syntax under interactive syntax rules.

The macro body cannot contain the characters \ or ^ unless they are contained within a quoted string, as in "a valid backslash \ in a quoted string".

Example

DEFINE !macro1(type = !DEFAULT(1) !TOKENS(1)
                             /varlist=!CMDEND)
!IF (!type = 1)!THEN
frequencies variables=!varlist.
!ELSE
descriptives variables=!varlist.
!IFEND
!ENDDEFINE.

To structure your command syntax jobs so that interactive processing rules are always used instead of batch processing rules:

Compatibility

Improvements to the macro facility may cause errors in jobs that previously ran without errors. Specifically, for syntax that is processed with interactive rules, if a macro call occurs at the end of a command, and there is no command terminator (either a period or a blank line), the next command after the macro expansion will be interpreted as a continuation line instead of a new command, as in:

DEFINE !macro1()
var1 var2 var3
!ENDDEFINE.
FREQUENCIES VARIABLES = !macro1
DESCRIPTIVES VARIABLES = !macro1.

In interactive mode, the DESCRIPTIVES command will be interpreted as a continuation of the FREQUENCIES command, and neither command will run.

Limitations