Returns the position and length of a substring that matches a pattern.

Category: Character String Matching
Restriction: Use with the PRXPARSE function.
Interaction: When invoked by the %SYSCALL macro statement, CALL PRXSUBSTR removes the quotation marks from its arguments. For more information, see Using CALL Routines and the %SYSCALL Macro Statement.


CALL PRXSUBSTR (regular-expression-id, source, position <, length> );

Required Arguments


specifies a numeric variable with a value that is an identification number that is returned by the PRXPARSE function.


specifies a character constant, variable, or expression that you want to search.


is a numeric variable with a returned value that is the position in source where the pattern begins. If no match is found, CALL PRXSUBSTR returns zero.

Optional Argument


is a numeric variable with a returned value that is the length of the substring that is matched by the pattern. If no match is found, CALL PRXSUBSTR returns zero.


The CALL PRXSUBSTR routine searches the variable source with the pattern from PRXPARSE, returns the position of the start of the string, and if specified, returns the length of the string that is matched. By default, when a pattern matches more than one character that begins at a specific position, CALL PRXSUBSTR selects the longest match.
For more information about pattern matching, see Pattern Matching Using Perl Regular Expressions (PRX).


CALL PRXSUBSTR performs the same matching as PRXMATCH, but CALL PRXSUBSTR additionally enables you to use the length argument to receive more information about the match.
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 SAS Functions and CALL Routines by Category.


Example 1: Finding the Position and Length of a Substring

The following example searches a string for a substring, and returns its position and length in the string.
data _null_;
      /* Use PRXPARSE to compile the Perl regular expression. */
   patternID = prxparse('/world/');
      /* Use PRXSUBSTR to find the position and length of the string. */
   call prxsubstr(patternID, 'Hello world!', position, length);
   put position= length=;
The following line is written to the SAS log:
   position=7 length=5

Example 2: Finding a Match in a Substring

The following example searches for addresses that contain avenue, drive, or road, and extracts the text that was found.
data _null_;
   if _N_ = 1 then 
      retain ExpressionID;
         /* The i option specifies a case insensitive search. */
      pattern = "/ave|avenue|dr|drive|rd|road/i";
      ExpressionID = prxparse(pattern);
   input street $80.;
   call prxsubstr(ExpressionID, street, position, length);
   if position ^= 0 then
      match = substr(street, position, length);
      put match:$QUOTE. "found in " street:$QUOTE.;
153 First Street
6789 64th Ave
4 Moritz Road
7493 Wilkes Place
The following lines are written to the SAS log:
   "Ave" found in "6789 64th Ave"
   "Road" found in "4 Moritz Road"