BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
I have a dataset which is created using datastep as shown below.
data sample;
input x y $;
datalines;
12 as
15 24
14 aq
16 26
10 15
;
run;

I want the output dataset to be
x y
15 24
16 26
10 15
i.e. subsetting the obeservation where y should have only numeric values.

When I use the SAS code,

data sample1;
set sample;
if y ne 'as' or y ne 'aq';
run;
I'm not getting the desired output instead I have all the data points.

But the desired output is obtained when the following SAS code is used.

data sample1;
set sample;
if y ne 'as' and y ne 'aq';
run;

Usually the generic understanding is whenever we don't want either 'as' or 'aq', we can use OR operator in IF statement. But in the given code it works well using AND operator. Can anyone give the explanation on how does SAS work when these OR & AND operators are used in the IF statement?

-Suresh.
1 ACCEPTED SOLUTION

Accepted Solutions
darrylovia
Quartz | Level 8
Let's go through the logic of your subsetting if statement.

The first record x=12, y=as
Your logic: if y ne 'as' or y ne 'aq';

y ne 'as', so that is false
y ne 'aq', so that is true

With an OR statement any true resolves to true, hence false or true will resolve to true. With an AND both statements need to be true for it to resolve to true, which is why the AND works and the or does not

BTW: SAS has a function called NOTDIGIT which searches a character string for any character that is not a digit and returns the first position at which that character is found

* For Example;
data sample2;
set sample;
num_check=notdigit(strip(y));
run;

data what_i_want;
set sample2;
if num_check then delete;
run;

View solution in original post

3 REPLIES 3
darrylovia
Quartz | Level 8
Let's go through the logic of your subsetting if statement.

The first record x=12, y=as
Your logic: if y ne 'as' or y ne 'aq';

y ne 'as', so that is false
y ne 'aq', so that is true

With an OR statement any true resolves to true, hence false or true will resolve to true. With an AND both statements need to be true for it to resolve to true, which is why the AND works and the or does not

BTW: SAS has a function called NOTDIGIT which searches a character string for any character that is not a digit and returns the first position at which that character is found

* For Example;
data sample2;
set sample;
num_check=notdigit(strip(y));
run;

data what_i_want;
set sample2;
if num_check then delete;
run;
deleted_user
Not applicable
Hi Darrylovia,

Thanks for your reply and time.
I understood the concept from your nice explanation.

Regards,
Suresh.
deleted_user
Not applicable
It is not a SAS thing, but a Boolean Logic thing.

not (A or B) = not A and not B

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
  • 3 replies
  • 81921 views
  • 0 likes
  • 2 in conversation