Search notes:

SAS macros: filesInDirectory

The macro tq84_filesInDirectory(&dir) returns a tq84_array that contains the names of the files and directories found in &dir.
%macro tq84_filesInDirectory(dir);

   %let filrf=tq84_dir;
   %let rc=%sysfunc(filename(filrf, &dir));

   %if &rc ne 0 %then %do;
       %put filesInDirectory: filename statement - rc=&rc;  /* 0 if successful */
       %put %sysfunc(sysmsg());
   %end;

   %local handle;
   %let   handle=%sysfunc(dopen(&filrf));

   %if &handle <= 0 %then %do;
        %put filesInDirectory location=&location, dopen returned &handle;
        %put %sysfunc(sysmsg());
        %abort;
   %end;

   %local count;
   %let count=%sysfunc(dnum(&handle));

   %local ret;
   %let   ret = %tq84_array();

   %do i=1 %to &count;
           %local fname;
           %let fname= %sysfunc(dread(&handle, &i));

           %tq84_arrayPushBack(&ret, &fname)
   %end;


   %let rc = %sysfunc(dclose(&handle));
   %if &rc ne 0 %then %do;
       %put filesInDirectory: dclose rc = &rc;
       %put %sysfunc(sysmsg());
   %end;
  

   %let rc=%sysfunc(filename(filrf)); /* Corresponds to > filename filrf clear < */
   %if &rc ne 0 %then %do;
       %put filesInDirectory, filename filrf clear rc=&rc;
       %put %sysfunc(sysmsg());
   %end;

   &ret

%mend tq84_filesInDirectory;
Github repository about-SAS, path: /macros/filesInDirectory.sas

Examples

Listing the files in a directory:
%let files=%tq84_filesInDirectory(/home/tq84);

/* tq84_filesInDirectory returns an array. So, we can iterate over it: */
%tq84_arrayApply(&files,%nrstr(
  %put file=&this;
))
Github repository about-SAS, path: /macros/tests/filesInDirectory.sas
Moving files from one directory to another:
%tq84_arrayApply(%tq84_filesInDirectory(&fromDir), %nrstr(
  %put file = &this;

  %put %sysfunc(rename(
    &fromDir/&this,
    &toDir/&this,
    file));
))

See also

macros

Index