Monday, April 2, 2018

SAS: List files in directory




I will just leave this here so I can find it later...


* which data files are available?
* macro copied from http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n0js70lrkxo6uvn1fl4a5aafnlgt.htm&docsetVersion=9.4&locale=en;
* with BQ mods to write file list to a sas table ;

%macro list_files(dir,ext);
  %local filrf rc did memcnt name i;
  %let rc=%sysfunc(filename(filrf,&dir));
  %let did=%sysfunc(dopen(&filrf));     

   %if &did eq 0 %then %do;
    %put Directory &dir cannot be open or does not exist;
    %return;
  %end;

   data work.&ext._files;
   attrib folder  length=$128;
   attrib filename length=$64;

   %do i = 1 %to %sysfunc(dnum(&did));  

   %let name=%qsysfunc(dread(&did,&i));

      %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
        %put &dir\&name;

           folder = "&dir";
           filename = "&name";
           output;


      %end;

      %else %if %qscan(&name,2,.) = %then %do;       

        %list_files(&dir\&name,&ext)

      %end;

   %end;

   run;

   %let rc=%sysfunc(dclose(&did));
   %let rc=%sysfunc(filename(filrf));    

%mend list_files;

%list_files(&RAWDATA_FOLDER, xlsx);
%list_files(&RAWDATA_FOLDER, txt);
%list_files(&RAWDATA_FOLDER, dat);


No comments:

Post a Comment