BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
greenie
Obsidian | Level 7
libname lib "N:\Research-Studies";

data data_mar22;
set lib.data_mar22;
run;

data data_subset;
set lib.data_mar22;
if menstat_type ne 1 or 2 or 3 then delete;
run;

Hi I want to create a subset of my dataset, but after running, it shows:

 

NOTE: There were 13698 observations read from the data set LIB.DATA_MAR22.

NOTE: The data set WORK.DATA_SUBSET has 0 observations and 278 variables.

 

Anyone knows why?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data data_subset;
set lib.data_mar22;
if menstat_type ne 1 or menstat_type ne 2 or menstat_type ne 3 then delete;
run;

 

This is the proper syntax. However, even this will likely result in the wrong result, it will probably not do any subsetting and then you will get all observations ... because this sequence of OR probably will select everything.

 

This should work:

 

data data_subset;
set lib.data_mar22;
if menstat_type not in (1 2 3) then delete;
run;

 

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data data_subset;
set lib.data_mar22;
if menstat_type ne 1 or menstat_type ne 2 or menstat_type ne 3 then delete;
run;

 

This is the proper syntax. However, even this will likely result in the wrong result, it will probably not do any subsetting and then you will get all observations ... because this sequence of OR probably will select everything.

 

This should work:

 

data data_subset;
set lib.data_mar22;
if menstat_type not in (1 2 3) then delete;
run;

 

 

--
Paige Miller
greenie
Obsidian | Level 7

Thank you! The second one works!

ballardw
Super User

SAS treats simple numeric values as logical when you use logical operators like OR , AND and such between them. The "rule" if is the number is not 0 (zero) or missing then it is "true".

 

So your statement

if menstat_type ne 1 or 2 or 3 then delete;

Was treated as (replacing the numbers with the SAS logical treatment)

If menstate_type ne 1 or (true) or (true) then delete.

So if the menstate_type was not 1 then SAS thinks you want to delete the record. Either use the IN operator to make sure the variable is compared to the list or you have to explicitly do each comparison. However "not equal" to multiple values become AND instead of OR. Look up De Morgan's Law for logic.

if menstat_type ne 1 AND menstat_type ne 2 AND menstat_type ne 3 then delete;

The logic involved is routine, tedious and an obnoxious exercise until you do a lot of these. (Or take a course about Logic).

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 548 views
  • 0 likes
  • 3 in conversation