SPSS Macro Guide
Tutorial sections and descriptions:
Note: SPSS syntax is the language used by SPSS to carry out commands, including
SPSS macros. If you wish to learn more about SPSS Syntax, you may consult
online syntax tutorials such as the following:
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 a Sobel's
test of mediation 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. Additionally, there are
literally thousands of ready-made macros freely available online.
Using SPSS Macros
In order to use an SPSS macro, the macro must be
Step 1: Saving SPSS Macros
Whether you have written a macro yourself, or are downloading it from a site, simply save
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].
Look for the word "DEFINE." The next word is the name of the macro. For example, the macro
may contain the line "DEFINE !screen." In this case, the macro name is !screen. It is
advisable to name macros using an exclamation mark before their name, so they will be easier to locate
in the syntax text. Using this name when saving the macro will help you identify it.
Macros can also 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.
|
*Create 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):
For example, if a macro called "!screen" was saved on the "C drive";
in the "My Documents" folder, you could include it using the following command in
SPSS syntax:
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:
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:
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 online. See, for example,
Macro Tutorial
by Raynald Levesque.
More technical information on SPSS macros and other syntax applications is
available
here
Matthew Galen and Dale Berger, updated 28 September 2011
|