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.

## This tutorial has 36 comments

## By Van on June 22nd, 2015

Dear Ruben

Thank you for the useful tutorial.

I've been wondering if you actually know some trick in SPSS to deal the following issue:

I have a task consisting of 30 trials, this means that each subject provides 30 answers. Now I need to calculate the mean of the reaction time but only for correct trials. Also, the order of these trials are randomized for all subjects and the randomization order is unfortunately not recorded by the program I use for collecting data.

At the moment I don't know how to do this the fastest way in SPSS.

What I am doing now is first I pick out the correct answers by telling SPSS to recode the original RT variables into new variables (called RT_correctanswer, for example) if each of them satisfies the condition that the corresponding Trial_correct variable is correct (1 = correct, 0 = incorrect). If the condition is not satisfied then the new variable just has a missing value. This means that I will have 30 new RT_correctanswer variables. Then I ask SPSS to take the mean of these RT_correctanswer variables. This method works fine for one dataset and for one thing to filter out (correct vs. incorrect). Now I have a few data sets with different number of trials and there are also a bunch of other things I have to filter out which entails a lot more syntax composing than I can manage without making some serious mistakes here and there. I wonder if you know anything better?

The problem lies in the condition, I don't know how to tell SPSS to do the following:

If Trial1_correct = 1 and Trial2_correct = 1 and ...

Then compute Mean_RT = MEAN(Trial1_RT, Trial2_RT, ...).

The thing is SPSS will not compose any mean. Because no one has perfect 30 correct answers. What I would like it to do is to calculate the mean RT but only for trials where the associated Correct variable is 1 and ignore others. I tried to change "and" to "or" but then only get error messages.

Thank you very much in advance for your help!

## By Ruben Geert van den Berg on March 23rd, 2015

See RANGE.

## By shimaa ahmed on March 23rd, 2015

i wanna to know how can i do this statement on spss syntax windows (if the answer in Q5 'No' , transfer to Q7)

and (Age ranges between 18 and 60)

## By Ruben Geert van den Berg on November 20th, 2014

There's a faster way than

`IF`

for doing so and it's discussed here. Regarding the date comparison, use the`DATE.DMY`

function. I wrote a tiny example of this and put it here.HTH,

Ruben

## By godfrey matumu on November 20th, 2014

very helpful.

However, i have one thing that troubles me.

I have spss db, it has a variable date of death with the format of dd.mm.yyyy.

I want to change this into yes or no.

how do I do it with the if statement?