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?
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;
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.