<?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 How to properly  use IF/ELSE statements in SAS in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763203#M39387</link>
    <description>&lt;P&gt;I have a dataset which looks like this.&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data have;
infile datalines truncover;
input type $ quarter phase counter percent avg std;
datalines;
XX-A 202001 1 30 20 20 1
XX-A 202002 1 50 2 20 1
XX-A 202003 1 60 2 20 2
XX-A 202004 1 50 10 20 2
XX-B 202001 2 30 4 30 3
XX-B 202002 2 60 4 30 3
XX-B 202003 2 70 1 30 3
XX-B 202004 2 20 3 30 3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;I want to create two new columns named STATUS1 and STATUS05 based on the following if/case statements:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;(for COLUMN STATUS1)&lt;/STRONG&gt;&lt;BR /&gt;If count is &amp;lt; (avg + std * 1) STATUS1 = 'green'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 1) and &amp;lt; (avg +std * 2)&amp;nbsp;STATUS1 = 'yellow'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 2) and (avg + std * 3)&amp;nbsp;STATUS1 = 'orange'&lt;BR /&gt;if count &amp;gt; (avg + std * 3) STATUS1 = 'red'&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;(for COLUMN STATUS05)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If count is &amp;lt; (avg + std * 0.5)&amp;nbsp; STATUS05 = 'green'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 0.5) and &amp;lt; (avg +std * 2)&amp;nbsp;STATUS05 = 'yellow'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 1) and (avg + std * 3)&amp;nbsp;STATUS05 = 'orange'&lt;BR /&gt;if count &amp;gt; (avg + std * 1.5)&amp;nbsp;STATUS05 = 'red'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Aug 2021 10:30:41 GMT</pubDate>
    <dc:creator>Andalusia</dc:creator>
    <dc:date>2021-08-23T10:30:41Z</dc:date>
    <item>
      <title>How to properly  use IF/ELSE statements in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763203#M39387</link>
      <description>&lt;P&gt;I have a dataset which looks like this.&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data have;
infile datalines truncover;
input type $ quarter phase counter percent avg std;
datalines;
XX-A 202001 1 30 20 20 1
XX-A 202002 1 50 2 20 1
XX-A 202003 1 60 2 20 2
XX-A 202004 1 50 10 20 2
XX-B 202001 2 30 4 30 3
XX-B 202002 2 60 4 30 3
XX-B 202003 2 70 1 30 3
XX-B 202004 2 20 3 30 3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;I want to create two new columns named STATUS1 and STATUS05 based on the following if/case statements:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;(for COLUMN STATUS1)&lt;/STRONG&gt;&lt;BR /&gt;If count is &amp;lt; (avg + std * 1) STATUS1 = 'green'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 1) and &amp;lt; (avg +std * 2)&amp;nbsp;STATUS1 = 'yellow'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 2) and (avg + std * 3)&amp;nbsp;STATUS1 = 'orange'&lt;BR /&gt;if count &amp;gt; (avg + std * 3) STATUS1 = 'red'&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;(for COLUMN STATUS05)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If count is &amp;lt; (avg + std * 0.5)&amp;nbsp; STATUS05 = 'green'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 0.5) and &amp;lt; (avg +std * 2)&amp;nbsp;STATUS05 = 'yellow'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 1) and (avg + std * 3)&amp;nbsp;STATUS05 = 'orange'&lt;BR /&gt;if count &amp;gt; (avg + std * 1.5)&amp;nbsp;STATUS05 = 'red'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Aug 2021 10:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763203#M39387</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-08-23T10:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to properly  use IF/ELSE statements in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763204#M39388</link>
      <description>&lt;P&gt;Although I suspect you already know this, your statements in SAS need to end with a semicolon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also need a THEN in each statement, and there are a few other syntax errors you need to fix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is correct code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if count &amp;lt;= (avg + std * 1) then STATUS1 = 'green';
