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

Hi

I have a small problem, I have a large data set and I want to take these values that have certain characters (in my case - prefixes). There are dozens of these prefixes so doing LIKE/CONTAINS each time does not only take space but is inefficient.. Prefixes usually look like that - NLLL - so 1 number and 3 letters. Is there a way to check for that pattern in like/contains coditions or just a way to have these conditions contain multiple values i.e. IN option with prefixes or a list of prefixes that could be checked instead of only one?

Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, have a look at perl regular expressions - prxparse/prxmatch.  You could also break it out a bit:

data want;

     set have;

     where     (substr(PREFIX,1,1) in ('1','2','3'))

          and     (substr(PREFIX(2,3) in ('ABC','CBD'));

run;

If you have lots, then make a dataset of the combinations and use that to generate your code from:

data _null_;

     set list_of_combinations end=last;

     if _n_=1 then call execute('data want; set have; where prefix in ("'||strip(item)||'");

     else call execute(',"'||strip(item)||'"');

     if last then call execute('); run;');

run;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, have a look at perl regular expressions - prxparse/prxmatch.  You could also break it out a bit:

data want;

     set have;

     where     (substr(PREFIX,1,1) in ('1','2','3'))

          and     (substr(PREFIX(2,3) in ('ABC','CBD'));

run;

If you have lots, then make a dataset of the combinations and use that to generate your code from:

data _null_;

     set list_of_combinations end=last;

     if _n_=1 then call execute('data want; set have; where prefix in ("'||strip(item)||'");

     else call execute(',"'||strip(item)||'"');

     if last then call execute('); run;');

run;

Patrick
Opal | Level 21

If you don't have too many different patterns then a Regular Expression could be quite efficient for coding. Eg. for your pattern - NLLL - it could be something like:

where prxmatch('/^\d[[:alpha:]]{3}/o',<your variable>)

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1173 views
  • 3 likes
  • 3 in conversation