Hello - I am currently using the following code to select specific records from a dataset where field "lab_test_accession_no" contain strings that I am specifying.
-------------
proc sql;
create table check1 as select *
from all.all_lab_lookup
where
upcase(lab_test_accession_no) contains 'ABC123' OR
upcase(lab_test_accession_no) contains 'CNDJD2984' OR
upcase(lab_test_accession_no) contains 'JDFDJF3445';
quit;
----------
My question is: is there a way, besides manually specifying 'ABC123', ''CNDJD2984', etc. within the "Where" section, that I can refer to a table (example of the table below) with a field containing those values? The reason I need an alternative to manual entry is that I am often searching for hundreds of these.
accession_number
ABC123
CNDJD2984
JDFDJF3445
ASDASD2323
ASJHASJKDH32
---
Importantly, I am not looking for only exact matches which I know I can use the "in" operator for, but, want to select the records where the accession_number is contained somewhere within my "lab_test_accession_no" field.
Is there a way? Thank you in advance for any insight.
If you know which values you are interested in best would be to place them in a dataset, especially if there are very many or could be from another set and then do a join.
proc sql;
create table want as
select b.*
from listdata as a, all.all_lab_lookup as b
where upcase (b.lab_test_accesion_no) contains listdata.value;
quit;
Where the dataset listdata would have one variable named value with each record consisting of one value like 'ABC123' or 'CNDJD2984' .
Note that this will generate a note in the log about a query that can't be optimized. This is because this compares every record from each dataset with every record in the other dataset.
Be aware that if your search values are subsets of another value such as DASD23 and ASDASD2323 you will get duplicate values from the all_lab_lookup data because both would match the criteria.
If you know which values you are interested in best would be to place them in a dataset, especially if there are very many or could be from another set and then do a join.
proc sql;
create table want as
select b.*
from listdata as a, all.all_lab_lookup as b
where upcase (b.lab_test_accesion_no) contains listdata.value;
quit;
Where the dataset listdata would have one variable named value with each record consisting of one value like 'ABC123' or 'CNDJD2984' .
Note that this will generate a note in the log about a query that can't be optimized. This is because this compares every record from each dataset with every record in the other dataset.
Be aware that if your search values are subsets of another value such as DASD23 and ASDASD2323 you will get duplicate values from the all_lab_lookup data because both would match the criteria.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.