Basic SPSS Syntax Functions for Daily Use

A researcher's cheat sheet for checking routing, managing complex survey logic, and variable transformation.

I wanted to do a quick article that shows the most common SPSS syntax all on one page, as I am forever looking at multiple sites to remember the formatting of the most common syntax. The most common thing that you will need syntax for is checking the routing of a complicated survey or singling out a group of people for analysis.

Note: If the routing only relies on two different variables, you can use crosstabs. Check out our free crosstabs tool here.

1

COMPUTE, IF, AND, OR, & FREQ

The most basic form of this is making a routing variable which changes between 0 for excluded and 1 for included.

compute routeq64=0.
*An if statement using AND to change the value of the routing variable to 1.
if (gender=1 and age=1 and transport=1) routeq64=1.

*See how many meet these conditions.
freq routeq64.
2

ANY

ANY is significantly cleaner when dealing with long lists of "OR" conditions.

compute routeq64=0.
*Traditional OR condition.
if (transport=1 or transport=2 or transport=3) routeq64=1.

*Same condition using ANY.
if (any(transport,1,2,3)) routeq64=1.
3

COUNT (with thru and to)

Easily count occurrences across ranges of variables (TO) and ranges of values (THRU).

*Count high scores across ONS wellbeing measures.
count wellbeing= satisfied to happy (7 thru 10).

freq wellbeing.
4

RECODE

The workhorse of data cleaning: grouping your codes into manageable bands.

*Grouping 0-10 scale into 3 bands.
recode happy (1 thru 4=1) (5 thru 6=2) (7 thru 10=3) into happyrecoded.

freq happyrecoded.
5

MISSING & SYSMIS

Dealing with "Don't Know" codes or system-generated gaps in the data.

*Set user-defined missing values.
missing values happy (11,12).

*Recoding system missing to a specific code.
recode happyrecoded (SYSMIS = 99).
6

Timings

Calculating survey duration and handling date filters.

*Difference between start and finish.
compute duration = datediff(FINISH_TIME, START_TIME, 'minutes').

*Filter by date.
compute cutoff_date = date.dmy(01,01,2026).
if (START_DATE > cutoff_date) filter_var=1.

QUICK REF The 2-Minute Summary

compute flag=0.
if (age=1 and gender=1) flag=1.
if (any(v1,1,2,3)) flag=1.
count score= q1 to q5 (7 thru 10).
recode v1 (1 thru 5=1)(6 thru 10=2) into v1_new.
missing values v1 (98, 99).