Previous Page | Next Page

Functions and CALL Routines

PRXCHANGE Function



Performs a pattern-matching replacement.
Category: Character String Matching

Syntax
Arguments
Details
The Basics
Compiling a Perl Regular Expression
Performing a Match
Comparisons
Examples
Example 1: Changing the Order of First and Last Names
Example 2: Matching Rows That Have the Same Name
Example 3: Changing Lowercase Text to Uppercase
Example 4: Changing a Matched Pattern to a Fixed Value
See Also

Syntax

PRXCHANGE(perl-regular-expression | regular-expression-id, times, source)

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

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, PRXCHANGE searches the variable source with the perl-regular-expression, and you do not need to call PRXPARSE. You can use PRXCHANGE with a perl-regular-expression in a WHERE clause and in PROC SQL.

For more information about pattern matching, see Pattern Matching Using Perl Regular Expressions (PRX).


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.  [cautionend]


Performing a Match

Perl regular expressions consist of characters and special characters that are called metacharacters. When performing a match, SAS searches a source string for a substring that matches the Perl regular expression that you specify.

To view a short list of Perl regular expression metacharacters that you can use when you build your code, see the table Tables of Perl Regular Expression (PRX) Metacharacters.


Comparisons

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.


Examples


Example 1: Changing the Order of First and Last Names


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 & $32.;
   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+)/$2 $1/', -1, name);
run;

proc print data=names;
run;    

Output from the DATA Step

                                 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 & $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;

options pageno=1 nodate ls=80 ps=64;
proc print data=names;
run; 

Output from PROC SQL

                                 The SAS System                                1

                              Obs        name

                               1     Fred Jones   
                               2     Kate Kavich  
                               3     Ron Turley   
                               4     Yolanda Dulix

Example 2: Matching Rows That Have the Same Name

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 & $32.;
   datalines;
Ron Turley
Judy Donnelly
Kate Kavich
Tully Sanchez
;

data ReversedNames;
   input name & $32.;
   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+)/$2 $1/', -1, b.name);
quit;

proc print data=NewNames;
run;

Output from Matching Rows That Have the Same Names

                                 The SAS System                                1

                               Obs       name

                                1     Ron Turley 
                                2     Kate Kavich

Example 3: Changing Lowercase Text to Uppercase

The following example uses the \U, \L and \E metacharacters to change the case of a string of text. Case modifications do not nest. In this example, note that "bear" does not convert to uppercase letters because the \E metacharacter ends all case modifications.

data _null_;
   length txt $32;
   txt = prxchange ('s/(big)(black)(bear)/\U$1\L$2\E$3/', 1, 'bigblackbear');
   put txt=;
run;

SAS returns the following output to the log:

txt=BIGblackbear


Example 4: Changing a Matched Pattern to a Fixed Value

This example locates a pattern in a variable and replaces the variable with a predefined value. The example uses a DATA step to find phone numbers and replace them with an informational message.

options nodate nostimer ls=78 ps=60;

   /* 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;

Output from Changing a Matched Pattern to a Fixed Value

                                The SAS System                               1

Obs                                    text

 1  The phone number for Ed is *PHONE NUMBER REMOVED* but not until tonight.  
 2  He can be reached at *PHONE NUMBER REMOVED* tomorrow for testing purposes.


See Also

Functions and CALL routines:

CALL PRXCHANGE Routine

CALL PRXDEBUG Routine

CALL PRXFREE Routine

CALL PRXNEXT Routine

CALL PRXPOSN Routine

CALL PRXSUBSTR Routine

PRXMATCH Function

PRXPAREN Function

PRXPARSE Function

PRXPOSN Function

Previous Page | Next Page | Top of Page