<?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: Why where clause did not work? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726670#M225830</link>
    <description>&lt;P&gt;How the brackets should be set in the where clause?? You have bot OR and AND operator there so the condition my be evaluated different that you expect it to be.&lt;/P&gt;
&lt;P&gt;Look into the log and see how SAS resolved the condition from the where clause and if it is what you expect it to be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filter;
	set var_cal;
	where n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ &amp;gt; 0
		and
		tpci='0'
		and
		( prcstd=3 or (prcstd=4 and LOC='CAN') or prcstd=10 ) /* brackets setup 1*/
		and
		input(SIC, 4.) not in (4900:4949, 6000:6999)
		and
		raw_return&amp;lt;2
	;
run;

data filter;
	set var_cal;
	where ( n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ &amp;gt; 0
		and
		tpci='0'
		and
		prcstd=3)  or  ( (prcstd=4 and LOC='CAN') ) or ( prcstd=10 /* brackets setup 2*/
		and
		input(SIC, 4.) not in (4900:4949, 6000:6999)
		and
		raw_return&amp;lt;2)
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Tue, 16 Mar 2021 09:53:35 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2021-03-16T09:53:35Z</dc:date>
    <item>
      <title>Why where clause did not work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726665#M225826</link>
      <description>&lt;P&gt;Hi all SAS Users,&lt;/P&gt;
&lt;P&gt;Today I faced a problem that where clause did not work in my datastep&lt;/P&gt;
&lt;P&gt;My code is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filter;
	set var_cal;
	where n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ &amp;gt; 0
		and
		tpci='0'
		and
		prcstd=3 or (prcstd=4 and LOC='CAN') or prcstd=10
		and
		input(SIC, 4.) not in (4900:4949, 6000:6999)
		and
		raw_return&amp;lt;2
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have a look on the dataset and see that in the dataset filter, return &amp;gt;2 still exists, could you please let me know what is wrong with my code? This is the proc means for variable "&lt;STRONG&gt;raw_return&lt;/STRONG&gt;" in two files.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_0-1615887967685.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55992iB861E52DB9CA9770/image-size/medium?v=v2&amp;amp;px=400" role="button" title="My97_0-1615887967685.png" alt="My97_0-1615887967685.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warmest regards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 09:46:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726665#M225826</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-16T09:46:35Z</dc:date>
    </item>
    <item>
      <title>Re: Why where clause did not work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726669#M225829</link>
      <description>&lt;P&gt;Because of the precedence of AND before OR, your condition is equivalent to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where
  n(ajexdi,prccd_abs_,trfd)=3
  or (
    n(prccd_abs_,prchd,prcld)=3
    and
    prccd_abs_ &amp;gt; 0
    and
    tpci='0'
    and
    prcstd=3
  )
  or (
    prcstd=4
    and
    LOC='CAN'
  )
  or (
    prcstd=10
    and
    input(SIC, 4.) not in (4900:4949, 6000:6999)
    and
    raw_return&amp;lt;2
  )
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Check if this is what you intended.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 09:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726669#M225829</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-16T09:51:40Z</dc:date>
    </item>
    <item>
      <title>Re: Why where clause did not work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726670#M225830</link>
      <description>&lt;P&gt;How the brackets should be set in the where clause?? You have bot OR and AND operator there so the condition my be evaluated different that you expect it to be.&lt;/P&gt;
&lt;P&gt;Look into the log and see how SAS resolved the condition from the where clause and if it is what you expect it to be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data filter;
	set var_cal;
	where n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ &amp;gt; 0
		and
		tpci='0'
		and
		( prcstd=3 or (prcstd=4 and LOC='CAN') or prcstd=10 ) /* brackets setup 1*/
		and
		input(SIC, 4.) not in (4900:4949, 6000:6999)
		and
		raw_return&amp;lt;2
	;
run;

data filter;
	set var_cal;
	where ( n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ &amp;gt; 0
		and
		tpci='0'
		and
		prcstd=3)  or  ( (prcstd=4 and LOC='CAN') ) or ( prcstd=10 /* brackets setup 2*/
		and
		input(SIC, 4.) not in (4900:4949, 6000:6999)
		and
		raw_return&amp;lt;2)
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 09:53:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726670#M225830</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-16T09:53:35Z</dc:date>
    </item>
    <item>
      <title>Re: Why where clause did not work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726762#M225880</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank"&gt;@KurtBremser&lt;/A&gt;&amp;nbsp; and&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763" target="_blank"&gt;@yabwon&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your explanation, it is my fault that I forget to set the brackets reasonably. My new code is as below, it means that the new code need to satisfy all rows from "where" clause.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data filter;
	set var_cal;
	where (n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3)
		and
		prccd_abs_ &amp;gt; 0
		and
		tpci='0'
		and
		(prcstd=3 or (prcstd=4 and LOC='CAN') or prcstd=10)
		and
		(input(SIC, 4.) not in (4900:4949, 6000:6999))
		and
		raw_return&amp;lt;2
	;
run;&lt;/LI-CODE&gt;
&lt;P&gt;However, the log is quite strange to me regarding the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;(input(SIC, 4.) not in (4900:4949, 6000:6999))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The log is&lt;/P&gt;
&lt;PRE&gt;NOTE: There were 8368199 observations read from the data set WORK.VAR_CAL.
      WHERE ((N(ajexdi, prccd_abs_, trfd)=3) or (N(prccd_abs_, prchd, prcld)=3)) and (prccd_abs_&amp;gt;0) and (tpci='0') and (prcstd in 
      (3, 10) or ((prcstd=4) and (LOC='CAN'))) and (raw_return&amp;lt;200) and &lt;STRONG&gt;((INPUT(SIC, 4.) not = INT(INPUT(SIC, 4.))) or  not 
      ((INPUT(SIC, 4.)&amp;gt;=4900 and INPUT(SIC, 4.)&amp;lt;=4949) or (INPUT(SIC, 4.)&amp;gt;=6000 and INPUT(SIC, 4.)&amp;lt;=6999)))&lt;/STRONG&gt;;&lt;/PRE&gt;
&lt;P&gt;What I want to achieve for such line of code is to exclude all firms having SIC (4-digit number under character type) in&amp;nbsp;&lt;CODE class=" language-sas"&gt;(4900:4949, 6000:6999)&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;I do not know why the log shows this one (I did not set this condition in my filter)&lt;/P&gt;
&lt;PRE&gt;(INPUT(SIC, 4.) not = INT(INPUT(SIC, 4.))) or &lt;/PRE&gt;
&lt;P&gt;Can you please help me to sort it out?&lt;/P&gt;
&lt;P&gt;Warm regards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 13:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726762#M225880</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-16T13:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Why where clause did not work?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726780#M225895</link>
      <description>&lt;P&gt;This:&lt;/P&gt;
&lt;PRE&gt;(INPUT(SIC, 4.) not = INT(INPUT(SIC, 4.)))&lt;/PRE&gt;
&lt;P&gt;is to exclude non-integers from possible list of values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;B.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 14:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-where-clause-did-not-work/m-p/726780#M225895</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-16T14:43:49Z</dc:date>
    </item>
  </channel>
</rss>

