As a data analyst, you may often need to work with large amounts of data stored in various directories and subdirectories. In SAS, there are several methods to list all files within a directory including subdirectories.
One method to list all files within a directory including subdirectories in SAS using data access functions is to use SAS data access functions. These functions can be used to obtain a list of the files in a directory under Windows or Unix.
data filenames; length fref $8 fname $200; did=filename(fref, '/home/9tos5as/inputs'); did=dopen(fref); do i=1 to dnum(did); fname=dread(did, i); output; end; did=dclose(did); did=filename(fref); keep fname; run;
This method only lists files and folders in the directory and does not include files in subdirectories.
In this blog post, I am going to introduce you to the list_files macro. This macro helps you to list all the filenames present in a directory, and it can also search for files in its subdirectories. If you need to look for files with a specific extension, don’t worry! This macro can also take care of that.
/* Delete dataset if exists */ proc datasets library=work nolist; delete allfiles; quit; %macro list_files(dir, ext) /parmbuff; %put Start&=dir; %local sep filrf rc did name i fid; %let sep= %sysfunc(ifc(%sysfunc(find(&dir, /)), /, \)); %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; %Put WARNING: not exist &dir.; %return; %end; %do i=1 %to %sysfunc(dnum(&did)); %let name=%qsysfunc(dread(&did, &i)); %let fid = %sysfunc(mopen(&did, &name)); %if &fid ne 0 %then %do; %if (&ext ne %str() and %qupcase(%qscan(&name, -1, .))=%upcase(&ext)) or &ext=%str() %then %do; %put &dir&sep&name; data files; length Directory $200 FileName $200; Directory="&dir&sep"; FileName="&name"; run; proc append base=allfiles data=files; run; %end; %end; %else %do; %list_files(&dir&sep%unquote(&name), &ext) %end; %end; %let rc=%sysfunc(dclose(&did)); %let rc=%sysfunc(filename(filrf)); %mend list_files; %list_files(/home/subhroster20070/sas_inputs); data _null_; dsid=open('allfiles', 'i'); n_obs=attrn(dsid, 'nobs'); n_vars=attrn(dsid, 'nvars'); dslabel=attrc(dsid, 'label'); rc=close(dsid); if n_obs gt 1 then do; call execute('proc print data=allfiles;run;'); end; else do; call execute("data no_data;msg='No Data';run;"); call execute('proc print data=no_data;run;'); end; run;
list_files macro is designed to recursively list all filenames in a specified directory (and its subdirectories) and store them in a dataset. The macro takes two parameters: the directory path (
dir) and an optional file extension (
ext). If the file extension is provided, the macro will only list files with that extension. Once all files are listed, the macro checks if there are any files in the dataset. If there are, it prints the dataset; otherwise, it prints a message indicating there’s no data.
Here is the complete mind map of the code.
I tested the code on Linux, and it works. Let me know in the comments if it works on Windows.