<?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: Using a WHERE AND in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605761#M175818</link>
    <description>&lt;P&gt;I can't see your data so needless to say, this is untested. But I think the problem is missing parenthesis around your conditions. SAS evaluates AND before OR, which I think causes the error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.REPORT_TEST as
select
orderid,
Order_thisMonth,
order_lastMonth
from work.trans_&amp;amp;curmnth.
where (order_lastMonth= 'new' or order_lastMonth= 'old')
and (Order_thisMonth= 'new' or Order_thisMonth= 'old');
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 20 Nov 2019 14:57:28 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-11-20T14:57:28Z</dc:date>
    <item>
      <title>Using a WHERE AND</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605759#M175816</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the code below which doesn't quite work as the second part of the WHERE clause, I still get a mixture of results and not just the ones stated, any idea why please? Within my first part of WHERE, I only get New and Old as stated, but in my second part, I get additional outputs such as New, Old, returned, faulty etc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table work.REPORT_TEST as&lt;BR /&gt;select &lt;BR /&gt;orderid,&lt;BR /&gt;Order_thisMonth,&lt;BR /&gt;order_lastMonth&lt;BR /&gt;from work.trans_&amp;amp;curmnth.&lt;BR /&gt;where order_lastMonth= 'new' or order_lastMonth= 'old'&lt;BR /&gt;and Order_thisMonth= 'new' or Order_thisMonth= 'old';&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 14:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605759#M175816</guid>
      <dc:creator>KC_16</dc:creator>
      <dc:date>2019-11-20T14:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using a WHERE AND</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605761#M175818</link>
      <description>&lt;P&gt;I can't see your data so needless to say, this is untested. But I think the problem is missing parenthesis around your conditions. SAS evaluates AND before OR, which I think causes the error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table work.REPORT_TEST as
select
orderid,
Order_thisMonth,
order_lastMonth
from work.trans_&amp;amp;curmnth.
where (order_lastMonth= 'new' or order_lastMonth= 'old')
and (Order_thisMonth= 'new' or Order_thisMonth= 'old');
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Nov 2019 14:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605761#M175818</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-20T14:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using a WHERE AND</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605762#M175819</link>
      <description>&lt;P&gt;The logical AND operator is evaluated before logical OR.&lt;/P&gt;
&lt;P&gt;So your condition is effectively&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where
  order_lastMonth = 'new' or
  (order_lastMonth = 'old' and Order_thisMonth = 'new') or
  Order_thisMonth = 'old'
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS not only SAS does that, it is a basic rule of mathematical logic and always handled like this.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 15:00:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605762#M175819</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-11-20T15:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using a WHERE AND</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605794#M175829</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I'm having trouble envisioning your data and what you want to get. Let's take some fake data that has your 3 variables in different combinations. Here's some fake data:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="fake_data_before.png" style="width: 374px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34110iE446EE234FEBB43A/image-size/large?v=v2&amp;amp;px=999" role="button" title="fake_data_before.png" alt="fake_data_before.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given your current WHERE clause, it's hard to understand whether you're going to get what you want, since you don't have any parentheses to clarify how the AND logical operator and the OR logical operator should be treated (which one gets precedence). For a concrete example, which of the above 9 rows do you expect your WHERE to select? If I run your original program without parentheses in the WHERE against the above data, THIS is what I get:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="_1_no_parens.png" style="width: 364px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34111i0E17D88E16F4F012/image-size/large?v=v2&amp;amp;px=999" role="button" title="_1_no_parens.png" alt="_1_no_parens.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Is that what you expect given the fake data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's some code to test with my fake data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data trans_Nov;
  infile datalines dlm=',';
  input orderid order_thismonth $ order_lastmonth $;
datalines;
11,new,new
12,new,old
13,old,new
14,old,old
15,aaa,bbb
16,new,ccc
17,old,ddd
18,eee,new
19,fff,old
;
run;
   
proc print data=work.trans_nov;
  title 'What is in Fake data?';
run;
    
proc sql;
create table work.REPORT_TEST1 as
select orderid, Order_thisMonth, order_lastMonth
from work.trans_nov
where order_lastMonth= 'new' or order_lastMonth= 'old'
and Order_thisMonth= 'new' or Order_thisMonth= 'old';
quit;
 
proc print data=work.report_test1;
  title '1) AFTER report_test1 no parentheses';
run;
 
proc sql;
create table work.REPORT_TEST1b as
select orderid, Order_thisMonth, order_lastMonth
from work.trans_nov
WHERE (order_lastMonth='new') or 
      ((order_lastMonth='old') and (Order_thisMonth='new')) or 
      (Order_thisMonth='old');
quit;
 
proc print data=work.report_test1b;
  title '1b) AFTER report_test1b possible parentheses';
run;
 
proc sql;
create table work.REPORT_TEST2 as
select orderid, Order_thisMonth, order_lastMonth
from work.trans_nov
where (order_lastMonth= 'new' or order_lastMonth= 'old')
       and 
      (Order_thisMonth= 'new' or Order_thisMonth= 'old');
quit;
 
proc print data=work.report_test2;
  title '2) AFTER report_test2 WITH alternate parentheses';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice in the results what a difference in results the parentheses make. I would suggest clarifying exactly what rows you want to have returned and then tweak your WHERE clause accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2019 15:37:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-WHERE-AND/m-p/605794#M175829</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2019-11-20T15:37:24Z</dc:date>
    </item>
  </channel>
</rss>

