<?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 vs where in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312661#M67835</link>
    <description>&lt;P&gt;Can you post the context where it yields a different number of observations? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Generally the where statement specifies which observations are read into the PDV while the subsetting if statement specifies that you output that record only if the condition is met.&lt;/P&gt;</description>
    <pubDate>Fri, 18 Nov 2016 16:14:46 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2016-11-18T16:14:46Z</dc:date>
    <item>
      <title>if vs where</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312653#M67831</link>
      <description>&lt;P&gt;Why does&amp;nbsp;&lt;/P&gt;&lt;P&gt;if ECLASS IN ("F1","F2","M1","M2","O1","O2","VQF") AND &amp;nbsp;RANKREV NOT IN ("5","6") ;&lt;/P&gt;&lt;P&gt;produce a different number of records than&lt;/P&gt;&lt;P&gt;Where ECLASS IN ("F1","F2","M1","M2","O1","O2","VQF") AND &amp;nbsp;RANKREV NOT IN ("5","6") ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only difference is the where vs if.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 16:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312653#M67831</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2016-11-18T16:01:45Z</dc:date>
    </item>
    <item>
      <title>If vs Where</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312651#M67840</link>
      <description>&lt;P&gt;Why does&amp;nbsp;&lt;/P&gt;&lt;P&gt;if ECLASS IN ("F1","F2","M1","M2","O1","O2","VQF") AND &amp;nbsp;RANKREV NOT IN ("5","6") ;&lt;/P&gt;&lt;P&gt;produce a different number of records than&lt;/P&gt;&lt;P&gt;Where ECLASS IN ("F1","F2","M1","M2","O1","O2","VQF") AND &amp;nbsp;RANKREV NOT IN ("5","6") ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only difference is the where vs if.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 16:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312651#M67840</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2016-11-18T16:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: if vs where</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312661#M67835</link>
      <description>&lt;P&gt;Can you post the context where it yields a different number of observations? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Generally the where statement specifies which observations are read into the PDV while the subsetting if statement specifies that you output that record only if the condition is met.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 16:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312661#M67835</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-18T16:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: if vs where</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312664#M67838</link>
      <description>&lt;P&gt;Its because of nulls. &amp;nbsp;If statements keep nulls where's do not.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 16:19:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312664#M67838</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2016-11-18T16:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: if vs where</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312698#M67852</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;Its because of nulls. &amp;nbsp;If statements keep nulls where's do not.&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;Not True, Not True, Not True.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider:&amp;nbsp;&amp;nbsp;In the logical expression&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ECLASS IN ("F1","F2","M1","M2","O1","O2","VQF") AND &amp;nbsp;RANKREV NOT IN ("5","6")&lt;/P&gt;
&lt;P&gt;the only part where a null value should pass is in the "RANKREV NOT IN ("5","6")&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So let's test that "NOT IN" clause with SASHELP.CLASS.&amp;nbsp; Set up&amp;nbsp;data set HAVE with the first six observations from sashelp.class&amp;nbsp;and blank out the name variable for&amp;nbsp;the 5th obs, yielding these six name values, in order:&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;"Alfred", "Alice", "Barbara", "Carol", "", "James"&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then run the logical condition;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; NOT IN ("Alfred","Barbara")&lt;BR /&gt;once via subsetting IF and once&amp;nbsp;via&amp;nbsp;WHERE.&amp;nbsp; The program below shows that results are the same - &lt;EM&gt;&lt;STRONG&gt;i.e. null character values are treated the same by where and subsetting if&lt;/STRONG&gt;&lt;/EM&gt;:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  set sashelp.class (keep=name obs=6);
  if _n_=5 then name=' ';
  put (_n_ name) (:);
run;


data _null_;
  set have;
  if name not in ('Alfred','Carol');  
  put (_n_ name) (:);
run;


data _null_;
  set have;
  where name not in ('Alfred','Carol'); 
  put (_n_ name) (:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what could explain the difference that you get?&amp;nbsp;&amp;nbsp;Notice that the values of _N_ reported via the PUT statements in the program differ between the IF and WHERE tests.&amp;nbsp; That's&amp;nbsp;because the subsetting IF brings every data record into the PDV, assigns an incremented value of _N_&amp;nbsp;and then deletes it when the logical condition is not satisfied..&amp;nbsp; But the WHERE statement outsources the filtering to the input/output engine, so only the successful records are brought into the PDV and therefore assigned an incremented value of _N_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To test this rerun the above program with OPTIONS OBS=3 preceding the first data _null_, and you'll see that the subsetting IF does not report the null value.&amp;nbsp;&amp;nbsp;The reason is&amp;nbsp;not because it is NULL, it's because the null is in&amp;nbsp;record 5 - i.e. beyond the global OBS limit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would that explain your results?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this is your problem, then issue the statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; options obs=max;&lt;/P&gt;
&lt;P&gt;and rerun your programs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;***&amp;nbsp;Edited addition ***&lt;/P&gt;
&lt;P&gt;My response explained why you would get a blank in the WHERE but not the IF but you report the opposite.&amp;nbsp; That can also be reproduced in the program above.&amp;nbsp; Make these changes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Change "IF _N_=5 ..." to "IF _N_=2 ...".&lt;/LI&gt;
&lt;LI&gt;Prior to "data have" make sure you have&lt;/LI&gt;
&lt;OL&gt;
&lt;LI&gt;OPTIONS OBS=MAX;&lt;/LI&gt;
&lt;LI&gt;OPTIONS FIRSTOBS=1;&lt;/LI&gt;
&lt;/OL&gt;
&lt;LI&gt;Prior to the first DATA _NULL_ add:
&lt;OL&gt;
&lt;LI&gt;OPTIONS FIRSTOBS=2;&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rerun the program and you'll see a null character value pass the IF test, but not the WHERE test.&amp;nbsp; Again it's not because of the value, it's due to the location of the record containing the null value vs the value of FIRSTOBS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I'm betting you might have a global OPTIONS FIRSTOBS set in your program.&amp;nbsp;And also that your IF test produces the NULL value at the beginning of the resulting data set.&amp;nbsp; Is that the case?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the take-home message is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; WHERE is honored before FIRSTOBS and OBS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;MK&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2016 00:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/312698#M67852</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-08T00:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: if vs where</title>
      <link>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/317467#M69519</link>
      <description>&lt;P&gt;You can see the behavior differences of &lt;A href="http://blogs.sas.com/content/sasdummy/2016/11/27/if-and-where-sas/" target="_self"&gt;IF and WHERE in the DATA step debugger&lt;/A&gt;, now available in SAS Enterprise Guide (and still available in PC SAS if you're running that).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WHERE rules are determined with the input/output criteria at compile time, so any record that doesn't meet the WHERE test is never loaded into the PDV. &amp;nbsp;IF statements are evaluated on the data values after they are loaded into the PDV.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Dec 2016 00:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/if-vs-where/m-p/317467#M69519</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2016-12-08T00:57:33Z</dc:date>
    </item>
  </channel>
</rss>

