As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


PRX

From sasCommunity
Jump to: navigation, search

The PRX functions and call routines are used to invoke the Perl-style regular expressions. These functions and call routines became available in Version 9.

Where Can I Use PRX in SAS?

Examples

Beginner

Matching a Literal

/* Find records where NAME contains an 's' */
data _null_ ;
    set sashelp.class ;
    where prxmatch( '/s/', name ) > 0 ;
    put name= ;
run;

SAS log:

Name=James
Name=Louise
Name=Thomas

Matching at the Beginning of a Field

/* Find records where NAME begins with an upper case vowel */
data _null_ ;
    set sashelp.class ;
    where prxmatch( '/^[AEIOU]/', name ) > 0 ;
    put name= ;
run;

SAS log:

Name=Alfred
Name=Alice

Optional Matching

/* Name contains 'Jane' followed by an optional 't' */
data _null_ ;
    set sashelp.class ;
    where prxmatch( '/Janet?/', name ) > 0 ;
    put name= ;
run;

SAS log:

Name=Jane
Name=Janet

The ? makes matching the subexpression that precedes it optional, which was 't' in this example.

Referencing a Capture Buffer

/* Find any 2 consecutive non-space characters */
data _null_ ;
    set sashelp.class ;
    match_start_pos=prxmatch( '/(\S)\1/', name ) ;
    if match_start_pos>0 then put name=  match_start_pos= ;
run;

SAS log:

Name=Jeffrey match_start_pos=3
Name=William match_start_pos=3

Intermediate

Using a Negative Lookahead Assertion - #1

/* Identify 'mr' not followed by an 's' */
/* Use a negative look-ahead assertion: (?!) */
/* Make the search case-insensitive */
data _null_ ;
    input name $32. ;
 
    match_starting_pos=prxmatch( '/mr(?!s)/io', name ) ;
    put @1 name= @33 match_starting_pos= ;
 
datalines;
Mr. Tiger Woods
Mrs. Robinson
mrs Butterworth
mister ROGERS
Mrs and Mr Hillary Clinton
;
run;

SAS log:

name=Mr. Tiger Woods            match_starting_pos=1
name=Mrs. Robinson              match_starting_pos=0
name=mrs Butterworth            match_starting_pos=0
name=mister ROGERS              match_starting_pos=0
name=Mrs and Mr Hillary Clinton match_starting_pos=9

Advanced

V9.2 Enhancements and Bug Fixes

Related Papers and Resources