BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

Hi all SAS Users,

Today I faced a problem that where clause did not work in my datastep

My code is

data filter;
	set var_cal;
	where n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ > 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<2
	;
run;

I have a look on the dataset and see that in the dataset filter, return >2 still exists, could you please let me know what is wrong with my code? This is the proc means for variable "raw_return" in two files.

My97_0-1615887967685.png

 

Warmest regards.

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

This:

(INPUT(SIC, 4.) not = INT(INPUT(SIC, 4.)))

is to exclude non-integers from possible list of values.

 

B.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Because of the precedence of AND before OR, your condition is equivalent to:

where
  n(ajexdi,prccd_abs_,trfd)=3
  or (
    n(prccd_abs_,prchd,prcld)=3
    and
    prccd_abs_ > 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<2
  )
;

Check if this is what you intended.

yabwon
Onyx | Level 15

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.

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.

 

For example:

data filter;
	set var_cal;
	where n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ > 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<2
	;
run;

data filter;
	set var_cal;
	where ( n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3
		and
		prccd_abs_ > 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<2)
	;
run;

All the best 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Phil_NZ
Barite | Level 11

Hi @KurtBremser  and @yabwon 

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.

data filter;
	set var_cal;
	where (n(ajexdi,prccd_abs_,trfd)=3 or n(prccd_abs_,prchd,prcld)=3)
		and
		prccd_abs_ > 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<2
	;
run;

However, the log is quite strange to me regarding the code

(input(SIC, 4.) not in (4900:4949, 6000:6999))

The log is

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_>0) and (tpci='0') and (prcstd in 
      (3, 10) or ((prcstd=4) and (LOC='CAN'))) and (raw_return<200) and ((INPUT(SIC, 4.) not = INT(INPUT(SIC, 4.))) or  not 
      ((INPUT(SIC, 4.)>=4900 and INPUT(SIC, 4.)<=4949) or (INPUT(SIC, 4.)>=6000 and INPUT(SIC, 4.)<=6999)));

What I want to achieve for such line of code is to exclude all firms having SIC (4-digit number under character type) in (4900:4949, 6000:6999)

I do not know why the log shows this one (I did not set this condition in my filter)

(INPUT(SIC, 4.) not = INT(INPUT(SIC, 4.))) or 

Can you please help me to sort it out?

Warm regards.

 

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
yabwon
Onyx | Level 15

This:

(INPUT(SIC, 4.) not = INT(INPUT(SIC, 4.)))

is to exclude non-integers from possible list of values.

 

B.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 533 views
  • 3 likes
  • 3 in conversation