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;
Using prxchange within a macro
%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 */