PRXCHANGE Function
Performs a pattern-matching replacement.
| Categories: | Character String Matching |
| CAS | |
| Restrictions: | This function is assigned an I18N Level 0 status, and is designed for SBCS data. Do not use this function to process DBCS or MBCS data. For more information, see Internationalization Compatibility. |
| If you use the regular-expression-id argument, you cannot process DBCS and MBCS data because the argument requires the PRXPARSE function, which is not DBCS compatible. | |
| Note: | SAS has adopted the International Components for Unicode (ICU). ICU enables SAS to apply regular expression matching to Unicode string data. For more information, see Regular Expressions. |
Syntax
Required Arguments
perl-regular-expression
specifies a character constant, variable, or expression with a value that is a Perl regular expression.
regular-expression-id
specifies a numeric variable with a value that is a 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 constant, variable, or expression 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 a character constant, variable, or expression that you want to search.
Details
The Basics
Compiling a Perl Regular Expression
Performing a Match
Comparisons
Examples
Example 1: Changing the Order of First and Last Names By Using the DATA Step in SAS
/* Create a data set that contains a list of names. */
data ReversedNames;
input name & $32.;
datalines;
Jones, Fred
Kavich, Kate
Turley, Ron
Dulix, Yolanda
;
/* Reverse last and first names with a DATA step. */
data names;
set ReversedNames;
name=prxchange('s/(\w+), (\w+)/$2 $1/', -1, name);
run;
proc print data=names;
run; 
Example 2: Changing the Order of First and Last Names By Using the DATA Step in SAS and CAS
data casuser.ReversedNames; input name & $32.; datalines; Jones, Fred Kavich, Kate Turley, Ron Dulix, Yolanda ;
data casuser.names;
set casuser.ReversedNames;
name=prxchange('s/(\w+), (\w+)/$2 $1/', -1, name);
run;
proc print data=casuser.names;
run;
Example 3: Changing the Order of First and Last Names By Using PROC SQL
data ReversedNames;
input name & $32.;
datalines;
Jones, Fred
Kavich, Kate
Turley, Ron
Dulix, Yolanda
;
proc sql;
create table names as
select prxchange('s/(\w+), (\w+)/$2 $1/', -1, name) as name
from ReversedNames;
quit;
proc print data=names;
run;
Example 4: Matching Rows That Have the Same Name
data names;
input name & $32.;
datalines;
Ron Turley
Judy Donnelly
Kate Kavich
Tully Sanchez
;
data ReversedNames;
input name & $32.;
datalines;
Jones, Fred
Kavich, Kate
Turley, Ron
Dulix, Yolanda
;
proc sql;
create table NewNames as
select a.name from names as a, ReversedNames as b
where a.name=prxchange('s/(\w+), (\w+)/$2 $1/', -1, b.name);
quit;
proc print data=NewNames;
run;
Example 5: Changing Lowercase Text to Uppercase
data _null_;
length txt $32;
txt=prxchange ('s/(big)(black)(bear)/\U$1\L$2\E$3/', 1, 'bigblackbear');
put txt=;
run;txt=BIGblackbear
Example 6: Changing a Matched Pattern to a Fixed Value
/* Create data set that contains confidential information. */
data a;
input text $80.;
datalines;
The phone number for Ed is (801)443-9876 but not until tonight.
He can be reached at (910)998-8762 tomorrow for testing purposes.
;
run;
/* Locate confidential phone numbers and replace them with message */
/* indicating that they have been removed. */
data b;
set a;
text=prxchange('s/\([2-9]\d\d\) ?[2-9]\d\d-\d\d\d\d/*PHONE NUMBER
REMOVED*/', -1, text);
put text=;
run;
proc print data=b;
run;