Help using Base SAS procedures

Do we have a "where NOT in" statement?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 128
Accepted Solution

Do we have a "where NOT in" statement?

I know this is a very simple question,
We have a " where in (1,2,3,....)" statement
do we have "where not in (1,2,3...)?" or a similar statement?

Thanks!

Accepted Solutions
Solution
‎07-29-2016 10:11 AM
Super Contributor
Posts: 291

Re: Do we have a "where NOT in" statement?

[ Edited ]

yes - just use it as you've written it (almost).

 

where varname not in (1,2,3);


Dare to experiment - error messages are free!

 

Editor's note: consolidating some other helpful comments in this solution...

 

@bhthomas99 asks about the NOTIN operator (no space).  @abbess replied:

The NOTIN operator work perfectly, I have tried it my self. Also it is mentioned in the SAS Support that NOT is a prefix wich can be used with other operators.

 

"A prefix operator is an operator that is applied to the variable, constant, function, or parenthetic expression that immediately follows it. The plus sign (+) and minus sign (-) can be used as prefix operators. The word NOT and its equivalent symbols are also prefix operators"

 

See SAS Operators in Expressions (SAS documentation)



 And @Doc_Duke points out that order-of-operations matters:

Be careful combining NOT, AND, and OR; you don't always get what you think, especially if you leave it to SAS to resolve the expression. For instance,

not varname IN (1, 2, 3)

does not give the same answer as

not (varname in (1,2,3)

 

View solution in original post


All Replies
Solution
‎07-29-2016 10:11 AM
Super Contributor
Posts: 291

Re: Do we have a "where NOT in" statement?

[ Edited ]

yes - just use it as you've written it (almost).

 

where varname not in (1,2,3);


Dare to experiment - error messages are free!

 

Editor's note: consolidating some other helpful comments in this solution...

 

@bhthomas99 asks about the NOTIN operator (no space).  @abbess replied:

The NOTIN operator work perfectly, I have tried it my self. Also it is mentioned in the SAS Support that NOT is a prefix wich can be used with other operators.

 

"A prefix operator is an operator that is applied to the variable, constant, function, or parenthetic expression that immediately follows it. The plus sign (+) and minus sign (-) can be used as prefix operators. The word NOT and its equivalent symbols are also prefix operators"

 

See SAS Operators in Expressions (SAS documentation)



 And @Doc_Duke points out that order-of-operations matters:

Be careful combining NOT, AND, and OR; you don't always get what you think, especially if you leave it to SAS to resolve the expression. For instance,

not varname IN (1, 2, 3)

does not give the same answer as

not (varname in (1,2,3)

 

Contributor
Posts: 33

Re: Do we have a "where NOT in" statement?

Bill wrote:
>Dare to experiment - error messages are free!


Thanks for the chuckle - my log is full of Red today.
Trusted Advisor
Posts: 2,113

Re: Do we have a "where NOT in" statement?

More completely, you can put a "not" infix operator in nearly every logical expression, and often outside, e.g.
where not (varname IN (1,2,3));
is a valid statement.

Be careful combining NOT, AND, and OR; you don't always get what you think, especially if you leave it to SAS to resolve the expression. For instance,

not varname IN (1, 2, 3)

does not give the same answer as

not (varname in (1,2,3)
PROC Star
Posts: 1,562

Re: Do we have a "where NOT in" statement?

Thanks for this Doc. I always use NOT before IN, and it never occured to me that

not varname IN (1, 2, 3)

reads as

(not varname) IN (1, 2, 3) which is true is var = 0

I'll keep this is mind, and add this comment so more people have a chance to read the thread.
N/A
Posts: 1

Re: Do we have a "where NOT in" statement?

I have recently seen "NOTIN", which seems to work: however, there is no space between the infix and the operator.  Is this even possible?

New Contributor
Posts: 2

Re: Do we have a "where NOT in" statement?

[ Edited ]

The NOTIN operator work perfectly, I have tried it my self. Also it is mentionned in the SAS Support that NOT is a prefix wich can be used with other operators.

 

"A prefix operator is an operator that is applied to the variable, constant, function, or parenthetic expression that immediately follows it. The plus sign (+) and minus sign (-) can be used as prefix operators. The word NOT and its equivalent symbols are also prefix operators"

 

Source: http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780367.htm

Regular Contributor
Posts: 231

Re: Do we have a "where NOT in" statement?

Although you have a couple of already accepted solutions, I wanted to throw my two cents in as a PROC SQL guy.  I use the NOTIN frequently, especially when querying text:

 

All my data:

proc sql;


select product, count(*)
from sashelp.shoes
group by product
order by product
;

quit;

Results:

Screen Shot 2016-07-29 at 7.46.55 PM.png

Excluding BOOT and SANDAL:

proc sql;


select product, count(*)
from sashelp.shoes

where product notin ('Boot','Sandal')

group by product
order by product
;

quit;

Results:

Screen Shot 2016-07-29 at 7.47.19 PM.png

 

Good luck!

Chris

Has my article or post helped? Please mark as Solution or Like the article!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 29585 views
  • 5 likes
  • 8 in conversation