Some Syntax and Macro Examples
Dr. Marta García-Granero
Dr. Art Kendall
Raynald Levesque
Hector Maletta
Kirill Orlov
Richard Ristow
Dr. Marta García-Granero
The multiplicity problem: When multiple statistical tests are run (and p-values obtained) on the same set of data, the chance of at least one spurious significance (type-I error) is greater than the accepted value (5%). A way of controlling the so called FamilyWise Error Rate (FWER) is needed. Bonferroni method is widely used but considered too conservative (low power to detect real significances). Other methods, like Holm's, Hommel's, Finner's, use a sequential procedure to adjust the p-values to control the FWER, while retaining good power. This code will compute adjusted p-values with several methods:

There is an online e-book I always recommend for beginners. It is called “Statistics at Square One.” I have written SPSS code (ranging from very simple to quite advanced) to solve every example and exercise presented in the book

Trivia: What is the most complicated data manipulation you can perform in the fewest statements?

I have answered recently a question like that in the list, and I solved it using MATRIX and 19 lines of code. [also see the following example]


Excellent example of the use of the FLIP command

Dr. Art Kendall
Computing age from two dates

Art helps a user devise a way to sum 40 items to get a total score in the presence of missing values.

Solution to the the question, "Any SPSS syntax that can allows to create a Likert scale with 10 variables and fill it with random interger values between 1-5 with 45 cases?"

Selecting cases with a specific word within a variable...and a follow-up explication of the code.

Random number without replacement

Raynald Levesque Top
One of the best things you can do to help yourself learn SPSS is to bookmark and become familiar with Raynald Levesque's website, the main purpose of which is to "present useful code and techniques to increase productivity of all levels of SPSS users."

Another is to buy (or download—free) and read his book, “SPSS Programming and Data Management: A Guide for SPSS and SAS Users,” which was released by SPSS on January 7th, 2004. The book is available in printed form for a fee, or as a free download in PDF format from SPSS. Here is the description found on SPSS's site:

SPSS Programming and Data Management: A Guide for SPSS and SAS® Users, Second Edition documents the wealth of functionality beneath the SPSS user interface. It includes detailed examples of command syntax, the macro facility, scripting, and the output management system (OMS). The accompanying CD-ROM includes the command and data files used in the book. The book also contains a chapter for SAS users, showing equivalent SPSS code for many common data management tasks. With knowledge gained from this book, you will be able to use the many tools available within SPSS to import data from almost any source, clean it, transform it, merge it with other data, and get it into the condition required to produce reliable models and informative results.


However, here are a few samples of Ray's contributions to the SPSSX list. The first is referred to by Marta García-Granero as an example of beautifully parsimonious code for disaggregating a data table.

Second is the solution to a very tricky data restructuring problem. This is a good example of Raynald's generosity and his desire to extend himself for others in order to continue learning.

Third, a little masterpiece, a small program and macro to remove all the variable labels from a data file.

Hector Maletta Top
Due to the loss of a hard drive and extensive travel demands at the time this site is being prepared, Hector is not able to provide selected examples of his problem solutions. These will be provided ASAP and posted to the online version of this project.

Kirill Orlov
Kirill has a web page where he gradually lays out some of the macros he has written during years of work with SPSS. Those are not examples or gimmicks but finalized pieces that are "commands absent in SPSS".

As for the piece of syntax for the current project, he selected an example of programming out of his archives. It is an example of an "ad hoc syntax," one that you write for the current study because you've got research ideas that cannot be fulfilled via pre-defined procedures as you want them to be. Use syntax and get whatever you fancy - the only moral of the vignette. It is not a pearl in itself.

That was a study of job attitudes among Russian diplomatic college senior students (Moscow State Institute of Foreign Affairs). It uncovered conflicting job expectations at a transition from 'socialist' to 'capitalist' frame of minds among Russian students in 90s. The results of the study were very interesting (in particularly they might be for western audience). Kirill wishes he could display them, - be not this page focusing on programming only.

Description, instructions and syntax in a Microsoft Word document (420 kb)

Sample data in SPSS SAV file (22 kb)


Richard Ristow Top
2004-01-09 Chapman - count zeroes.TXT

Count number of zeroes in a contiguous set of variables (trivial, with COUNT), and find the length of the longest run of zeroes (VECTOR/LOOP logic)


2004-02-16 Smith - Leading zeroes.TXT

Convert a number to string with leading zeroes, when there's a decimal portion and 'N' format can't be used


2004-04-01 Moser - Time in a file name.TXT

Generate macro, INCLUDE, to create a .SAV file whose name contains the current time. (Raynald Levesque, of course, is the master of writing and INCLUDing files. I learned the technique from him.)


2004-07-09 Fine - Change values across cases.TXT

Compute the value of "jadhd", which is 1 if the person has an ADHD dx, 0 otherwise, based on:

If person is a child (famem = 0), set if adhdsub > 0
If person is mother (famem = 1), set if child madhddx = 1,4
If person is father (famem = 2), set if child fadhddx = 1,4


2004-07-20 wang - Combine groups.TXT

A tour de force: "If the same ID occurs in two groups, the two are to be combined to form a single group." Within the combined groups, those that now have IDs in common are again combined, continuing until no ID occurs in more than one group.


2005-03-25 Grigsby - Importing text file.TXT

Another tour de force: An input parser for a tagged-text file, highly flexible (all right, needlessly flexible)


In this tricky example, the task is to assign sons to fathers. There's what looks like a bug in the LAG function...all is revealed.

Here's one where the user didn't tell what they really wanted in regard to "reading numeric data with leading zeros."

I said I'd send you a problem I'd recently found difficult. As is common, the difficulty wasn't technical; it was quite easy, technically, to get the desired result. The difficulty was layers of misunderstanding about what was really wanted, and what it meant.