BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ucdcrush
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

ucdcrush
Obsidian | Level 7
Thank you ballardw! It works great.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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