Sample SPSS Macros
SPSS Macros are wonderful! Macros are to syntax what syntax is to the Graphic User Interface (GUI): A big step forward.
- reduce the number of lines of codes by up to 80-90%
- greatly simplify code maintenance
- speed up code development
- facilitate code recycling
- do things syntax can't do, for instance: create variable names "on the fly" using !CONCAT(var,!cnt).
If macro language did not exists, SPSS would have to invent it. Do try to learn macros! You will not regret it.
Once you know macros you find many use for them. It is for this reason that most macros are in the Syntax section of this site (there are in excess of 140 macros).
If you need a good book (IMHO) on syntax and macros, see SPSS Programming and Data Management.
If you do not know how to use a macro from this site see Someone posted a macro but I do not know how to use it!
Tip: to quickly find all macros, search this site for "!ENDDEFINE".
Are Macros still relevant?Of course anything done with macros can be done using Python but I think macros can still be useful for simple jobs.
The syntax files with macro language are broadly classified by purposes as follows:
See also the example of One fully commented macros and the bottom of the page to feel how macro operates.
Never underestimate the power of SPSS!
Macro Gems (also called "beauties")
- Define list of variables between two Variables.SPS
- Reorder vector names.SPS (this macro is listed and explained here)
- Variable type.sps (Input: a list of variables --> Output: two lists 1) list of string variables and 2) list of numeric variables.
- Xpand vector names.SPS
- Add casewise regression parameters to the file.SPS
- Arithmetic with macro variables.SPS (with thanks to Paul Cook) This illustrates the "traditional" method based on strings as well a method based on loops.
- Call a macro with argument equal to each value of a variable.SPS
- Define macro variable equal to distinct values of a given variable.SPS
- How to multiply two macro variables.SPS
- Macro facility is just a string parser.SPS (interesting comments by D. Marso)
- Multiply two macro variables.SPS
- Odd feature of macro parser.SPS
- Operations with macro variables.SPS (see also Doing arithmetic with macro variable)
- Use pairs of variables given in macro Call.SPS
Self adjusting macros
- Include 2 outputs in syntax.SPS
- Include output in syntax.SPS
- Macro exits Loop when data meet convergence criteria.SPS This is useful when you have an iteration process, each iteration takes a lot of time, and you do not know how many iterations will be required.
- Find LAG(var1,var2).SPS
- Get all results for school1 then school2 etc.SPS
- Generate a variable number of syntax commands.SPS
- Imbedded macros.SPS
- List a variable number of variables in the KEEP subcommand.SPS
- Listing in a loop.SPS (even though procedures are not 'allowed' in a LOOP)
- Pass a conditionnal expression as a macro argument.SPS
- Define path by macro.SPS (very useful technique)
- Execute a macro for each distinct value of a var.SPS
- Macro autogenerates initial data file.SPS with thanks to Fernando Cartwright
- Print message in Output window.SPS
- Process variable subsets of file.SPS
- Quote string on the fly.SPS (I use this often)
- Run macro on different sets of parameters.SPS
- Run a macro for each value in a parameter file.SPS
Note that a macro does not have to be complicated to be extremely useful. See the Macro Tutorial for an introduction to macros.
One fully commented macro
Reorder Vector Names (this macro is in the Macro Gems above)
* Reorder vector names in data files. * This is useful when you have vectors say a1 TO a50, b1 TO b50, c1 TO c50. * and you want to reorder them as a1 b1 c1 a2 b2 c2 ... a50 b50 c50. * Raynald Levesque 2001/04/28. DATA LIST LIST /dummy. BEGIN DATA 1 END DATA. LIST. * Following vectors are "consecutive" (all a's, all b's then all c's). NUMERIC id x y a1 TO a50 z1 b1 TO b50 z2 c1 TO c50 k. * Say we want to Reorder vectors a b c "side by side" that is a1 b1 c1 a2 b2 c2 ... a50 b50 c50. SET MPRINT=no *//////////////////////. DEFINE !reorder (id=!TOKENS(1) /beg !TOKENS(1) /end !TOKENS(1) /vnames !CMDEND) MATCH FILES FILE=* /KEEP=!id !DO !i=!beg !TO !end !DO !var !IN (!vnames) !CONCAT(!var,!i," ") !DOEND !DOEND ALL. !ENDDEFINE. *//////////////////////. * Example of use of the macro. SET MPRINT=yes. !reorder id=id beg=1 end=50 vnames=a b c. **********************. *** Explanations***. **********************. * The following format helps to understand the !DO - !END loops in the above match command, *!DO !i=!beg !TO !end * !DO !var !IN (!vnames) * !CONCAT(!var,!i," ") * !DOEND *!DOEND *ALL. * NOTES: *1. ALL means keep all other variables not previously named. *2. The macro loop is written in one line to reduce the number of lines in the syntax file being created. *3. Note that macro commands do NOT need a command terminator (a decimal point) at the end. In fact if periods had been inserted after !DO !i=!beg !TO !end for instance, the macro would no longer work! because this period would effectively "end" the MATCH FILES command.