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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Companie...', 1745904819, '18.117.162.216', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Companies-Products/SAS/programming/functions/prxchange/index(97): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78