Featured Post

Reference Books and material for Analytics

Website for practising R on Statistical conceptual Learning: https://statlearning.com  Reference Books & Materials: 1) Statis...

Sunday, April 22, 2018

SAS Regular Expression Example



Below is the example of  SAS Regular Expression function to make you understand this.

Two Perl Regular Expression(PRX) Functions
1.       PRXPARSE
Description - It define a Perl regular expression which is further used by other Perl Regular Expression function like PRXMATCH.

SyntaxPRXPARSE(“/Perl Regular Expression/i”)
                  “ ” à Part of SAS syntax
                  / à Default Perl delimiter
                I à Ignore case sensitive

Example à PRXPARSE(“/sas/i”)

2.       PRXMATCH
Description – To locate the position in a string, where a regular expression is matched. This function always returns the first position in a string expression of the pattern described by the regular expression.  If pattern is not found, then returns a zero.

SyntaxPRXPARSE(“/Perl Regular Expression/i” or Pattern_id, String)
                  “ ” à Part of SAS syntax
                  / à Default Perl delimiter
                I à Ignore case sensitive
                Pattern_id à is the value returned from the  PRXPARSE function

Example à PRXMATCH(“/sas/i”, String)
or
                       If _N_ = 1 then Pattern = PRXPARSE(“/sas/i”);
                       Retain Pattern;
                      PRXMATCH(Pattern, String)

Code
To find the word “SAS” anywhere in the string.

DATA Test;   
IF _N_ = 1 THEN PATTERN_NUM = PRXPARSE("/sas/i");   
* match for the word 'SAS' anywhere in the string;   
RETAIN PATTERN_NUM;
INPUT STRING $30.;   
POSITION = PRXMATCH(PATTERN_NUM,STRING);   
FILE PRINT;   
PUT PATTERN_NUM= STRING= POSITION=;
DATALINES;
Welcome to SAS india
SAS with Perl regular expression
Enjoy SAS with PRX
Perl Regular expression
;
run;

Output-
                              


No comments:

Post a Comment