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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.