Macros
Home FAQ Syntax Macros Scripts Tips Links

Macro Tutorial
Debugging Macros

Enter keywords


Exact match


Advanced Search
Feedback
Site Map

 

Other languages
Deutsch
Español
Français
Italiano
Português

 

Do you love this site?

 

SPSS Macros are wonderful!

Macros are to syntax what syntax is to the Graphic User Interface (GUI): A big step forward.

Macros can:

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 want to purchase 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".

Index of Macro categories

  1. Macro Gems
  2. Macro variables
  3. Self adjusting macros
  4. Unclassified
  5. Utilities
  6. See also the Macro Library at the spss site.

 

Never underestimate the power of SPSS!

 

Note: Files with a at the end were added or modified less than 30 days ago.

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. Odd feature of macro parser.SPS
  7. Operations with macro variables.SPS (see also Doing arithmetic with macro variable)
  8. Use pairs of variables given in macro Call.SPS
  9. Macro facility is just a string parser.SPS (interesting comments by D. Marso)
  10. Multiply two macro variables.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. Imbedded macros.SPS
  4. List a variable number of variables in the KEEP subcommand.SPS
  5. Listing in a loop.SPS (even though procedures are not 'allowed' in a LOOP)
  6. Pass a conditionnal expression as a macro argument.SPS

Utilities

  1. Define path by macro.SPS (very useful technique)
  2. Print message in Output window.SPS
  3. Process variable subsets of file.SPS
  4. Run macro on different sets of parameters.SPS
  5. 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.

 

Fully commented macro

  1. 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.

 

 

 

Top of page

 

This page was last updated on February 11, 2004.  Raynald's SPSS Tools

© Raynald Levesque 2001-04