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 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

No comments:

Post a Comment