Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 2. Perl Regular Expressions > Function That Substitutes One String for ...

Function That Substitutes One String for Another

Function:CALL PRXCHANGE
Purpose:To substitute one string for another. One advantage of using PRXCHANGE over TRANWRD is that you can search for strings using wild cards. Note that you need to use the substitution (s) operator in the regular expression to specify the search and replacement expression (see the explanation following the program).
Syntax:CALL PRXCHANGE (pattern-id or regular-expression, times, old-string <, new-string <, result-length <,truncation-value <, number-of-changes>>>>);
 pattern-id is the value returned from the PRXPARSE function.
 regular-expression is a Perl regular expression, placed in quotation marks.
 times is the number of times to search for and replace a string. A value of -1 will replace all matching patterns.
 old-string is the string that you want to replace. If you do not specify new-string, the replacement will take place in old-string.
 new-string, if specified, names the variable to hold the text after replacement. If new-string is not specified, the changes are made to old-string.
 result-length is the name of the variable that, if specified, is assigned a value representing the length of the string after replacement. Note that trailing blanks in old-string are not copied to new-string.
 truncation-value is the name of the variable that, if specified, is assigned a value of 0 or 1. If the resulting string is longer than the length of new-string, the value is 1; otherwise it is a 0. This value is useful to test if your string was truncated because the replacements resulted in a length longer than the original specified length.
 number-of-changes is the name of the variable that, if specified, is assigned a value representing the total number of replacements that were made.


Program 2.11. Demonstrating the CALL PRXCHANGE function

***Primary functions: PRXPARSE, CALL PRXCHANGE;

data cat_and_mouse;
   input Text $char40.;
   length New_text $ 80;

   if _n_ = 1 then Match = prxparse("s/[Cc]at/Mouse/");
   *replace "Cat" or "cat" with Mouse;
   retain Match;

   call prxchange(Match,-1,Text,New_text,R_length,Trune,N_of_changes);
   if Trunc then put "Note: New_text was truncated";
datalines;
The Cat in the hat
There are two cat cats in this line
;
title "Listing of CAT_AND_MOUSE";
proc print data=cat_and_mouse noobs;
run;


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial