SPSS Macro Guide

Tutorial sections and descriptions:

  • WHAT IS AN SPSS MACRO? 
    An overview of what macros do and how they function
  • WHEN MIGHT I USE AN SPSS MACRO? 
    Some examples of applications
  • USING MACROS 
    How to call an existing macro

What Is an SPSS Macro?

An SPSS macro functions as a “mini program” within the syntax of SPSS. These mini programs are written in a combination of a special SPSS macro language and the familiar SPSS syntax language. Similar to other programs, macros serve to “automate” a series of operations which would be much more time-consuming or complex to perform by issuing commands one step at a time. The SPSS macro language is quite flexible. Macros can conduct a whole series of analyses, make adjustments to multiple output statistics, produce multiple charts with many combinations of variables, or any number of other tasks.

After a macro has been defined, written, and saved, it can be used by “Calling” it.


When Might I Use an SPSS Macro?

Suppose you run weekly analyses for a marketing company. Your report provides descriptive statistics and regression models to examine the impacts of several marketing campaigns on company profits. Assuming you need to run similar analyses for 12 sets of variables every week, these analyses could be performed using one of three methods. These methods and estimates of time required are as follows:

  • SPSS Point-and-click interface (GUI) : about 2 hours [at 10 minutes per analysis]
  • SPSS Syntax editor: about 1 hour [at 5 minutes per analysis]
  • SPSS Macros: about 2 minutes [at 10 seconds per analysis]

This example involves only one simple application of SPSS macros, used for a relatively straightforward task, performed once-per-week. While the time-saving potential of a well-designed macro is apparent here, it becomes even more attractive for more complex or frequent SPSS tasks (imagine the previous example, performed every day, using 100 sets of variables, and requiring subtle adjustments to multiple output statistics).

There are many applications for using macros. For instance, macros can be used to conduct tests of mediation and moderation in regression, estimate path coefficients of other indirect effects, or to perform bootstrapping functions. SPSS macros are fairly easy to create if you have a fundamental understanding of SPSS syntax. There are literally thousands of ready-made macros freely available online.  Raynald’s SPSS Tools site has a large collection.


Using SPSS Macros

In order to use an SPSS macro, the macro must be

  • Saved in an accessible directory (preferably the directory which contains other SPSS files for the same project)
  • “Included” in an SPSS Session
  • Called by the user
  • Run by the user
Step 1: Saving SPSS Macros
  • Whether you have written a macro yourself, or are downloading it from the Internet, begin by saving it to an accessible directory. Macros are written in SPSS syntax, and should generally be saved as a file with a .SPS extension [EXAMPLE: !screen.SPS].
  • A macro begins with the word “DEFINE.” The next word is the name of the macro. For example, if a macro contains the line “DEFINE !screen” , the macro name is !screen. It is advisable to use an exclamation mark before the name, so the macro will be easier to locate in the syntax text.
  • Macros can be saved in a .txt file in ASCII format. Simply copy and paste the macro text into the SPSS syntax window when you wish to use it.
Example of a macro for initial data screening:

DEFINE !screen (variables = !CMDEND)
FREQUENCIES VARIABLES=!variables
/STATISTICS=STDDEV MINIMUM MAXIMUM MEAN MEDIAN SKEWNESS SESKEW KURTOSIS SEKURT
/HISTOGRAM NORMAL
/FORMAT=LIMIT(10)
/ORDER=ANALYSIS.
!ENDDEFINE.
Step 2: Including an SPSS Macro in a Session
  • If the macro is saved in an .sps file, a line of syntax must be executed in order to use the macro. Simply include the following command anywhere in the syntax file (SPSS deals with macros before running any other syntax, so it does not have to be in the beginning):
    • INCLUDE [file directory\macrofilename.sps].

For example, if a macro called “!screen” was saved on the “C drive”; in the “My Documents” folder, you could include it by using the following command in SPSS syntax:

    • INCLUDE C:\My Documents\!screen.sps.

If the macro has been saved in the same directory as other SPSS files in the open project, it can be included without including the file directory, as in:

    • INCLUDE [!screen.sps]
  • If an SPSS macro has been saved as a text file: Open the file, copy and paste the text into the syntax in which you wish to use the macro.
Step 3: Calling an SPSS Macro
  • To actually perform the function of an SPSS macro, it must be “called.” The basic format for calling a macro is as follows:
    • [macro name] [argument values].
  • In place of [argument values] the user provides the list of variables the macro will use.
  • For example, if the macro named “!screen” runs an initial screening analysis for a list of variables, the user must enter the variables of interest in place of [argument values].
Example:*Call the macro and provide the variable names.
!screen variables = X1 X2 X3 X4.
*Screen some more.
!screen variables = Y1 Y2 Y3 Y4.


Example of an SPSS macro

Here is SPSS syntax that includes definition of a macro and calling the macro. This simple macro provides screening information on variables, including skew and kurtosis and the maximum and minimum values, along with a histogram showing the shape of each distribution.

*Activate the data set of interest with variables X1 through X4 and Y1 through Y4.
*Create a macro for initial data screening.DEFINE !screen (myvars = !CMDEND)
FREQUENCIES VARIABLES=!myvars
/STATISTICS=STDDEV MINIMUM MAXIMUM MEAN MEDIAN SKEWNESS SESKEW KURTOSIS SEKURT
/HISTOGRAM NORMAL
/FORMAT=LIMIT(10)
/ORDER=ANALYSIS.
!ENDDEFINE.

*Call the macro and provide the variable names.
!screen variables = X1 to X4.
*screen some more.
!screen variables = Y1 to Y4.

You can find more information on SPSS macros at Macro Tutorial by Raynald Levesque.

More technical information from SPSS on macros and other syntax applications is available here


Matthew Galen and Dale Berger, updated 21 April 2015

Loading