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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

View solution in original post

5 REPLIES 5
Ksharp
Super User

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

sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

Tom
Super User Tom
Super User

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 ;

Ron_MacroMaven
Lapis Lazuli | Level 10

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

yukclam9
Calcite | Level 5

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

that is,

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 924 views
  • 1 like
  • 6 in conversation