proc sql - selecting records with values "like"/"contain" that in another table?

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

proc sql - selecting records with values "like"/"contain" that in another table?

[ Edited ]

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.


Accepted Solutions
Solution
‎03-15-2017 03:51 PM
Super User
Posts: 11,343

Re: proc sql - selecting records with values "like"/"contain" that in another ta

[ Edited ]

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.

View solution in original post


All Replies
Solution
‎03-15-2017 03:51 PM
Super User
Posts: 11,343

Re: proc sql - selecting records with values "like"/"contain" that in another ta

[ Edited ]

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.

Contributor
Posts: 38

Re: proc sql - selecting records with values "like"/"contain" that in another ta

Thank you ballardw! It works great.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 130 views
  • 2 likes
  • 2 in conversation