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-
                              


Sunday, April 8, 2018

SAS Functions for File Operation : Basic Level


The Below code to understand SAS Functions related to Directory. You might not be using them in case you are using SAS metadata based tools but it is always advantageous to understand them .

This Code will provide the list of files and folders available within specific directory(List of Members within Directory).

Note: File could be with any extension(.sql, .sas, .txt, .xls & etc.)


Data Work.Test / view=work.Test;
/*Data _Null_;*/
Drop RC DID i;
RC = filename("Mydir", "G:\Test");
put RC;
did = dopen("Mydir");   /* Dopen  - open the directory and returns with directory identifier */
Put did;
if did > 0 then
      do i=0 to dnum(did); /* DNum  - returns number of members in a directory */
      dset = dread(did, i);   /* Dread  - returns the name of directory Member.  Dset will hold file name with extension and also folder name(if available) */

      dset1 = scan(dset,1,'.'); /* Dset1 will hold only file name also folder name(if available) */

      Ext = scan(dset,-1,'.'); /* Ext will hold only file extension also folder name(if available) */

      output;
end;
RC = dclose(did); /* DClose  - Close the Directory Opened by DOpen Function */

run;


Contributed by Shoaib Ansari