DATA Step, Macro, Functions and more

IF clause

Accepted Solution Solved
Reply
Super Contributor
Posts: 435
Accepted Solution

IF clause

I need to extract the data which does not have the products 'auto' and 'cd'. I tried the code below,

data have;

input product $;

datalines;

auto

Cd

WIS

PL

;

run;

data want;

set have;

if product ne 'auto' or product ne 'Cd' then output ;

run;

It is not working. Any possibilities to achive this one If (or where) clause? I know we can achive with two if clauses like below.

if product ne 'auto';

if product ne 'Cd';

Thanks for the suggestions.


Accepted Solutions
Solution
‎05-25-2015 07:55 AM
Super User
Posts: 10,018

Re: IF clause

if product not in ('auto'  'Cd')  then output ;

View solution in original post


All Replies
Solution
‎05-25-2015 07:55 AM
Super User
Posts: 10,018

Re: IF clause

if product not in ('auto'  'Cd')  then output ;

Super Contributor
Super Contributor
Posts: 3,174

Re: IF clause

For the "help me understand why my code doesn't work?" perspective.....

Review your use of OR versus AND in your IF <expression> THEN OUTPUT;    statement.

However, also consider that the two IF statements, as you have them coded, will work correctly, given your desired result.

Super User
Super User
Posts: 7,039

Re: IF clause

You are just having a logic problem.

Your first conditional is of the form  (NOT A or NOT B)   Since it is never possible for A and B to be true at the same time (this would mean that product is both 'auto' and 'Cd' at the same time) your condition is always true.

Your second is of the form (NOT A AND NOT B) and so actually succeeds in sub-setting the data.

So instead of

if product ne 'auto' or product ne 'Cd' then output ;

you want

if product ne 'auto' AND product ne 'Cd' then output ;

You can reformulate (NOT A AND NOT B) to NOT (A OR B) using basic logic and get

if  not (product = 'auto' OR product = 'Cd') then output ;

This can also be written as

if  product not in ('auto','Cd') then output ;

Regular Contributor
Posts: 227

Re: IF clause

Hi Tom

Yes, I agree this a logic problem.

and De Morgan's Rules of conversion of negations are non-trivial.

Here is a program to help others understand nand and nor.

Truth Table - sasCommunity

Ron Fehd  logical maven

New Contributor
Posts: 2

Re: IF clause

i think the statement could be simplified by eliminating the output statement.

that is,

if product not in ('auto'  'Cd')  ;

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 238 views
  • 1 like
  • 6 in conversation