Interact, learn and grow

Join Now
This is an open group. Sign in and click the "Join Group" button to become a group member and start posting.
SAS Employee
Posts: 2
Juletip #12 - SAS Regular Expressions

Good morning everyone


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.


data thomas1;

   retain myREGEX;

   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.


data thomas2;

   retain myREGEX;

   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.



SAS paper


SAS documentation - SAS functions and call routines


Build and test your regex on the fly (very useful site)


Find and learn everything about regex

SAS Employee
Posts: 19
Re: Juletip #12 - SAS Regular Expressions
[ Edited ]
Posted in reply to ThomasFrederiksen

Thank you for a great "Juletip" Thomas.

Amazing what can be done with just one function!


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)


data ValidCpr;
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