Returns the value for a capture buffer
Category: Character String Matching
Restriction: Use with the PRXPARSE function
PRXPOSN ( regular-expression -id , capture-buffer , source )
regular-expression-id
specifies a numeric pattern identifier that is returned by the PRXPARSE function.
capture-buffer
is a numeric value that identifies the capture buffer for which to retrieve a value:
If the value of capture-buffer is zero, PRXPOSN returns the entire match.
If the value of capture-buffer is between 1 and the number of open parentheses in the regular expression, then PRXPOSN returns the value for that capture buffer.
If the value of capture-buffer is greater than the number of open parentheses, then PRXPOSN returns a missing value.
source
specifies the text from which to extract capture buffers.
The PRXPOSN function uses the results of PRXMATCH , PRXSUBSTR, PRXCHANGE, or PRXNEXT to return a capture buffer. A match must be found by one of these functions for PRXPOSN to return meaningful information.
A capture buffer is part of a match, enclosed in parentheses, that is specified in a regular expression. This function simplifies using capture buffers by returning the text for the capture buffer directly, and by not requiring a call to SUBSTR as in the case of CALL PRXPOSN.
For more informaiton about pattern matching, see 'Pattern Matching Using SAS Regular Expressions (RX) and Perl Regular Expressions (PRX)' on page 260.
The PRXPOSN function is similar to the CALL PRXPOSN routine, except that it returns the capture buffer itself rather than the position and length of the capture buffer.
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.
The following example uses PRXPOSN to extract first and last names from a data set.
data ReversedNames; input name & .; datalines; Jones, Fred Kavich, Kate Turley, Ron Dulix, Yolanda ; data FirstLastNames; length first last $ 16; keep first last; retain re; if _N_ = 1 then re = prxparse('/(\w+), (\w+)/'); set ReversedNames; if prxmatch(re, name) then do; last = prxposn(re, 1, name); first = prxposn(re, 2, name); end; run; options pageno=1 nodate ls=80 ps=64; proc print data = FirstLastNames; run;
The SAS System 1 Obs first last 1 Fred Jones 2 Kate Kavich 3 Ron Turley 4 Yolanda Dulix
The following example creates a data set that contains a list of names. Observations that have only a first name or only a last name are invalid. PRXPOSN extracts the valid names from the data set, and writes the names to the data set NEW.
data old; input name .; datalines; Judith S Reaveley Ralph F. Morgan Jess Ennis Carol Echols Kelly Hansen Huff Judith Nick Jones ; data new; length first middle last $ 40; keep first middle last; re = prxparse('/(\S+)\s+([^\s]+\s+)?(\S+)/o'); set old; if prxmatch(re, name) then do; first = prxposn(re, 1, name); middle = prxposn(re, 2, name); last = prxposn(re, 3, name); output; end; run; options pageno=1 nodate ls=80 ps=64; proc print data = new; run;
The SAS System 1 Obs first middle last 1 Judith S Reaveley 2 Ralph F. Morgan 3 Jess Ennis 4 Carol Echols 5 Kelly Hansen Huff
Functions:
'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
'CALL PRXCHANGE Routine' on page 354
'PRXCHANGE Function' on page 739
'PRXMATCH Function' on page 743
'PRXPAREN Function' on page 747
'PRXPARSE Function' on page 748