Performs a pattern-matching replacement
Category: Character String Matching
PRXCHANGE ( perl- regular-expression regular-expression-id , times , source )
perl-regular-expression
specifies a Perl regular expression.
regular-expression-id
specifies a numeric pattern identifier that is returned from the PRXPARSE function.
Restriction: If you use this argument, you must also use the PRXPARSE function.
times
is a numeric value that specifies the number of times to search for a match and replace a matching pattern.
Tip: If the value of times is -1, then matching patterns continue to be replaced until the end of source is reached.
source
specifies the character expression that you want to search.
The Basics If you use regular-expression-id , the PRXCHANGE function searches the variable source with the regular-expression-id that is returned by PRXPARSE. It returns the value in source with the changes that were specified by the regular expression. If there is no match, PRXCHANGE returns the unchanged value in source .
If you use perl-regular-expression , PRXMATCH searches the variable source with the perl-regular-expression , and you do not need to call PRXPARSE. You can use PRXMATCH with a perl-regular-expression in a WHERE clause and in PROC SQL.
For more information about pattern matching, see 'Pattern Matching Using SAS Regular Expressions (RX) and Perl Regular Expressions (PRX)' on page 260.
Compiling a Perl Regular Expression If perl-regular-expression is a constant or if it uses the /o option, then the Perl regular expression is compiled once and each use of PRXCHANGE reuses the compiled expression. If perl-regular-expression is not a constant and if it does not use the /o option, then the Perl regular expression is recompiled for each call to PRXCHANGE.
Note: The compile-once behavior occurs when you use PRXCHANGE in a DATA step, in a WHERE clause, or in PROC SQL. For all other uses, the perl-regular-expression is recompiled for each call to PRXCHANGE.
The PRXCHANGE function is similar to the CALL PRXCHANGE routine except that the function returns the value of the pattern-matching replacement as a return argument instead of as one of its parameters.
The Perl regular expression (PRX) functions and CALL routines work together to manipulate strings that match patterns. To see a list and short description of these functions and CALL routines, see the Character String Matching category in 'Functions and CALL Routines by Category' on page 270.
Changing the Order of First and Last Names by Using the DATA Step The following example uses the DATA step to change the order of first and last names.
/* Create a data set that contains a list of names. */ data ReversedNames; input name & .; datalines; Jones, Fred Kavich, Kate Turley, Ron Dulix, Yolanda ; /* Reverse last and first names with a DATA step. */ options pageno=1 nodate ls=80 ps=64; data names; set ReversedNames; name = prxchange('s/(\w+), (\w+)/ /', -1, name); run; proc print data=names; run;
![]() |
The SAS System 1 Obs name 1 Fred Jones 2 Kate Kavich 3 Ron Turley 4 Yolanda Dulix
![]() |
Changing the Order of First and Last Names by Using PROC SQL The following example uses PROC SQL to change the order of first and last names.
data ReversedNames; input name & .; datalines; Jones, Fred Kavich, Kate Turley, Ron Dulix, Yolanda ; proc sql; create table names as select prxchange('s/(\w+), (\w+)/ /', -1, name) as name from ReversedNames; quit; options pageno=1 nodate ls=80 ps=64; proc print data=names; run;
![]() |
The SAS System 1 Obs name 1 Fred Jones 2 Kate Kavich 3 Ron Turley 4 Yolanda Dulix
![]() |
The following example compares the names in two data sets, and writes those names that are common to both data sets.
data names; input name & .; datalines; Ron Turley Judy Donnelly Kate Kavich Tully Sanchez ; data ReversedNames; input name & .; datalines; Jones, Fred Kavich, Kate Turley, Ron Dulix, Yolanda ; options pageno=1 nodate ls=80 ps=64; proc sql; create table NewNames as select a.name from names as a, ReversedNames as b where a.name = prxchange('s/(\w+), (\w+)/ /', -1, b.name); quit; proc print data=NewNames; run;
![]() |
The SAS System 1 Obs name 1 Ron Turley 2 Kate Kavich
![]() |
Functions and CALL routines:
'CALL PRXCHANGE Routine' on page 354
'CALL PRXDEBUG Routine' on page 356
'CALL PRXFREE Routine' on page 358
'CALL PRXNEXT Routine' on page 359
'CALL PRXPOSN Routine' on page 361
'CALL PRXSUBSTR Routine' on page 364
'PRXMATCH Function' on page 743
'PRXPAREN Function' on page 747
'PRXPARSE Function' on page 748
'PRXPOSN Function' on page 750