<?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 If Condition with and... or ... or... or... is incorrect in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756607#M30082</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test_out;
set work.test_in;
if value &amp;lt; 0 and item= 1 or item = 2  Or  item = 3 Or item =4 Then document= 4;
else if value &amp;gt; 0 and item = 5 or item = 6  Or  item = 7 Or item =8 Then document= 3;
else if value &amp;lt; 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 3;
else if value &amp;gt; 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 4;
else document= ' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi SAS-Experts,&lt;/P&gt;&lt;P&gt;my code is like above and i have following problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When i have e.g. (+) 41 as value with Item-Nr. 10 i get&amp;nbsp; document-Nr. 3 but i expect 4 for document.&lt;/P&gt;&lt;P&gt;The question is, where my mistake is. Maybe i have to clamp something but i dont know what.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would be nice, if u could help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
    <pubDate>Mon, 26 Jul 2021 10:36:44 GMT</pubDate>
    <dc:creator>Hansi_12345</dc:creator>
    <dc:date>2021-07-26T10:36:44Z</dc:date>
    <item>
      <title>If Condition with and... or ... or... or... is incorrect</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756607#M30082</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test_out;
set work.test_in;
if value &amp;lt; 0 and item= 1 or item = 2  Or  item = 3 Or item =4 Then document= 4;
else if value &amp;gt; 0 and item = 5 or item = 6  Or  item = 7 Or item =8 Then document= 3;
else if value &amp;lt; 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 3;
else if value &amp;gt; 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 4;
else document= ' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi SAS-Experts,&lt;/P&gt;&lt;P&gt;my code is like above and i have following problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When i have e.g. (+) 41 as value with Item-Nr. 10 i get&amp;nbsp; document-Nr. 3 but i expect 4 for document.&lt;/P&gt;&lt;P&gt;The question is, where my mistake is. Maybe i have to clamp something but i dont know what.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would be nice, if u could help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 10:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756607#M30082</guid>
      <dc:creator>Hansi_12345</dc:creator>
      <dc:date>2021-07-26T10:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: If Condition with and... or ... or... or... is incorrect</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756609#M30083</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else if value &amp;lt; 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In SAS (and I think&amp;nbsp; most computer languages), AND takes precedence over OR. In other words, the AND is evaluated first, and so your line above is really interpreted as if you had written the line below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else if (value &amp;lt; 0 and item = 9) or item = 10  Or  item = 11 Or item =12 Then document= 3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;because AND is evaluated before OR is evaluated. So when Item=10, it fails the test &lt;FONT face="courier new,courier"&gt;(value&amp;lt;0 and item=9)&lt;/FONT&gt; but then it passes the next test which is &lt;FONT face="courier new,courier"&gt;or&amp;nbsp;item=10&lt;/FONT&gt;, and so DOCUMENT=3 is the result. So, what you should be writing (and it is always a good idea to use parentheses to make it much more visible what you are trying to achieve regardless of the order or precendence)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else if value &amp;lt; 0 and (item = 9 or item = 10 or item = 11 or item =12) then document= 3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 10:51:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756609#M30083</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-26T10:51:08Z</dc:date>
    </item>
    <item>
      <title>Re: If Condition with and... or ... or... or... is incorrect</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756612#M30084</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/389307"&gt;@Hansi_12345&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also avoid the chains of "OR" expressions by using the IN operator:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.test_out;
set work.test_in;
if value &amp;lt; 0 and item in (1:4) or value &amp;gt; 0 and item in (9:12) then document= 4;
else if value &amp;gt; 0 and item in (5:8) or value &amp;lt; 0 and item in (9:12) then document= 3;
else document= .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I replaced the incorrect character missing &lt;FONT face="courier new,courier"&gt;' '&lt;/FONT&gt; by a numeric missing value &lt;FONT face="courier new,courier"&gt;.&lt;/FONT&gt; in the last ELSE statement. Also, remember that a missing value in variable &lt;FONT face="courier new,courier"&gt;value&lt;/FONT&gt; would satisfy the condition &lt;FONT face="courier new,courier"&gt;value &amp;lt; 0&lt;/FONT&gt;, not only negative values. To exclude missing values, you can write&lt;/P&gt;
&lt;PRE&gt;&lt;STRONG&gt;.z &amp;lt;&lt;/STRONG&gt; value &amp;lt; 0&lt;/PRE&gt;
&lt;P&gt;(using the largest special missing value &lt;FONT face="courier new,courier"&gt;.z&lt;/FONT&gt;).&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 11:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756612#M30084</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-07-26T11:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: If Condition with and... or ... or... or... is incorrect</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756615#M30085</link>
      <description>&lt;P&gt;All excellent points from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would modify his code to improve readability only by adding parentheses as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (value &amp;lt; 0 and item in (1:4)) or (value &amp;gt; 0 and item in (9:12)) then document= 4;
