SPSS Tutorials

BASICS REGRESSION T-TEST CHI-SQUARE TEST ANOVA

SPSS DO IF Command

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.

cd 'd:downloaded'./*Or wherever "employees.sav" is located.

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.

Previous tutorial: SPSS Datasets

Next tutorial: SPSS Error Saving Dialog Specification

Let me know what you think!

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

This tutorial has 2 comments