In SPSS, IF computes a new or existing variable
for a selection of cases.
For analyzing a selection of cases, use FILTER or SELECT IF instead.
- Example 1 - Flag Cases Based on Date Function
- Example 2 - Replace Range of Values by Function
- Example 3 - Compute Variable Differently Based on Gender
- SPSS IF Versus DO IF
- SPSS IF Versus RECODE
Data File Used for Examples
All examples use bank.sav, a short survey of bank employees. Part of the data are shown below. For getting the most out of this tutorial, we recommend you download the file and try the examples for yourself.
Example 1 - Flag Cases Based on Date Function
Let's flag all respondents born during the 80’s. The syntax below first computes our flag variable -born80s- as a column of zeroes. We then set it to one if the year -extracted from the date of birth- is in the RANGE 1980 through 1989.
compute born80s = 0.
*Set value to 1 if respondent born between 1980 and 1989.
if(range(xdate.year(dob),1980,1989)) born80s = 1.
execute.
*Optionally: add value labels.
add value labels born80s 0 'Not born during 80s' 1 'Born during 80s'.
Result
Example 2 - Replace Range of Values by Function
Next, if we'd run a histogram on weekly working hours -whours- we'd see values of 160 hours and over. However, weeks only hold (24 * 7 =) 168 hours. Even Kim Jong Un wouldn't claim he works 160 hours per week!
We assume these respondents filled out their monthly -rather than weekly- working hours. On average, months hold (52 / 12 =) 4.33 weeks. So we'll divide weekly hours by 4.33 but only for cases scoring 160 or over.
sort cases by whours (d).
*Divide 160 or more hours by 4.33 (average weeks per month).
if(whours >= 160) whours = whours / 4.33.
execute.
Result
Note
We could have done this correction with RECODE as well: RECODE whours (160 = 36.95)(180 = 41.57). Note, however, that RECODE becomes tedious insofar as we must correct more distinct values. It works reasonably for this variable but IF works great for all variables.
Example 3 - Compute Variable Differently Based on Gender
We'll now flag cases who work fulltime. However, “fulltime” means 40 hours for male employees and 36 hours for female employees. So we need to use different formulas based on gender. The IF command below does just that.
compute fulltime = 0.
*Set fulltime to 1 if whours >= 36 for females or whours >= 40 for males.
if(gender = 0 & whours >= 36) fulltime = 1.
if(gender = 1 & whours >= 40) fulltime = 1.
*Optionally, add value labels.
add value labels fulltime 0 'Not working fulltime' 1 'Working fulltime'.
*Quick check.
means whours by gender by fulltime
/cells min max mean stddev.
Result
Our syntax ends with a MEANS table showing minima, maxima, means and standard deviations per gender per group. This table -shown below- is a nice way to check the results.
The maximum for females not working fulltime is below 36. The minimum for females working fulltime is 36. And so on.
SPSS IF Versus DO IF
Some SPSS users may be familiar with DO IF. The main differences between DO IF and IF are that
- IF is a single line command while DO IF requires at least 3 lines: DO IF, some transformation(s) and END IF.
- IF is a conditional COMPUTE command whereas DO IF can affect other transformations -such as RECODE or COUNT- as well.
- If cases meet more than 1 condition, the first condition prevails when using DO IF - ELSE IF. If you use multiple IF commands instead, the last condition met by each case takes effect. The syntax below sketches this idea.
DO IF - ELSE IF Versus Multiple IF Commands
do if(condition_1).
result_1.
else if(condition_2). /*excludes cases meeting condition_1.
result_2.
end if.
*IF: respondents meeting both conditions get result_2.
if(condition_1) result_1.
if(condition_2) result_2. /*includes cases meeting condition_1.
SPSS IF Versus RECODE
In many cases, RECODE is an easier alternative for IF. However, RECODE has more limitations too.
First off, RECODE only replaces (ranges of) constants -such as 0, 99 or system missing values- by other constants. So something like
recode overall (sysmis = q1).
is not possible -q1 is a variable, not a constant- but
if(sysmis(overall)) overall = q1.
works fine. You can't RECODE a function -mean, sum or whatever- into anything nor recode anything into a function. You'll need IF for doing so.
Second, RECODE can only set values based on a single variable. This is the reason why
you can't recode 2 variables into one
but you can use an IF condition involving multiple variables:
if(gender = 0 & whours >= 36) fulltime = 1.
is perfectly possible.
You can get around this limitation by combining RECODE with DO IF, however. Like so, our last example shows a different route to flag fulltime working males and females using different criteria.
Example 4 - Compute Variable Differently Based on Gender II
recode whours (40 thru hi = 1)(else = 0) into fulltime2.
*Apply different recode for female respondents.
do if(gender = 0).
recode whours (36 thru hi = 1)(else = 0) into fulltime2.
end if.
*Optionally, add value labels.
add value labels fulltime2 0 'Not working fulltime' 1 'Working fulltime'.
*Quick check.
means whours by gender by fulltime2
/cells min max mean stddev.
Final Notes
This tutorial presented a brief discussion of the IF command with a couple of examples. I hope you found them helpful. If I missed anything essential, please throw me a comment below.
Thanks for reading!
THIS TUTORIAL HAS 61 COMMENTS:
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 ;-)
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 Daisylyn Chin on November 1st, 2016
Is there a limit to how many operands, operators allowed in a "if" statement?
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
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 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?