Search notes:

SAS programming, function prxchange

prxchange: change text with regular expressions.

Replace numbers

data _null_;

   source = 'foo 42 bar 178 baz';
   regexp = 's/\d+/NUM/';
   times  = -1;  /* -1: replace all occurences */

   result = prxchange(regexp, times, source);

   put result=;
   /*  result=foo NUM bar NUM baz */

run;
Github repository about-SAS, path: /programming/functions/prxchange/replace-numbers.sas

Switch words

(...) captures text and $n inserts it again.
data _null_;

   source = 'world, Hello!';
   regexp = 's/(\w+), *(\w+)/$2, $1/';
   times  = 1;  /* 1: One replacement suffices */

   result = prxchange(regexp, times, source);

   put result=;
   /*  result=Hello, world! */

run;
Github repository about-SAS, path: /programming/functions/prxchange/switch-words.sas

Using prxchange within a macro

Within a macro, prxchange can be used together with %sysfunc:
%macro removeNumAtEnd(text);

  %local result;
  %let   result = %sysfunc(prxchange(s/\d+$//,  -1, &text));
  &result

%mend  removeNumAtEnd;

%put %removeNumAtEnd(Say 42 times hello); /* Say 42 times hello */
%put %removeNumAtEnd(Say hello times 42); /* Say hello times    */
Github repository about-SAS, path: /programming/functions/prxchange/sysfunc.sas

See also

SAS macro reReplace
prxmatch
functions

Index