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

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


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





   %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);