{"id":305,"date":"2025-09-29T03:10:52","date_gmt":"2013-09-11T13:30:13","guid":{"rendered":"http:\/\/www.pythonforspss.org\/?p=305"},"modified":"2024-01-03T09:09:33","modified_gmt":"2024-01-03T07:09:33","slug":"spss-if-command","status":"publish","type":"post","link":"https:\/\/www.spss-tutorials.com\/spss-if-command\/","title":{"rendered":"SPSS IF &#8211; A Quick Tutorial"},"content":{"rendered":"<!--body-->\n\n<p>In SPSS, IF computes a variable for a subset of cases<strong><\/strong>. For <em>analyzing<\/em> a subset of cases, use <a href=\"https:\/\/www.spss-tutorials.com\/spss-filter-command\/\">FILTER<\/a> or <a href=\"https:\/\/www.spss-tutorials.com\/spss-select-if-command\/\">SELECT IF<\/a> instead.\n<\/p>\n\n<div class=\"embed-youtube dbid-0305\">\n    <iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/X-zQwkcPj-E?si=5BgtfMnNnTPykkPt\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen>\n    <\/iframe>\n<\/div>\n\n<ul> \n    <li><a href=\"#example-1-flag-cases-based-on-date-function\">Example 1 - Flag Cases Based on Date Function<\/a><\/li> \n    <li><a href=\"#example-2-replace-range-of-values-by-function\">Example 2 - Replace Range of Values by Function<\/a><\/li> \n    <li><a href=\"#example-3-compute-variable-differently-based-on-gender\">Example 3 - Compute Variable Differently Based on Gender<\/a><\/li> \n    <li><a href=\"#spss-if-versus-do-if\">SPSS IF Versus DO IF<\/a><\/li> \n    <li><a href=\"#spss-if-versus-recode\">SPSS IF Versus RECODE<\/a><\/li> \n<\/ul> \n\n<span class = \"img w720\"> \n    <img src='https:\/\/spss-tutorials.com\/img\/spss-if-tutorial-replace-range-by-function.png' alt = \"SPSS If Tutorial Replace Range By Function\"> \n<\/span>\n\n\n<h2>Data File Used for Examples<\/h2>\n\n<p>All examples use <a href=\"https:\/\/www.spss-tutorials.com\/downloads\/bank.sav\">bank.sav<\/a>, 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.<\/p>\n\n<span class = \"img w720\"> \n    <img src='https:\/\/spss-tutorials.com\/img\/spss-if-tutorial-data-view.png' alt = \"SPSS If Tutorial Data View\"> \n<\/span> \n\n \n<h2 id=\"example-1-flag-cases-based-on-date-function\">Example 1 - Flag Cases Based on Date Function<\/h2> \n\n<p>Let's flag all respondents born during the 80&rsquo;s. The <a href=\"https:\/\/www.spss-tutorials.com\/spss-syntax\/ \">syntax<\/a> 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 <a href=\"https:\/\/www.spss-tutorials.com\/spss-range-function\/\">RANGE<\/a> 1980 through 1989.\n<\/p>\n\n<div class='code'><strong>*Create new variable holding only zeroes.<br><\/strong>compute born80s = 0.<br><br><strong>*Set value to 1 if respondent born between 1980 and 1989.<br><\/strong>if(range(xdate.year(dob),1980,1989)) born80s = 1.<br>execute.<br><br><strong>*Optionally: add value labels.<br><\/strong>add value labels born80s 0 &#39;Not born during 80s&#39; 1 &#39;Born during 80s&#39;.<\/div><!--class='code'-->\n\n<h2>Result<\/h2>\n\n<span class = \"img w720\"> \n    <img src='https:\/\/spss-tutorials.com\/img\/spss-if-tutorial-flag-selection-of-cases.png' alt = \"SPSS If Tutorial Flag Selection Of Cases\"> \n<\/span> \n \n\n<h2 id=\"example-2-replace-range-of-values-by-function\">Example 2 - Replace Range of Values by Function<\/h2> \n\n<p>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 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Kim_Jong-un\">Kim Jong Un<\/a> wouldn't claim he works 160 hours per week!<br>\nWe assume these respondents filled out their <em>monthly<\/em> -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.<\/p>\n \n<div class='code'><strong>*Sort cases descendingly on weekly hours.<br><\/strong>sort cases by whours (d).<br><br><strong>*Divide 160 or more hours by 4.33 (average weeks per month).<br><\/strong>if(whours >= 160) whours = whours \/ 4.33.<br>execute.<br><\/div><!--class='code'-->\n\n<h2>Result<\/h2>\n \n<span class = \"img w720\"> \n    <img src='https:\/\/spss-tutorials.com\/img\/spss-if-tutorial-replace-range-by-function.png' alt = \"SPSS If Tutorial Replace Range By Function\"> \n<\/span>\n\n\n\n<h2>Note<\/h2>\n\n<p>We could have done this correction with <a href=\"https:\/\/www.spss-tutorials.com\/spss-recode-command\/ \">RECODE<\/a> as well:\n<span class='code'>RECODE whours (160 = 36.95)(180 = 41.57).<\/span>\nNote, however, that RECODE becomes tedious insofar as we must correct more distinct values. It works reasonably for this variable but IF works great for <em>all<\/em> variables.<\/p>\n\n\n<h2 id=\"example-3-compute-variable-differently-based-on-gender\">Example 3 - Compute Variable Differently Based on Gender<\/h2> \n\n<p>We'll now flag cases who work fulltime. However, &ldquo;fulltime&rdquo; 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.<\/p>\n \n<div class='code'><strong>*Compute fulltime holding only zeroes.<br><\/strong>compute fulltime = 0.<br><br><strong>*Set fulltime to 1 if whours >= 36 for females or whours >= 40 for males.<br><\/strong>if(gender = 0 & whours >= 36) fulltime = 1.<br>if(gender = 1 & whours >= 40) fulltime = 1.<br><br><strong>*Optionally, add value labels.<br><\/strong>add value labels fulltime 0 &#39;Not working fulltime&#39; 1 &#39;Working fulltime&#39;.<br><br><strong>*Quick check.<br><\/strong>means whours by gender by fulltime <br>\/cells min max mean stddev.<br><\/div><!--class='code'-->\n<h2>Result<\/h2>\n\n<p>Our syntax ends with a <a href=\"https:\/\/www.spss-tutorials.com\/spss-means-command\/\">MEANS<\/a> table showing minima, maxima, means and standard deviations per gender per group. This table -shown below- is a nice way to check the results.<\/p>\n\n<span class = \"img w720\"> \n    <img src='https:\/\/spss-tutorials.com\/img\/spss-if-check-result-by-means-table.png' alt = \"SPSS If Check Result By Means Table\"> \n<\/span> \n\n<p>The <strong>maximum<\/strong> for females <em>not<\/em> working fulltime is below 36. The <strong>minimum<\/strong> for females working fulltime is 36. And so on.<\/p>\n \n\n<h2 id=\"spss-if-versus-do-if\">SPSS IF Versus DO IF<\/h2>\n\n<p>Some <a href=\"https:\/\/www.spss-tutorials.com\/spss-what-is-it\/\">SPSS<\/a> users may be familiar with <a href=\"https:\/\/www.spss-tutorials.com\/spss-do-if-command\/\">DO IF<\/a>. The main differences between DO IF and IF are that<\/p>\n\n<ul>\n\t<li>IF is a single line command while DO IF requires at least 3 lines: DO IF, some transformation(s) and END IF.<\/li>\n\t<li>IF is a conditional <a href=\"https:\/\/www.spss-tutorials.com\/spss-compute-command\/\">COMPUTE<\/a> command whereas DO IF can affect other transformations -such as RECODE or COUNT- as well.<\/li>\n\t<li>If cases meet more than 1 condition, the <em>first<\/em> condition prevails when using DO IF - ELSE IF. If you use multiple IF commands instead, the <em>last<\/em> condition met by each case takes effect. The syntax below sketches this idea.<\/li>\n<\/ul>\n\n\n<h2>DO IF - ELSE IF Versus Multiple IF Commands<\/h2>\n\n<div class='code'><strong>*DO IF: respondents meeting both conditions get result_1.<br><\/strong>do if(condition_1).<br>result_1.<br>else if(condition_2). \/*excludes cases meeting condition_1.<br>result_2.<br>end if.<br><br><strong>*IF: respondents meeting both conditions get result_2.<br><\/strong>if(condition_1) result_1.<br>if(condition_2) result_2. \/*includes cases meeting condition_1.<br><\/div><!--class='code'-->\n\n<h2 id=\"spss-if-versus-recode\">SPSS IF Versus RECODE<\/h2>\n\n<p>In many cases, RECODE is an easier alternative for IF. However, RECODE has more limitations too.<br>\nFirst off, RECODE only replaces (ranges of) constants -such as 0, 99 or <a href=\"https:\/\/www.spss-tutorials.com\/spss-missing-values\/#spss-system-missing-values\">system missing values<\/a>- by other constants. So something like\n<span class='code'>recode overall (sysmis = q1).<\/span>\nis <strong>not possible<\/strong> -q1 is a variable, not a constant- but\n<span class='code'>if(sysmis(overall)) overall = q1.<\/span>\nworks 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.<br><br>\nSecond, RECODE can only set values based on a single variable. This is the reason why\n<span class='def'>you can't recode 2 variables into one<\/span>\nbut you can use an IF condition involving multiple variables:\n<span class='code'>if(gender = 0 & whours >= 36) fulltime = 1.<\/span>\nis perfectly possible.<br><br>\nYou 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.\n<\/p>\n\t\n\t\n<h2>Example 4 - Compute Variable Differently Based on Gender II<\/h2> \n\n<div class='code'><strong>*Recode whours into fulltime for everyone.<br><\/strong>recode whours (40 thru hi = 1)(else = 0) into fulltime2.<br><br><strong>*Apply different recode for female respondents.<br><\/strong>do if(gender = 0).<br>recode whours (36 thru hi = 1)(else = 0) into fulltime2.<br>end if.<br><br><strong>*Optionally, add value labels.<br><\/strong>add value labels fulltime2 0 &#39;Not working fulltime&#39; 1 &#39;Working fulltime&#39;.<br><br><strong>*Quick check.<br><\/strong>means whours by gender by fulltime2 <br>\/cells min max mean stddev.<br><\/div><!--class='code'-->\n\n<h2>Final Notes<\/h2>\n\n<p>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.<\/p>\n\n<p><strong>Thanks for reading!<\/strong><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>In SPSS, IF computes a new or existing variable but for a selection of cases only.<\/p>\n<p>For example:<br \/>\n<span class=\"code\">IF(GENDER = 0) SCORE = MEAN(Q1 TO Q5).<\/span><br \/>\ncomputes &ldquo;score&rdquo; as the mean over variables Q1 to Q5 but only for cases whose gender is 0 (female).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[253,294],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-ii","category-selecting-cases-in-spss"],"_links":{"self":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":0,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spss-tutorials.com\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}