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 Lorena on March 31st, 2018

Hi Ruben!

I love your tutorial on the IF command, thank you! Unfortunately I am struggling...

I want to compute a variable (X) which is the sum of three others variables (x1+x2+x3). x1, x2 and x3 have either the values 1 or 0. I recoded system missings to -77. There are some conditions which should be met.

1) If there is a missing value in either x1,x2 or x3, then it should be ignored if one or two of the other variables have the value 1. So the sum should be calculated although there is a missing value but only if there is at least one 1 (Eg. X = x1 + x2 + x3 = 0 + missing + 1 = 1)

2) If there is a missing value in either x1, x2 or x3, then it should not be ignored if there is no 1 at all and the sum should not be calculated. (Eg. X = x1 + x2 + x3 = 0 + missing + missing = missing).

I tried to make a loop with IF but it won't work and I just can't figure out why.

COMPUTE X =SUM(x1, x2, x3).

IF (x1=-77 AND x2~=1 AND x3~=1) X=999.

IF (x2=-77 AND x1~=1 AND x3~=1) X=999.

IF (x3=-77 AND x1~=1 AND x2 ~=1) X=999.

EXECUTE.

*These are the returned results:

when x1=1, x2 = 0, x3=-77 then X=1. (That is the result I want.

The problem arises when x1=-77, x2=0, x3=0 because then X=0 and not 999 as I want it to be.

I think that with the loop above I am close to the result but something is missing.

Below I post some other loops I made, but neither did work...

Thank you yo much for your help!!

Cheers desperate Lorena :)

COMPUTE X = x1 + x2 + x3.

RECODE X (SYSMIS=-77).

IF ((X = -77 AND x1 = 1) OR (X = -77 AND x2 = 1) OR (X = -77 AND x3 = 1)) X = 1.

EXECUTE.

*Here X is always returned as -77.

*In the next step I tired to create a new variable (Z) because I thought that maybe the problem is that X is the outcome variable as well as one of the conditions in IF.

COMPUTE X = x1 + x2 + x3.

RECODE X (SYSMIS=-77).

COMPUTE Z = 999.

IF ((X = -77 AND x1 = 1) OR (X = -77 AND x2 = 1) OR (X = -77 AND x3 = 1)) Z = 1.

EXECUTE.

*Here X is still returned as -77 and Z stays 999.

## By Ruben Geert van den Berg on August 3rd, 2017

Hi Jacob!

Perhaps try something like

`do if (vara = 1).`

recode varb varc vard (else = 1).

end if.

Also see DO IF.

## By Jacob Christ on August 3rd, 2017

HI Sammy,

I am trying to use an If command to recode several variables. I'm hoping to do something like this:

If (Vara=1)

Varb=Varb.1

Varc=Varc.1

Vard=Vard.1

Execute.

Is there a command I need to place between Varb.1 and the following row so that it performs the action for each row?

Thanks!

## By Ruben Geert van den Berg on May 25th, 2017

Hi Ravshan!

You could use the ANY keyword here:

`if(any(1,a to d)) inno = 1.`

Or even simpler perhaps: if(sum(a to d) >= 1) inno = 1.

Hope that helps!

## By Ravshan on May 25th, 2017

Hi Ruben, thank you for samples. I new in SPSS. I have a question. Hope you will find time to respond :).

How I can analysis:

% of firms innovate if any of the types of innovation they hold. In other words:

X=true if Any of the four variables (a,b,c,d) equal to 1.

Thanks in advance

Regards, Ravshan