I have a macro that does exactly what you need. Just pass the folder name ending with '/' and a data set name and the entire contents of the folder will be outputted to your data set
Darryl
/*******************************************************************************************************************************************************/
/* Program Name: get_folder_info (macro of the same name) */
/* */
/* Description: SAS Utility: */
/* Dump the contents of folder into a SAS data set (user defined) */
/* Output data set contains the input folder name, all the files inside the folder, and */
/* whether those files are a directory or file. */
/* */
/* If a file is inputted the program stops with a message in the log stating that the input */
/* directory is not a directory. */
/* */
/* */
/* Macro Input Parameters: dir = Unquoted Full Directory Path ending in \. */
/* foldercontents_dataset = SAS data set containing the directory information. */
/* */
/*******************************************************************************************************************************************************/
%macro get_folder_info(dir=,output_ds=);
%put &dir;
/* Check for Existence of directory path */
%if %sysfunc(fileexist(&dir)) %then %do;
/* Assigns the fileref of mydir to the directory and opens the directory */
/* Assigns the fileref of myfile to the directory and opens the file */
/* Assign using file i/o functions and directory i/o functions, will then compare the output of the functions */
%let dirrf=md;
%let filrf=mf;
%let dc=%sysfunc(filename(dirrf,&dir));
%let fc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&dirrf)); /* opens as a directory, 0 means could not be opened
note: unable to open a file with dopen */
%let fid=%sysfunc(fopen(&filrf)); /* opens as a file, 0 means could not be opened
note: unable to open a directory with fopen */
/* Returns the number of members in the directory, */
%if &did > 0 %then %do;
%let memcnt=%sysfunc(dnum(&did));
%put memcnt=&memcnt;
%end;
%* QA: put macro variable values into log;
%put QA: dc=&dc fc=&fc did=&did fid=&fid;
%if &fid>0 and &did=0 %then %put &dir is file not a directory;
%if &did > 0 %then %do;
%do i=1 %to &memcnt;
%let dir&i=&dir.%qsysfunc(dread(&did,&i)); %* create full path of directory/file;
%let mem&i=%qsysfunc(dread(&did,&i));
%* Assign filerefs;
%let dirrf&i=md&i;
%let filrf&i=mf&i;
%let dc&i=%sysfunc(filename(dirrf&i,&&dir&i));
%let fc&i=%sysfunc(filename(filrf&i,&&dir&i));
%* open files;
%let did&i=%sysfunc(dopen(&&dirrf&i));
%let fid&i=%sysfunc(fopen(&&filrf&i));
%* QA: put macro variable values into log;
%put QA: dirrf=&&dirrf&i filrf=&&filrf&i did=&&did&i fid=&&fid&i name=&&dir&i;
%* close files;
%let dc&i=%sysfunc(dclose(&&did&i));
%let fc&i=%sysfunc(fclose(&&fid&i));
%* clear filerefs;
filename md&i clear;
filename mf&i clear;
%end;
%* create output table;
data &output_ds;
attrib DirName length=$512 label='Name of Input Directory'
FileName length=$1024 label='Full File Name with Path of Directory Member'
MemberName length=$512 label='File Name of Directory Member'
FileType length=$9 label='Directory Member: File or Directory';
%do i=1 %to &memcnt;
DirName="&dir";
FileName="&&dir&i";
MemberName="&&mem&i";
if (&&did&i=0 and &&fid&i>0) then FileType='File';
else FileType='Directory';
output;
%end;
run;
title "Contents of &dir";
proc print data=FolderContents;
run;
title;
%end;
/* Closes the input directory/file. All opened files must be closed. */
%let dc=%sysfunc(dclose(&did));
%let fc=%sysfunc(fclose(&fid));
%* clear filerefs;
filename md clear;
filename mf clear;
%end;
%else %do;
%put ERROR: Folder &dir Not Found;
%end;
%mend get_folder_info;
* END OF SAS PGM;
... View more