else if count &amp;gt; (avg + std * 1) and count &amp;lt;= (avg +std * 2) then STATUS1 = 'yellow';
else if count &amp;gt; (avg + std * 2) and count &amp;lt;= (avg + std * 3) then STATUS1 = 'orange';
else if count &amp;gt; (avg + std * 3) then STATUS1 = 'red';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might also want to include the following BEFORE the above code, as it will prevent truncation of the values of STATUS1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length status1 $ 6;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Aug 2021 10:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763204#M39388</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-23T10:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to properly  use IF/ELSE statements in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763206#M39389</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358343"&gt;@Andalusia&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a dataset which looks like this.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data have;
infile datalines truncover;
input type $ quarter phase counter percent avg std;
datalines;
XX-A 202001 1 30 20 20 1
XX-A 202002 1 50 2 20 1
XX-A 202003 1 60 2 20 2
XX-A 202004 1 50 10 20 2
XX-B 202001 2 30 4 30 3
XX-B 202002 2 60 4 30 3
XX-B 202003 2 70 1 30 3
XX-B 202004 2 20 3 30 3&lt;/LI-CODE&gt;
&lt;P&gt;&lt;STRONG&gt;I want to create two new columns named STATUS1 and STATUS05 based on the following if/case statements:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;(for COLUMN STATUS1)&lt;/STRONG&gt;&lt;BR /&gt;If count is &amp;lt; (avg + std * 1) STATUS1 = 'green'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 1) and &amp;lt; (avg +std * 2)&amp;nbsp;STATUS1 = 'yellow'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 2) and (avg + std * 3)&amp;nbsp;STATUS1 = 'orange'&lt;BR /&gt;if count &amp;gt; (avg + std * 3) STATUS1 = 'red'&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;(for COLUMN STATUS05)&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If count is &amp;lt; (avg + std * 0.5)&amp;nbsp; STATUS05 = 'green'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 0.5) and &amp;lt; (avg +std * 2)&amp;nbsp;STATUS05 = 'yellow'&lt;/P&gt;
&lt;P&gt;if count &amp;gt; (avg + std * 1) and (avg + std * 3)&amp;nbsp;STATUS05 = 'orange'&lt;BR /&gt;if count &amp;gt; (avg + std * 1.5)&amp;nbsp;STATUS05 = 'red'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You were almost there but have several import issues. First all statements end in a semicolon.&lt;/P&gt;
&lt;P&gt;Second you have to have the value in the right relationship for comparison. I am not aware of any programming language the "distributes" a variable as you try in the second and third statement. SAS will allow you to place the value between comparison operators:&lt;/P&gt;
&lt;P&gt;Third you need a THEN after the comparison(s) to tell SAS what to do when the condition is true.&lt;/P&gt;
&lt;P&gt;Fourth, the length of character values will be set with the first value used if you do not set it prior. So yours would have been 5 characters long and truncated values for "yellow" and "orange" to "yello" and "orang" because the first value for status "green" set length to 5.&lt;/P&gt;
&lt;P&gt;And last you need another IF in the last comparison.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;length status1 $ 6;
If count is &amp;lt; (avg + std * 1) &lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;THE&lt;/FONT&gt;&lt;FONT color="#0000FF"&gt;N &lt;/FONT&gt;&lt;/STRONG&gt;STATUS1 = 'green';
else if (avg + std * 1) &lt;STRONG&gt;&amp;lt; count  &amp;lt;&lt;/STRONG&gt; (avg +std * 2) Then STATUS1 = 'yellow';
else if   (avg + std * 2)&lt;STRONG&gt;&amp;lt; count &amp;lt;&lt;/STRONG&gt;(avg + std * 3) then STATUS1 = 'orange';
else if count &amp;gt; (avg + std * 3) Then STATUS1 = 'red' ;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Aug 2021 10:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763206#M39389</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-23T10:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to properly  use IF/ELSE statements in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763207#M39390</link>
      <description>Oke and where is the STATUS05? I want STATUS1 and STATUS05 in one view</description>
      <pubDate>Mon, 23 Aug 2021 10:54:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763207#M39390</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-08-23T10:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to properly  use IF/ELSE statements in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763208#M39391</link>
      <description>Where is STATUS05?</description>
      <pubDate>Mon, 23 Aug 2021 10:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763208#M39391</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-08-23T10:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to properly  use IF/ELSE statements in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763213#M39392</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358343"&gt;@Andalusia&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Oke and where is the STATUS05? I want STATUS1 and STATUS05 in one view&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I didn't show code for STATUS05. I expected you would be able to see what changes were made to the STATUS1 code, and make the same changes to the STATUS05 code.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Aug 2021 11:14:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-properly-use-IF-ELSE-statements-in-SAS/m-p/763213#M39392</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-08-23T11:14:17Z</dc:date>
    </item>
  </channel>
</rss>

