# SPSS tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

# SPSS IF – A Quick Tutorial

In SPSS, IF is a conditional COMPUTE command. It calculates a (possibly new) variable but only for those cases that satisfy some condition(s). This tutorial walks you through some typical examples of the IF command.

## Example 1 - Replace Missing Values

With the syntax below we'll first create some test data. Next we'll set the existing variable score to 100 for all respondents (only one in this case) having a missing value on score. An alternative here is RECODE score (missing = 100). The effect becomes visible after sorting the cases in a more conventient way.This is because IF is technically a transformation.

## SPSS IF Syntax Example 1

*1. Create test data.

data list free/gender score.
begin data
0 80 1 85 0 90 1 95 0 '' 1 105 0 110 1 115
end data.

*2. Replace missing value with 100.

if missing(score) score = 100.

*3. Sort cases.

sort cases gender.

## Example 2 - Score Groups

Next, we'll create score groups. Respondents scoring under 100 points get a 1 (‘low score’). The others get a 2 (‘high score’). We'll demonstrate three ways to do so. The third may seem a little weird. It's explained in Compute A = B = C.

## SPSS IF Syntax Example 2

*1. Create score groups option 1.

if score lt 100 group_a = 1.
if score ge 100 group_a = 2.
exe.

*2. Create score groups option 2.

recode score (100 thru hi = 2) (else = 1) into group_b.
exe.

*3. Create score groups option 3.

compute group_c = (score ge 100) + 1.
exe.

## Example 3 - Gender-Score Groups

Now we'll create score groups for female and male respondents separately. At this point we can't use a simple RECODE anymore. This is because the conditions now involve two variables, gender and score. A simple approach here is using four IF statements. Each holds two conditions (gender and score). A faster but more difficult equivalent here is a single COMPUTE command.

## SPSS IF Syntax Example 3

*1. Gender-score groups option 1.

if score lt 100 and gender eq 0 group_d = 1.
if score ge 100 and gender eq 0 group_d = 2.
if score lt 100 and gender eq 1 group_d = 3.
if score ge 100 and gender eq 1 group_d = 4.
exe.

*2. Gender-score groups option 2.

compute group_e = 2 * gender + (score ge 100) + 1.
exe.

## Difference Between IF and DO IF

Very similar to the IF commands we showed is DO IF-ELSE IF-END IF. Apart from the latter usually requiring more syntax, there's an important difference between the two. This occurs when conditions are not mutually exclusive. This means that a single case may satisfy two or more conditions simultaneously. In this case, the following happens

• With IF the last condition that holds prevails. Since IF statements are completely separate commands, later ones simply overwrite the results of previous ones.
• With DO IF-ELSE IF-END IF the first condition that holds prevails. The trick is in ELSE IF. The “ELSE” here means “if the preceding condition(s) don't hold, only then...

The final syntax example demonstrates this difference between IF and DO IF-ELSE IF-END IF.

## SPSS IF Syntax Example 4

*1. Three score groups with DO-IF.

compute group_f = 1.
do if score ge 100.
compute group_f = 3.
else if score ge 90.
compute group_f = 2.
end if.

*2. Sort cases.

sort cases score.

*3. Equivalent IF statements don't work.

compute group_g = 1.
if score ge 100 group_g = 3.
if score ge 90 group_g = 2.
exe.

# Let me know what you think!

*Required field. Your comment will show up after approval from a moderator.

# This tutorial has 36 comments

• ### By Laura on December 4th, 2016

I have 4 sub-scores. I want to calculate an overall mean score. I want to omit scores of zero on any of the subscores because if I dont, then the average is divided by 4 when only 3 scores went into the average.

how do I do this?

• ### By Ruben Geert van den Berg on November 1st, 2016

Hi Diasylyn! No, I don't think there's an official limit. However, I do think you can -and should- shorten your syntax an awful lot because there's a ton of repetitions in it. This also makes the whole thing unmanageable. Perhaps try something like

