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.';
spyf2=prxmatch(myregex,spyf1);
put 'Then first digit is at position #' spyf2;
end;
run;
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.';
spyf2=prxchange(myregex,1,spyf1);
put 'Fooled you as ' spyf2;
end;
run;
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.
Links
SAS paper
http://www2.sas.com/proceedings/sugi29/043-29.pdf
SAS documentation - SAS functions and call routines
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002295977.htm
Build and test your regex on the fly (very useful site)
https://regex101.com/
Find and learn everything about regex
http://www.regular-expressions.info/