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

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
Fluorite | Level 6
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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 742 views
  • 1 like
  • 2 in conversation