Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 39 ยท Syntax and Automation

Data Steps in SPSS Syntax: COMPUTE, RECODE, IF, SELECT IF

Transform data in SPSS syntax with COMPUTE, RECODE, IF, DO IF, COUNT, SELECT IF, FILTER and TEMPORARY, with examples, EXECUTE rules and runnable stand-ins.

Before any analysis, data usually need new variables, recoded categories, and sometimes a subset of cases. Those jobs are done through Transform menus or, better, through a handful of syntax commands. This lesson covers the ones you will use every week. The syntax layout was introduced in syntax basics.

COMPUTE

COMPUTE creates or overwrites a variable from an expression.

Text
COMPUTE bmi = weight / (height/100)**2.
COMPUTE total = SUM(q1 TO q5).
COMPUTE mean_sat = MEAN.4(q1 TO q5).
COMPUTE log_income = LN(income).
EXECUTE.

Notes:

  • Operators: + - * / **; comparison = <> < > <= >= (or EQ NE LT GT), logical AND OR NOT.
  • Functions: SUM, MEAN, SD, MIN, MAX, ABS, SQRT, LN, LG10, RND, TRUNC, MOD, NVALID, and for strings CONCAT, SUBSTR, UPCASE, LENGTH.
  • MEAN.4(...) means "compute the mean only if at least 4 values are valid". Without the suffix SPSS uses whatever values exist, which hides missing data.
  • A single missing value in an arithmetic expression (q1 + q2) makes the result system-missing. SUM(q1,q2) ignores missing values, which is a different (and sometimes wrong) result.
  • TO ranges such as q1 TO q5 use the order of variables in the file, not the names alphabetically.

EXECUTE: SPSS is lazy

Transformations are pending: SPSS stores them and applies them at the next command that reads the data. EXECUTE. forces a pass. If a transformation is followed by an analysis, you do not need it. Add it when you want to see the new column in Data View straight away, or just before SAVE.

RECODE

Text
RECODE age (18 THRU 29=1) (30 THRU 49=2) (50 THRU HI=3) INTO age_group.
RECODE q1 q2 q3 (1=5) (2=4) (3=3) (4=2) (5=1).   /* reverse items in place */
RECODE income (-9=SYSMIS).
RECODE gender ('M'=1) ('F'=2) INTO gender_n.
VALUE LABELS age_group 1 '18-29' 2 '30-49' 3 '50+'.
EXECUTE.

Rules: values are tested left to right and the first matching rule wins; LO and HI are the extremes; ELSE catches everything else; MISSING and SYSMIS refer to missing data. Prefer INTO a new variable so the original stays; recoding in place destroys information.

IF and DO IF

Text
IF (age >= 65) senior = 1.
IF (age < 65) senior = 0.

DO IF (income >= 100000).
  COMPUTE bracket = 3.
ELSE IF (income >= 40000).
  COMPUTE bracket = 2.
ELSE.
  COMPUTE bracket = 1.
END IF.
EXECUTE.

Cases with a missing value in the condition are not assigned (so senior would stay missing for them in the first example). Check with FREQUENCIES afterwards.

COUNT

Text
COUNT n_yes = q1 q2 q3 q4 (1).

counts how many of the listed variables equal 1 for each case.

Selecting cases

Text
SELECT IF (country = 'DE' AND age >= 18).

SELECT IF permanently deletes the other cases from the working file (not from the file on disk until you save). To select without deleting, use a filter:

Text
COMPUTE filter_$ = (age >= 18).
FILTER BY filter_$.
FREQUENCIES VARIABLES=gender.
FILTER OFF.

For one analysis only use TEMPORARY. right before SELECT IF; the selection disappears after the next procedure.

Text
TEMPORARY.
SELECT IF (region = 2).
DESCRIPTIVES VARIABLES=income.

Also useful: SORT CASES BY id., SPLIT FILE LAYERED BY gender. (repeat every analysis per group; turn off with SPLIT FILE OFF.), WEIGHT BY w. (and WEIGHT OFF.) and SAMPLE .1. for a random 10%.

Python Stand-in: COMPUTE, RECODE and IF on cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reverse-scoring example

A questionnaire has items rated 1 to 5, and item q3 is worded negatively. Reverse-score it before summing:

Text
COMPUTE q3_r = 6 - q3.
COMPUTE scale_total = SUM.3(q1, q2, q3_r).

For a 1-to-5 scale the rule is new = (max + min) - old = 6 - old.

Python Stand-in: reverse scoring and a scale score

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Checking your transformations

Always verify after changing data:

Text
FREQUENCIES VARIABLES=age_group.
CROSSTABS /TABLES=age BY age_group.
DESCRIPTIVES VARIABLES=total.

A crosstab of old by new values shows each rule worked and that no case fell through a gap. Dates, text and numeric variables follow different rules, so use STRING newvar (A10). before computing text values.

Common mistakes

  • Forgetting EXECUTE and wondering why the column is not there (or running it twice and double-transforming a variable computed in place).
  • Overlapping RECODE ranges, which are applied left to right.
  • Using SELECT IF and then saving over the original file.
  • Letting missing values flow silently through SUM versus +.
  • Recoding into the same variable and losing the original.

For repeated or looped transformations see macros, loops and Python.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. RECODE age (LO THRU 29=1) (18 THRU 99=2). What does age 20 become?

    Rules are tested left to right and the first match wins, so 20 becomes 1.
  2. 2. What does SELECT IF do?

    SELECT IF removes cases permanently from the working file; use FILTER or TEMPORARY to keep them.
  3. 3. Why use MEAN.4(q1 TO q5) rather than MEAN(q1 TO q5)?

    The .n suffix sets the minimum number of valid values.
  4. 4. A 1-5 item is reverse-scored by:

    new = (max + min) - old = 6 - old maps 1 to 5 and 5 to 1.

Summary

  • COMPUTE builds variables, RECODE maps values, IF and DO IF set values by condition.
  • Transformations are pending until the next command or EXECUTE.
  • SELECT IF deletes cases; FILTER or TEMPORARY keep them.
  • Recode into new variables, set minimum-valid counts for scale scores and verify with frequencies and crosstabs.

Next: Macros, Loops and Python in SPSS