<?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: Conditional statements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814174#M321389</link>
    <description>&lt;P&gt;You separated three conditions by binary operators.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;V1 and V2 or V3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There are two ways to group those three conditions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(V1 and V2) or V3
V1 and (V2 or V3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's run an experiment by testing all possible combinations of true or false for each of the three conditions and calculate all three expressions.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  do v1=0,1;
  do v2=0,1;
  do v3=0,1;
    a=v1 and v2 or v3;
    b=v1 and (v2 or v3);
    c=(v1 and v2) or v3;
    output;
  end;
  end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1652904986733.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71612i7AABB881270909D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1652904986733.png" alt="Tom_0-1652904986733.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;As you can see the calculate A column is the same as the C column.&lt;/P&gt;
&lt;P&gt;So SAS grouped the operations to perform the AND before the OR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The meaning of (V1 and V2) or V3 is that either both V1 and V2 are true or V3 is true.&lt;/P&gt;
&lt;P&gt;The meaning of V1 and (V2 or V3) is that&amp;nbsp; V1 is true and also at least one of V2 or V3 is true.&lt;/P&gt;</description>
    <pubDate>Wed, 18 May 2022 20:22:19 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-05-18T20:22:19Z</dc:date>
    <item>
      <title>Conditional statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814172#M321388</link>
      <description>&lt;P&gt;Could someone please clearly explain the difference, ideally with pictures of tables and selected observations?:&lt;/P&gt;
&lt;P&gt;A) if v1=1 and v2=2 or v3=3;&lt;/P&gt;
&lt;P&gt;B) if v1=1 and (v2=2 or v3=3);&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 May 2022 20:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814172#M321388</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-05-18T20:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814174#M321389</link>
      <description>&lt;P&gt;You separated three conditions by binary operators.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;V1 and V2 or V3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There are two ways to group those three conditions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(V1 and V2) or V3
V1 and (V2 or V3)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Let's run an experiment by testing all possible combinations of true or false for each of the three conditions and calculate all three expressions.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  do v1=0,1;
  do v2=0,1;
  do v3=0,1;
    a=v1 and v2 or v3;
    b=v1 and (v2 or v3);
    c=(v1 and v2) or v3;
    output;
  end;
  end;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1652904986733.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71612i7AABB881270909D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1652904986733.png" alt="Tom_0-1652904986733.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;As you can see the calculate A column is the same as the C column.&lt;/P&gt;
&lt;P&gt;So SAS grouped the operations to perform the AND before the OR.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The meaning of (V1 and V2) or V3 is that either both V1 and V2 are true or V3 is true.&lt;/P&gt;
&lt;P&gt;The meaning of V1 and (V2 or V3) is that&amp;nbsp; V1 is true and also at least one of V2 or V3 is true.&lt;/P&gt;</description>
      <pubDate>Wed, 18 May 2022 20:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814174#M321389</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-18T20:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814176#M321390</link>
      <description>&lt;P&gt;Here is a brief example of creating some observations where the comparison is different for the two statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   do v1=1 to 4;
     do v2 = 1 to 4;
       do v3 = 1 to 4;
          a= v1=1 and v2=2 or v3=3;
          b=  v1=1 and (v2=2 or v3=3);
          if a ne b then output;
       end;
     end;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;1 for either A or B would indicate the statement is True, 0 is false. The above code only selects those that are different.&lt;/P&gt;
&lt;P&gt;You could remove the "if a ne b then" to get the comparison values for all the values in the output set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remember to work from inside parentheses outward.&lt;/P&gt;
&lt;P&gt;You may want to look up "Order of Evaluation in Compound Expressions" in the SAS documentation&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically A and B is true only when both elements are.&lt;/P&gt;
&lt;P&gt;A or B is true when either or both elements is true. So when you have comparisons at the same priority of evaluation you do left to right.&lt;/P&gt;</description>
      <pubDate>Wed, 18 May 2022 20:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814176#M321390</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-05-18T20:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814189#M321398</link>
      <description>&lt;P&gt;You are right &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;, there are hidden parenthesis around v1 and v2 because of sequential evaluation. That decouples v3 condition from v1 condition. So v3=3 can be true on its own in (A). In (B), SAS first looks inside the parenthesis to see if either condition is true. If v3=3, that is not enough because then SAS checks the v1 condition which must also be true.&lt;/P&gt;</description>
      <pubDate>Wed, 18 May 2022 22:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814189#M321398</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-05-18T22:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814212#M321404</link>
      <description>&lt;P&gt;In Boolean logic, AND takes precedence over OR. This means that your first condition is equivalent to this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if (v1=1 and v2=2) or v3=3;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 May 2022 06:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditional-statements/m-p/814212#M321404</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-05-19T06:23:25Z</dc:date>
    </item>
  </channel>
</rss>

