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
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;
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;
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>)
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.