<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: if-then in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-then/m-p/561047#M157004</link>
    <description>&lt;P&gt;Your use of "or" as a logical operator causes this, combined with the lack of "else"s to prevent entering the other branches.&lt;/P&gt;
&lt;P&gt;There are several solutions to your problem:&lt;/P&gt;
&lt;P&gt;Use "and" instead of "or":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age;
if age &amp;lt; 55 then age2 = 1;
if age &amp;gt;= 55 and age &amp;lt; 65 then age2 = 2;
if age &amp;gt;= 65 and age &amp;lt; 75 then age2 = 3;
if age &amp;gt;= 75 and age &amp;lt; 85 then age2 = 4;
if age &amp;gt;= 85 then age2 = 5;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use "else":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age;
if age &amp;lt; 55 then age2 = 1;
else if age &amp;lt; 65 then age2 = 2;
else if age &amp;lt; 75 then age2 = 3;
else if age &amp;lt; 85 then age2 = 4;
else age2 = 5;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use a select block:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age;
select;
  when (age &amp;lt; 55) age2 = 1;
  when (age &amp;lt; 65) age2 = 2;
  when (age &amp;lt; 75) age2 = 3;
  when (age &amp;lt; 85) age2 = 4;
  otherwise age2 = 5;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use a format with ranges:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value myage
  low -&amp;lt; 55 = 1
  55 -&amp;lt; 65 = 2
  65 -&amp;lt; 75 = 3
  75 -&amp;lt; 85 = 4
  85 - high = 5
;
run;

data age1;
set age;
age2 = input(put(age,myage.),best.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The additional input() function is needed to make age2 numeric.&lt;/P&gt;</description>
    <pubDate>Thu, 23 May 2019 05:54:51 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-05-23T05:54:51Z</dc:date>
    <item>
      <title>if-then</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then/m-p/561020#M156989</link>
      <description>&lt;P&gt;why this is wrong?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age.age;
if age &amp;lt; 55 then age2 = 1;
if age &amp;gt;=55 or age &amp;lt; 65 then age2 = 2;
if age &amp;gt;=65 or age &amp;lt;75 then age2 = 3;
if age &amp;gt;=75 or age &amp;lt; 85 then age2 = 4;
else age2 = 5;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 May 2019 02:46:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then/m-p/561020#M156989</guid>
      <dc:creator>SChander1</dc:creator>
      <dc:date>2019-05-23T02:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: if-then</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then/m-p/561021#M156990</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/267351"&gt;@SChander1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;why this is wrong?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age.age;
if age &amp;lt; 55 then age2 = 1;
if age &amp;gt;=55 or age &amp;lt; 65 then age2 = 2;
if age &amp;gt;=65 or age &amp;lt;75 then age2 = 3;
if age &amp;gt;=75 or age &amp;lt; 85 then age2 = 4;
else age2 = 5;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just pick a number and see how the values of AGE2 change as the IF statements are evaluated.&lt;/P&gt;
&lt;P&gt;So if AGE=30 then it is less than 50 so AGE2 is set to 1.&lt;/P&gt;
&lt;P&gt;Then since it is also less than 65 it is set to 2.&lt;/P&gt;
&lt;P&gt;etc.&lt;/P&gt;
&lt;P&gt;So everything less than 85 will have AGE2 set to 4 and values equal to or greater than 85 will be set to 5.&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 02:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then/m-p/561021#M156990</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-23T02:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: if-then</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-then/m-p/561047#M157004</link>
      <description>&lt;P&gt;Your use of "or" as a logical operator causes this, combined with the lack of "else"s to prevent entering the other branches.&lt;/P&gt;
&lt;P&gt;There are several solutions to your problem:&lt;/P&gt;
&lt;P&gt;Use "and" instead of "or":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age;
if age &amp;lt; 55 then age2 = 1;
if age &amp;gt;= 55 and age &amp;lt; 65 then age2 = 2;
if age &amp;gt;= 65 and age &amp;lt; 75 then age2 = 3;
if age &amp;gt;= 75 and age &amp;lt; 85 then age2 = 4;
if age &amp;gt;= 85 then age2 = 5;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use "else":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age;
if age &amp;lt; 55 then age2 = 1;
else if age &amp;lt; 65 then age2 = 2;
else if age &amp;lt; 75 then age2 = 3;
else if age &amp;lt; 85 then age2 = 4;
else age2 = 5;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use a select block:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data age1;
set age;
select;
  when (age &amp;lt; 55) age2 = 1;
  when (age &amp;lt; 65) age2 = 2;
  when (age &amp;lt; 75) age2 = 3;
  when (age &amp;lt; 85) age2 = 4;
  otherwise age2 = 5;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use a format with ranges:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value myage
  low -&amp;lt; 55 = 1
  55 -&amp;lt; 65 = 2
  65 -&amp;lt; 75 = 3
  75 -&amp;lt; 85 = 4
  85 - high = 5
;
run;

data age1;
set age;
age2 = input(put(age,myage.),best.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The additional input() function is needed to make age2 numeric.&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2019 05:54:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-then/m-p/561047#M157004</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-05-23T05:54:51Z</dc:date>
    </item>
  </channel>
</rss>

