In a prvious post, “Filename: Create SAS dataset of folder hierarchical structure and file list“, I have described a way to extract filenames in a folder or subfolder to a file. Here is a different way of doing it.
Reference: Reading and Processing the Contents of a Directory by Ben Cochran, The Bedford Group, Raleigh, NC. https://www.mwsug.org/proceedings/2012/S1/MWSUG-2012-S128.pdf
The following SAS codes get the filenames from L:\DaraFiles directory and pass them to a macro variable called varlist.
%let varlist =; data null; rc =filename("mydir", "L:\DataFiles"); did = dopen("mydir"); if did > 0 then do; num =dnum(did); do i = 1 to num; fname =dread (did, i ); put fname = ; call symput('varlist', trim(resolve('&varlist'))||' '||trim(fname)); end; end; run; %put &varlist.;
Key Functions:
- filename: assign the directory to a alias.
- dopen: open the directory and get the directory identifier;
- dnum: get the number of member in the directory;
- dread: get the filenname of the directory member;