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.
if product not in ('auto' 'Cd') then output ;
if product not in ('auto' 'Cd') then output ;
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.
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 ;
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.
Ron Fehd logical maven
i think the statement could be simplified by eliminating the output statement.
that is,
if product not in ('auto' 'Cd') ;
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.
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.