Do you know that you actually can use regular expressions in SAS code?
Well you can, and some of us do. It’s handy when you need to do complex string operations.
There is a number of SAS functions and call routines you can use from plain SAS code (view the links section).
Here is an example where we want to find the position of the first digit. Use PRXPARSE to create and compile the regex. Use PRXMATCH to use the regex to find the first position.
if _N_=1 then do;
myREGEX = prxparse("/\d/");
spyf1='You have won 100 kr.';
put 'Then first digit is at position #' spyf2;
Here is an example where we want to replace parts of the string if we find what we are looking for. We are looking for any digit and the rest of the string. Use PRXPARSE to create and compile the regex. Use PRXCHANGE to use the regex to make the replacement.
if _N_=1 then do;
myREGEX = prxparse("s/\d.*/zero,zip,nothing at all/");
spyf1='you have won 100 kr.';
put 'Fooled you as ' spyf2;
In Data Management Studio it’s almost impossible to avoid using regular expressions. E.g. we use it when we work with the QKB, and we do that a lot in DM Studio.
Don’t be afraid to start using regular expressions. Most of challenges you meet and the syntax you need can be googled. It’s used massively throughout the world, and you will find many replies on the internet.
I just recently found this example, which might be usefull for someone else 🙂
It can be used for validating if 10 digits is a valid Danish social security id (CPRNR)
input cpr $10.;
/* ^ = beginning of text
Max 31 for month 1,3,5,7,8,10,12
29 in february, but only if leap year
29 in february, but only if digt-7 is between 4 and 9
. . . .
$ = end of text
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.