BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
elained
Calcite | Level 5

I have 2 tables that I need to join.

1.  Patients   

2.  LabResults

 

I want everyone in the Patients table.

I only want LabResults that match records in the Patients table.

 

The identifier that both tables contain is PatientID

How do I match all records in the Patients table to those that have the same PatientID in the LabResults table?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

This is called a MERGE.

data want;
  merge patients lab_results;
  by patientid;
run;

If you want to exclude lab results that are not actually from patients (how did they get into your database then?) you can use the IN= dataset option to create temporary indicator variables to indicate if the the given dataset is contributing to the current observation.

data want;
  merge patients(in=in1) lab_results;
  by patientid;
  if not in1 then do;
    if first.patientid then put 'ERROR: ' patientid= 'found in LABRESULTS but not in PATIENTS.';
    delete;
  end;
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

This is called a MERGE.

data want;
  merge patients lab_results;
  by patientid;
run;

If you want to exclude lab results that are not actually from patients (how did they get into your database then?) you can use the IN= dataset option to create temporary indicator variables to indicate if the the given dataset is contributing to the current observation.

data want;
  merge patients(in=in1) lab_results;
  by patientid;
  if not in1 then do;
    if first.patientid then put 'ERROR: ' patientid= 'found in LABRESULTS but not in PATIENTS.';
    delete;
  end;
run;
elained
Calcite | Level 5
Thank you so much! My SAS programming skills are rusty from years of not using SAS. I have to re-learn SAS in a hurry! Really appreciate this.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 648 views
  • 1 like
  • 2 in conversation