Functions and CALL Routines |
Syntax of Perl Regular Expressions |
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. Using metacharacters enables SAS to perform special actions. These actions include forcing the match to begin in a particular location, and matching a particular set of characters. Paired forward slashes are the default delimiters. The following two examples show metacharacters and the values they match:
If you use the metacharacter \d, SAS matches a digit between 0-9.
If you use /\dt/, SAS finds the digits in the string "Raleigh, NC 27506".
You use the PRXMATCH function to find the position of a matched value in a source string. PRXMATCH has the following general form:
/search-string/source-string/
The following example uses the PRXMATCH function to find the position of search-string in source-string:
prxmatch('world', 'Hello world!');
The result of PRXMATCH is the value 7, because world occurs in the seventh position of the string Hello world!.
The basic syntax for searching and replacing text has the following form:
s/regular-expression/replacement-string/
The following example uses the PRXCHANGE function to show how substitution is performed:
prxchange('s/world/planet/', 1, 'Hello world!');
where
s |
specifies the metacharacter for substitution. |
world |
specifies the regular expression. |
planet |
specifies the replacement value for world. |
1 |
specifies that the search ends when one match is found. |
Hello world! |
specifies the source string to be searched. |
The result of the substitution is Hello planet.
Another example of using the PRXCHANGE function changes the value Jones, Fred to Fred Jones:
prxchange('s/(\w+), (\w+)/$2 $1',-1, 'Jones, Fred');
In this example, the Perl regular expression is s/(\w+), (\w+)/$2 $1 . The number of times to search for a match is -1. The source string is 'Jones, Fred'. The value -1 specifies that matching patterns continue to be replaced until the end of the source is reached.
The Perl regular expression can be divided into its elements:
s |
specifies a substitution regular expression. |
(\w+) |
matches one or more word characters (alphanumeric and underscore). The parentheses indicate that the value is stored in capture buffer 1. |
,<space> |
matches a comma and a space. |
(\w+) |
matches one or more word characters (alphanumeric and underscore). The parentheses indicate that the value is stored in capture buffer 2. |
/ |
separator between the regular expression and the replacement string. |
$2 |
part of the replacement string that substitutes the value in capture buffer 2, which in this case is the word after the comma, puts the substitution in the results. |
<space> |
puts a space in the result. |
$1 |
puts capture buffer 1 into the result. In this case, it is the word before the comma. |
The following example uses the \u and \L metacharacters to replace the second character in MCLAUREN with a lower case letter:
data _null_; x = 'MCLAUREN'; x = prxchange("s/ (MC) /\u\L$1/i", -1, x); put x=; run;
SAS writes the following output to the log:
x=McLAUREN
Example 1: Validating Data |
You can test for a pattern of characters within a string. For example, you can examine a string to determine whether it contains a correctly formatted telephone number. This type of test is called data validation.
The following example validates a list of phone numbers. To be valid, a phone number must have one of the following forms: (XXX) XXX-XXXX or XXX-XXX-XXXX .
data _null_; 1 if _N_ = 1 then do; paren = "\([2-9]\d\d\) ?[2-9]\d\d-\d\d\d\d"; 2 dash = "[2-9]\d\d-[2-9]\d\d-\d\d\d\d"; 3 expression = "/(" || paren || ")|(" || dash || ")/"; 4 retain re; re = prxparse(expression); 5 if missing(re) then 6 do; putlog "ERROR: Invalid expression " expression; 7 stop; end; end; length first last home business $ 16; input first last home business; if ^prxmatch(re, home) then 8 putlog "NOTE: Invalid home phone number for " first last home; if ^prxmatch(re, business) then 9 putlog "NOTE: Invalid business phone number for " first last business; datalines; Jerome Johnson (919)319-1677 (919)846-2198 Romeo Montague 800-899-2164 360-973-6201 Imani Rashid (508)852-2146 (508)366-9821 Palinor Kent . 919-782-3199 Ruby Archuleta . . Takei Ito 7042982145 . Tom Joad 209/963/2764 2099-66-8474 ; run;
The following items correspond to the lines that are numbered in the DATA step that is shown above.
NOTE: Invalid home phone number for Palinor Kent NOTE: Invalid home phone number for Ruby Archuleta NOTE: Invalid business phone number for Ruby Archuleta NOTE: Invalid home phone number for Takei Ito 7042982145 NOTE: Invalid business phone number for Takei Ito NOTE: Invalid home phone number for Tom Joad 209/963/2764 NOTE: Invalid business phone number for Tom Joad 2099-66-8474
Example 2: Replacing Text |
This example uses a Perl regular expression to find a match and replace the matching characters with other characters. PRXPARSE compiles the regular expression and uses PRXCHANGE to find the match and perform the replacement. The example replaces all occurrences of a less than sign with < , a common substitution when converting text to HTML.
data _null_; 1 input; 2 _infile_ = prxchange('s/</</', -1, _infile_); 3 put _infile_; 4 datalines; 5 x + y < 15 x < 10 < y y < 11 ; run;
The following items correspond to the numbered lines in the DATA step that is shown above.
Create a DATA step. | |
Bring an input data record into the input buffer without creating any SAS variables. | |
Call the PRXCHANGE routine to perform the pattern exchange. The format for the regular expression is s/regular-expression/replacement-text/ . The s before the regular expression signifies that this is a substitution regular expression. The -1 is a special value that is passed to PRXCHANGE and indicates that all possible replacements should be made. | |
Write the current output line to the log by using the _INFILE_ option with the PUT statement. | |
Identify the input file. |
x + y < 15 x < 10 < y y < 11
The ability to pass a regular expression to PRXCHANGE and return a result enables calling PRXCHANGE from a PROC SQL query. The following query produces a column with the same character substitution as in the preceding example. From the input table the query reads text_lines , changes the text for the column line , and places the results in a column named html_line :
proc sql; select prxchange('s/</</', -1, line) as html_line from text_lines; quit;
Example 3: Extracting a Substring from a String |
You can use Perl regular expressions to find and easily extract text from a string. In this example, the DATA step creates a subset of North Carolina business phone numbers. The program extracts the area code and checks it against a list of area codes for North Carolina.
data _null_; 1 if _N_ = 1 then do; paren = "\(([2-9]\d\d)\) ?[2-9]\d\d-\d\d\d\d"; 2 dash = "([2-9]\d\d)-[2-9]\d\d-\d\d\d\d"; 3 regexp = "/(" || paren || ")|(" || dash || ")/"; 4 retain re; re = prxparse(regexp); 5 if missing(re) then 6 do; putlog "ERROR: Invalid regexp " regexp; 7 stop; end; retain areacode_re; areacode_re = prxparse("/828|336|704|910|919|252/"); 8 if missing(areacode_re) then do; putlog "ERROR: Invalid area code regexp"; stop; end; end; length first last home business $ 16; length areacode $ 3; input first last home business; if ^prxmatch(re, home) then putlog "NOTE: Invalid home phone number for " first last home; if prxmatch(re, business) then 9 do; which_format = prxparen(re); 10 call prxposn(re, which_format, pos, len); 11 areacode = substr(business, pos, len); if prxmatch(areacode_re, areacode) then 12 put "In North Carolina: " first last business; end; else putlog "NOTE: Invalid business phone number for " first last business; datalines; Jerome Johnson (919)319-1677 (919)846-2198 Romeo Montague 800-899-2164 360-973-6201 Imani Rashid (508)852-2146 (508)366-9821 Palinor Kent 704-782-4673 704-782-3199 Ruby Archuleta 905-384-2839 905-328-3892 Takei Ito 704-298-2145 704-298-4738 Tom Joad 515-372-4829 515-389-2838 ;
The following items correspond to the numbered lines in the DATA step that is shown above.
Build a Perl regular expression to identify a phone number that matches (XXX)XXX-XXXX, and assign the variable PAREN to hold the result. Use the following syntax elements to build the Perl regular expression:
| |||||||||||||
Build a Perl regular expression to identify a phone number that matches XXX-XXX-XXXX, and assign the variable DASH to hold the result. | |||||||||||||
Build a Perl regular expression that concatenates the regular expressions for (XXX)XXX-XXXX and XXX--XXX--XXXX. The concatenation enables you to search for both phone number formats from one regular expression. The PAREN and DASH regular expressions are placed within parentheses. The bar metacharacter (|) that is located between PAREN and DASH instructs the compiler to match either pattern. The slashes around the entire pattern tell the compiler where the start and end of the regular expression is located. | |||||||||||||
Pass the Perl regular expression to PRXPARSE and compile the expression. PRXPARSE returns a value to the compiled pattern. Using the value with other Perl regular expression functions and CALL routines enables SAS to perform operations with the compiled Perl regular expression. | |||||||||||||
Use the MISSING function to check whether the Perl regular expression compiled without error. | |||||||||||||
Use the PUTLOG statement to write an error message to the SAS log if the regular expression did not compile. | |||||||||||||
Compile a Perl regular expression that searches a string for a valid North Carolina area code. | |||||||||||||
Use the PRXPAREN function to determine which submatch to use. PRXPAREN returns the last submatch that was matched. If an area code matches the form (XXX), PRXPAREN returns the value 2. If an area code matches the form XXX, PRXPAREN returns the value 4. | |||||||||||||
Call the PRXPOSN routine to retrieve the position and length of the submatch. | |||||||||||||
Use the PRXMATCH function to determine whether the area code is a valid North Carolina area code, and write the observation to the log. |
Output from Extracting a Substring from a String
In North Carolina: Jerome Johnson (919)846-2198 In North Carolina: Palinor Kent 704-782-3199 In North Carolina: Takei Ito 704-298-4738
Example 4: Another Example of Extracting a Substring from a String |
In this example, the PRXPOSN function is passed to the original search text instead of to the position and length variables. PRXPOSN returns the text that is matched.
data _null_; 1 length first last phone $ 16; retain re; if _N_ = 1 then do; 2 re=prxparse("/\(([2-9]\d\d)\) ?[2-9]\d\d-\d\d\d\d/"); 3 end; input first last phone & 16.; if prxmatch(re, phone) then do; 4 area_code = prxposn(re, 1, phone); 5 if area_code ^in ("828" "336" "704" "910" "919" "252") then putlog "NOTE: Not in North Carolina: " first last phone; 6 end; datalines; 7 Thomas Archer (919)319-1677 Lucy Mallory (800)899-2164 Tom Joad (508)852-2146 Laurie Jorgensen (252)352-7583 ; run;
The following items correspond to the numbered lines in the DATA step that is shown above.
Create a DATA step. | |||||||||||||||||||||||||||||||||||||
If this is the first record, find the value of re. | |||||||||||||||||||||||||||||||||||||
Build a Perl regular expression for pattern matching. Use the following syntax elements to build the Perl regular expression:
| |||||||||||||||||||||||||||||||||||||
Return the position at which the string begins. | |||||||||||||||||||||||||||||||||||||
Identify the position at which the area code begins. | |||||||||||||||||||||||||||||||||||||
Search for an area code from the list. If the area code is not valid for North Carolina, use the PUTLOG statement to write a note to the SAS log. | |||||||||||||||||||||||||||||||||||||
Identify the input file. |
Output from Extracting a Substring from a String
NOTE: Not in North Carolina: Lucy Mallory (800)899-2164 NOTE: Not in North Carolina: Tom Joad (508)852-2146
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.