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

Hello,

 

I have two data sets with patients, which all have an id. The patients_all dataset contains all patients, the patients_exclude dataset contains the patients I want to exclude.

 

Now I want to create a dataset patients_all_clear which contains all patients except those from the dataset patients_exclude. The patients are identified by their id.

 

In short: various id from the dataset patients_exclude should no longer appear in the new dataset patients_all_clear.

 

What is the most clever way to do this?

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Most modern and most efficient method:

data patients_all_clear;
set patients_all;
if _n_ = 1
then do;
  declare hash ex (dataset:"patients_exclude");
  ex.definekey("id");
  ex.definedata("id"); /* not necessary, but reduces memory footprint */
  ex.definedone();
end;
if ex.check() = 0 /* id is found */ then delete;
run;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Try something like this.

 

proc sql;
    create table patients_all_clear as select * from patients_all
        where id not in (select distinct id from patients_exclude);
quit;
--
Paige Miller
Kurt_Bremser
Super User

Most modern and most efficient method:

data patients_all_clear;
set patients_all;
if _n_ = 1
then do;
  declare hash ex (dataset:"patients_exclude");
  ex.definekey("id");
  ex.definedata("id"); /* not necessary, but reduces memory footprint */
  ex.definedone();
end;
if ex.check() = 0 /* id is found */ then delete;
run;
Tamino
Obsidian | Level 7

I tried:

data patients_all_clear;
     merge	patients_all	(in = in1)
         	patients_exclude    (in = in2);
     by id;
     if in1 and not in2;
run;

Could there be a reason not to do it like this, or is it possible like this?

Astounding
PROC Star

This is probably the most common approach, well understand by most SAS programmers.

 

On the downside:  it requires sorted data sets.  (The other suggestions work on unsorted data.)  It might take slightly longer to run, but that is unlikely to be an issue.

 

Also note that the final statement could be simplified:

 

if not in2;

 

Any patient not found in IN2 must have come from IN1.

Kurt_Bremser
Super User

If the datasets are already sorted by id, you can do this, and it will perform fine. Be careful which variables are contained in the datasets, because you might overwrite values you want to keep.

The hash method allows to use a lookup table without having to sort any of the datasets, as the sort is performed implicitly in memory when the key table is built.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 1101 views
  • 4 likes
  • 4 in conversation