Macros can:

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 happyFace2.gif (633 bytes) 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.

Index

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 Categories

Macro Gems (also called "beauties")

  1. Define list of variables between two Variables.SPS
  2. Reorder vector names.SPS (this macro is listed and explained here)
  3. Variable type.sps (Input: a list of variables --> Output: two lists 1) list of string variables and 2) list of numeric variables.
  4. Xpand vector names.SPS

Macro variables

  1. Add casewise regression parameters to the file.SPS
  2. 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. 
  3. Call a macro with argument equal to each value of a variable.SPS
  4. Define macro variable equal to distinct values of a given variable.SPS
  5. How to multiply two macro variables.SPS
  6. Macro facility is just a string parser.SPS (interesting comments by D. Marso)
  7. Multiply two macro variables.SPS
  8. Odd feature of macro parser.SPS
  9. Operations with macro variables.SPS (see also Doing arithmetic with macro variable)
  10. Use pairs of variables given in macro Call.SPS

Self adjusting macros

  1. Include 2 outputs in syntax.SPS
  2. Include output in syntax.SPS
  3. 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.

Unclassified

  1. Find LAG(var1,var2).SPS
  2. Get all results for school1 then school2 etc.SPS
  3. Generate a variable number of syntax commands.SPS
  4. Imbedded macros.SPS
  5. List a variable number of variables in the KEEP subcommand.SPS
  6. Listing in a loop.SPS (even though procedures are not 'allowed' in a LOOP)
  7. Pass a conditionnal expression as a macro argument.SPS

Utilities

  1. Define path by macro.SPS (very useful technique)
  2. Execute a macro for each distinct value of a var.SPS
  3. Macro autogenerates initial data file.SPS with thanks to Fernando Cartwright
  4. Print message in Output window.SPS
  5. Process variable subsets of file.SPS
  6. Quote string on the fly.SPS (I use this often)
  7. Run macro on different sets of parameters.SPS
  8. 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.