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') ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.