do if (Head_Circumference_DMC < 9999 AND ...)./*UNIVERSAL EXCLUSIONS. do if (sex = 0).
if (Gestational_Age_DMC EQ 28 ...)...
if (Gestational_Age_DMC EQ 29 ...)...

else if (sex = 1).
if (Gestational_Age_DMC EQ 28 ...)...
if (Gestational_Age_DMC EQ 29 ...)...

end if./*SEX SELECTOR.
end if./*UNIVERSAL EXCLUSIONS.

Also see DO IF. Hope that helps!

• ### By Daisylyn Chin on November 1st, 2016

Is there a limit to how many operands, operators allowed in a "if" statement?

• ### By Daisylyn Chin on November 1st, 2016

This is giving me an error.

IF ((Gestational_Age_DMC EQ 28 AND sex EQ 0 AND Head_Circumference_DMC GE 22.77 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 32.1 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 0.73 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 29 AND sex EQ 0 AND Head_Circumference_DMC GE 23.66 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 33.37 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 0.83 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 30 AND sex EQ 0 AND Head_Circumference_DMC GE 24.55 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 34.64 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 0.93 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 31 AND sex EQ 0 AND Head_Circumference_DMC GE 25.43 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 35.91 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.05 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 32 AND sex EQ 0 AND Head_Circumference_DMC GE 26.32 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 37.18 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.18 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 33 AND sex EQ 0 AND Head_Circumference_DMC GE 28.07 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 39.39 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.13 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 34 AND sex EQ 0 AND Head_Circumference_DMC GE 28.76 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 40.76 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.4 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 35 AND sex EQ 0 AND Head_Circumference_DMC GE 29.39 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 41.99 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.65 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 36 AND sex EQ 0 AND Head_Circumference_DMC GE 29.99 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 43.1 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.88 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 37 AND sex EQ 0 AND Head_Circumference_DMC GE 30.54 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 44.09 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.08 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 38 AND sex EQ 0 AND Head_Circumference_DMC GE 31.05 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 44.98 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.27 AND Weight_KG_DMC LT 9999 ) OR
(Gestational_Age_DMC EQ 39 AND sex EQ 0 AND Head_Circumference_DMC GE 31.54 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 45.79 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.43 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 40 AND sex EQ 0 AND Head_Circumference_DMC GE 32.00 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 46.52 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.58 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 41 AND sex EQ 0 AND Head_Circumference_DMC GE 32.44 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 47.19 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.71 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 42 AND sex EQ 0 AND Head_Circumference_DMC GE 32.85 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 47.8 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.83 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 28 AND sex EQ 1 AND Head_Circumference_DMC GE 22.52 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 31.6 7AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 0.69 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 29 AND sex EQ 1 AND Head_Circumference_DMC GE 23.41 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 32.94 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 0.78 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 30 AND sex EQ 1 AND Head_Circumference_DMC GE 24.30 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 34.21 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 0.88 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 31 AND sex EQ 1 AND Head_Circumference_DMC GE 25.18 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 35.48 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 0.99 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 32 AND sex EQ 1 AND Head_Circumference_DMC GE 26.07 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 36.75 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.12 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 33 AND sex EQ 1 AND Head_Circumference_DMC GE 27.75 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 39.53 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.15 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 34 AND sex EQ 1 AND Head_Circumference_DMC GE 28.47 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 40.79 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.42 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 35 AND sex EQ 1 AND Head_Circumference_DMC GE 29.12 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 41.9 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.66 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 36 AND sex EQ 1 AND Head_Circumference_DMC GE 29.71 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 42.89 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 1.87 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 37 AND sex EQ 1 AND Head_Circumference_DMC GE 30.24 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 43.77 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.06 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 38 AND sex EQ 1 AND Head_Circumference_DMC GE 30.73 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 44.56 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.23 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 39 AND sex EQ 1 AND Head_Circumference_DMC GE 31.17 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 45.27 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.38 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 40 AND sex EQ 1 AND Head_Circumference_DMC GE 31.57 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 45.9 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.5 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 41 AND sex EQ 1 AND Head_Circumference_DMC GE 31.94 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 46.47 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.61 AND Weight_KG_DMC LT 9999) OR
(Gestational_Age_DMC EQ 42 AND sex EQ 1 AND Head_Circumference_DMC GE 32.28 AND Head_Circumference_DMC LT 9999 AND Length_cm_DMC GE 45 AND Length_cm_DMC LT 9999 AND Weight_KG_DMC GE 2.24 AND Weight_KG_DMC LT 9999)
) Microcephaly_Status_HC_LG_WG_DMC EQ 0.
EXECUTE.

• ### By Ruben Geert van den Berg on August 24th, 2016

Hi Nicholas! Thanks for the compliment! My website's backend is managed with some scripts I developed over the years. I'd recommend doing so only if you're either very driven or -more likely- very crazy ;-)