SPSS TUTORIALS FULL COURSE BASICS ANOVA REGRESSION FACTOR

# SPSS DO IF – Simple Tutorial

SPSS transformations between DO IF ... and END IF are applied only to cases (rows of data) that satisfy one or more conditions. In many cases, IF is a faster way to accomplish the same results.

## SPSS Do If Example

Say we'd like to convert people's monthly income into income classes. We may want to use different cut off points for male and female respondents. In this case, we can first use a RECODE command only for cases whose gender is female. Next, we'll use a different RECODE command for males. The syntax below demonstrates this, using employees.sav.

## SPSS Do If Syntax Example 1

*1. Set default directory and open data file.

get file 'employees.sav'.

*2. Recode command restricted to female respondents.

do if gender = 0.
recode monthly_income (lo thru 2000 = 1)(lo thru 2400 = 2)(lo thru hi = 3) into income_class.
end if.

*3. Recode command restricted to male respondents.

do if gender = 1.
recode monthly_income (lo thru 2500 = 1)(lo thru 3000 = 2)(lo thru hi = 3) into income_class.
end if.

*3. Inspect result.

crosstabs income_class by gender.

## Else If

Although the previous syntax does its job, there's a shorter way to accomplish the exact same result: ELSE IF ... This means that the commands that follow are carried out only for cases who 1) satisfy the current condition(s) and 2) don't satisfy any of the previous conditions. The syntax below shows how to use it.

## SPSS Do If Syntax Example 2

*Different recode commands for females and males.

do if gender = 0.
recode monthly_income (lo thru 2000 = 1)(lo thru 2400 = 2)(lo thru hi = 3) into income_class.
else if gender = 1.
recode monthly_income (lo thru 2500 = 1)(lo thru 3000 = 2)(lo thru hi = 3) into income_class.
end if.

## Else

In a similar vein to ELSE IF ... commands that follow ELSE are carried out for all cases who don't satisfy any of the previous conditions. An important thing to notice here is that ELSE does not include cases for whom previous conditions could not be evaluated due to missing values.
The final syntax example demonstrates this by creating a birth decennium variable using XDATE. Next, FREQUENCIES confirms that respondents whose birthday is unknown are not assigned to any birth decennium.

## SPSS Do If Syntax Example 3

*1. Create birth_decennium variable.

do if xdate.year(date_of_birth) lt 1960.
compute birth_decennium = 1.
else if xdate.year(date_of_birth) lt 1970.
compute birth_decennium = 2.
else if xdate.year(date_of_birth) lt 1980.
compute birth_decennium = 3.
else.
compute birth_decennium = 4.
end if.

*2. Apply value labels to birth_decennium.

value labels birth_decennium 1 '50''s' 2 '60''s' 3 '70''s' 4 '80''s'.

*3. Inspect frequency distribution for birth_decennium.

frequencies birth_decennium.

## Note

Only SPSS transformation commands can be used within DO IF. These exclude most commands that generate output such as FREQUENCIES and DESCRIPTIVES. For using such commands on subsets of cases, see FILTER, SPLIT FILE and SELECT IF.

# Tell us what you think!

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

# THIS TUTORIAL HAS 16 COMMENTS:

• ### By Gee Dcos on July 12th, 2019

Hi Ruben, I have a question. I am using SPSS v. 25 on a Mac to combine values from 3 different variables into one variable using the Compute function with multiple conditions. I used this syntax, but it returns with 0 valid and all missing values. Anybody knows what I'm doing wrong? Thanks in advance for your help! ☺️

DO IF variable1 = 0 and variable2=0 and variable3=0.
COMPUTE newvariable=0.
ELSE IF variable1 = 1 and variable2=1 and variable3=1.
COMPUTE newvariable=1.
ELSE IF variable1 = 7 and variable2=7 and variable3=8.
COMPUTE newvariable=7.
ELSE IF variable1 = 9 and variable2=9 and variable3=9.
COMPUTE newvariable=9.
END IF.
EXECUTE.

• ### By Ruben Geert van den Berg on July 12th, 2019

Hi Gracie!

First off, are you sure none of variable1 to variable3 are string variables?

All system missings could also occur if your values have hidden decimals. You could check for it by using something like

compute check = (variable1 = trunc(variable1)).

If there's no hidden decimals, "check" should result in a column of 1's which you can check by

descriptives check.

Hope that helps!

P.s. you could shorten your syntax a lot by using IF instead of DO IF.

• ### By Gee Dcos on July 15th, 2019

I do not have string variables but I do have missing values. I did try doing 'check' and it confirmed valid values and missing values. Wouldn't the compute function work in this case? I am still having problems with combining these 3 variables. If I shorten the syntax to IF, END IF turns up red (error) towards the end of the syntax. When I revert back to the syntax using DO IF, the whole thing runs with the output valid = 14211, missing = 0 but all the data is coded as 9.

I also tried saving a work file with only these 3 variables, plus the id number and some other basic data of participants. When I run the same syntax, the output is valid = 0, system missing = 14211. I do not understand how I can have one output in one occasion, and a different one on another occasion using the same data. Maybe there's something wrong with my software?

These 3 variables represent data gathered from 3 separate interview dates in a prospective cohort. I wanted to combine the partipants from the 3 different waves into one variable (I have no duplicates). Can you suggest a good way on how to do this?

Would highly appreciate your thoughts on this.

• ### By Ruben Geert van den Berg on July 15th, 2019

It's pretty simple: cases having missing values may not satisfy any of your conditions. So they get a system missing value on the new variable.

Either adjust the DO IF conditions or replace the missing values before running your syntax.

I could look also fix the data for you but I'd have to charge for doing so.

Hope that helps!

Ruben

• ### By Tashe on April 26th, 2020

how do I perform If command (steps of recoding variable for gender with different cuts off points).