else if (value &amp;gt; 0 and item in (5:8)) or (value &amp;lt; 0 and item in (9:12)) then document= 3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To me this makes the code just a little more clear when I read it (or when others read it). It's a "style" thing, obviously it works without adding the extra parentheses.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 11:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756615#M30085</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-07-26T11:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: If Condition with and... or ... or... or... is incorrect</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756616#M30086</link>
      <description>&lt;P&gt;I dont know where i can answer to all of you at once. So Thanks to all of you for your fast answers &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 11:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/756616#M30086</guid>
      <dc:creator>Hansi_12345</dc:creator>
      <dc:date>2021-07-26T11:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: If Condition with and... or ... or... or... is incorrect</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/757087#M30122</link>
      <description>&lt;P&gt;&lt;FONT face="sans-serif" size="2"&gt;I have a similar problem like yesterday but now with ...and... and.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="sans-serif" size="2"&gt;I realy dot now why i am always struggling with this, in vba i have not such problems....&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="sans-serif" size="2"&gt;My Code is like below:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.xyz;
if (a = 4 and b = 11108 and c = 6) Then d = 111;
else if (a = 3 and b = 11219 and c = 6) Then d = 112;
else if (a= 4 and b = 11223 and c = 6) Then d = 113;
else if (a = 3 and b = 11223 and c = 6) Then d = 114;
else if (a = 3 and b = 11203 and c = 6) Then d = 115;
else if (a = 4 and b = 11205 and c = 6) Then d = 116;
else if (a = 3 and b = 11206 and c = 6) Then d = 117;
else if (a = 4 and b = 11200 and c = 6) Then d = 118;
else if (a = 4 and b = 11319 and c = 7) Then d = 119;
else if (a = 4 and b = 11319 and c = 8) Then d = 120;
else if (a = 3 and b = 11319 and c = 7) Then d = 121;
else if (a = 3 and b = 11319 and c = 8) Then d = 122;
else if (a = 4 and b = 11323 and c = 7) Then d = 123;
else if (a = 4 and b = 11323 and c = 8) Then d = 124;
else if (a = 3 and b = 11323 and c = 7) Then d = 125;
else if (a = 3 and b = 11323 and c = 8) Then d = 126;
else if (a = 3 and b = 11303 and c = 7) Then d = 127;
else if (a = 3 and b = 11303 and c = 8) Then d = 128;
else if (a = 4 and b = 11305 and c = 7) Then d = 129;
else if (a = 4 and b = 11305 and c = 8) Then d = 130;
else if (a = 3 and b = 11306 and c = 7) Then d = 131;
else if (a = 3 and b = 11306 and c = 8) Then d = 132;
else if (a = 4 and b = 11300 and c = 7) Then d = 133;
else if (a = 4 and b = 11300 and c = 8) Then d = 134;
else d = 555;
&amp;nbsp; &amp;nbsp; set work.zyx;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I placed the brackets at different places but i didn't find the right one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If i have for a the value 3, for b the value 11319 and for c the value 7, I expect to get 121 as value but what i get is 555.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 10:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/757087#M30122</guid>
      <dc:creator>Hansi_12345</dc:creator>
      <dc:date>2021-07-27T10:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: If Condition with and... or ... or... or... is incorrect</title>
      <link>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/757100#M30123</link>
      <description>&lt;P&gt;Keep in mind that all variables (that are not implicitly or explicitly retained) are set to missing at the start of the data step iteration. This means that you have only missing values when you evaluate your condition.&lt;/P&gt;
&lt;P&gt;Place the SET statement first in the data step.&lt;/P&gt;
&lt;P&gt;For illustration, see these codes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
if age = 13;
set sashelp.class;
run;

data test2;
set sashelp.class;
if age = 13;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jul 2021 11:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/If-Condition-with-and-or-or-or-is-incorrect/m-p/757100#M30123</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-07-27T11:07:18Z</dc:date>
    </item>
  </channel>
</rss>

