# An example of using a MACRO to make a custom data transformation function in SPSS

MACROS in SPSS are ways to make custom functions. They can either accomplish very simple tasks, as I illustrate here, or can wrap up large blocks of code. If you pay attention to many of my SPSS blog posts, or the NABBLE SPSS forum you will see a variety of examples of their use. They aren’t typical fodder though for introductory books in SPSS, so here I will provide a very brief example and refer those interested to other materials.

I was reading Gelman’s and Hill’s Data Analysis Using Regression and Multilevel/Hierarchical Models and for their chapter on Logistic regression they define a function in R, `invlogit`, to prevent the needless repetition of writing `1/1 + (exp(-x))` (where `x` is some arbitrary value or column of data) when transforming predictions on the logit scale to the probability scale. We can do the same in SPSS with a custom macro.

``````
DEFINE !INVLOGIT (!POSITIONAL  !ENCLOSE("(",")") )
1/(1 + EXP(-!1))
!ENDDEFINE.
``````

To walk one through the function, an SPSS macro defintion starts with a `DEFINE` statement, and ends with `!ENDDEFINE.` In between these are the name of the custom function, `!INVLOGIT`, and the parameters the function will take within parentheses. This function only takes one parameter, defined as the first argument passed after the function name that is enclosed within parentheses, `!POSITIONAL !ENCLOSE("(",")")`.

After those statements comes the functions the macro will perform. Here it is just a simple data transformation, `1/(1 + EXP(-!1))`, and `!1` is where the argument is passed to the function. The `POSITIONAL` key increments if you use mutliple `!POSITIONAL` arguments in a macro call, and starts at `!1`. The enclose statement says the value that will be passed to `!1` will be contained within a left and right parenthesis.

When the MACRO is called, by typing `!INVLOGIT(x)` for example, it will then expand to the SPSS syntax `1/(1 + EXP(-x))`, where the `!1` is replaced by `x`. I could pass anything here though within the parenthesis, like a constant value or a more complicated value such as `(x+5)/3*1.2`. To make sense you only need to provide a numeric value. The macro is just a tool to that when expanded writes SPSS code with the arbitrary arguments inserted.

Below is a simple use example case. One frequent mistake of beginners is to not expand the macro call in the text log using `SET MPRINT ON.` to debug incorrect code, so the code includes that as an example (and uses `PRESERVE.` and `RESTORE.` to keep the your intial settings).

``````
DEFINE !INVLOGIT (!POSITIONAL  !ENCLOSE("(",")") )
1/(1 + EXP(-!1))
!ENDDEFINE.

data list free / x.
begin data
0.5
1
2
end data.

PRESERVE.
SET MPRINT ON.
compute logit = !INVLOGIT(x).
RESTORE.
*you can pass more complicated arguments since.
*they are enclosed within parentheses.
compute logit2 = !INVLOGIT(x/3).
compute logit3 = !INVLOGIT((x+5)/3*1.2).
compute logit4 = !INVLOGIT(1).
EXECUTE.
``````

Like all SPSS transformation statements, the `INVLOGIT` transformation is not sensitive to case (e.g. you could write `!InvLogit(1)` or `!invlogit(1)` and they both would be expanded). It is typical practice to write custom macro functions with a leading exclamation mark not because it is necessary, but to clearly differentiate them from native SPSS functions. Macros can potentially be expanded even when in `*` marked comments (but will not be expanded in `/* */` style comments), so I typically write macro names excluding the exclamation in comments and state something along the lines of `*replace the * with a ! to run the macro.`. Here I intentially write the macro to look just like an SPSS data transformation that only takes one parameter and is enclosed within parentheses. Also I do not call the execute statement in the macro, so just like all data transformations this is not immediately performed.

This is unlikely to be the best example case for macros in SPSS, but I merely hope to provide more examples to the unfamiliar. Sarah Boslaugh’s An Intermediate Guide to SPSS Programming has one of the simplest introductions to macros in SPSS you can find. Also this online tutorial has some good use examples of using loops and string functions to perform a variety of tasks with macros. Of course viewing Raynald’s site of SPSS syntax examples provides a variety of use cases in addition to the programming and data management guide that comes with SPSS.

1. #### Jon Peck

/  June 26, 2013

The macro syntax was designed specifically to fit the structure of SPSS syntax, and it is a built-in part of the Statistics package going back many years, but it is severely limited in what it can do, since it works by static text substitution. For anything beyond very simple uses, going the route of Python programmability is far preferable. And as a bonus, the Python plugin includes an api that can create a macro value for use in regular syntax 🙂

• #### apwheele

/  June 27, 2013

Totally agree Jon, use the right tools for the right job. Hopefully this is a good example of where a macro function is sufficient for the job.