SAS (Base SAS)+
filename mydir "C:\Users\curio\Desktop\Rough";
data file_list;
length full_filename filename folder basename extension $256;
folder = pathname("mydir");
did = dopen("mydir");
data file_list;
length full_filename filename folder basename extension $256;
folder = pathname("mydir");
did = dopen("mydir");
if did > 0 then do;
nfiles = dnum(did);
do i = 1 to nfiles;
filename = dread(did, i);
full_filename = cats(folder, "\", filename);
*Extract extension and basename;
if index(filename, ".") then do;
extension = scan(filename, -1, ".");
basename = substr(filename, 1, length(filename) - length(extension) - 1);
end;
else do;
extension = "";
basename = filename;
end;
output;
end;
rc = dclose(did);
end;
drop did nfiles i rc;
run;
- SAS does not have a single "list files" function — we build it out of filesystem primitives inside a DATA step.
filename mydir "C:\Users\...\Rough";gives the folder a logical name we can reference by string throughout the step.pathname("mydir")returns the resolved folder path (the string we passed to the filename statement), which we store in thefoldercolumn.dopen("mydir")opens the directory and returns a directory ID; a positive value means success.dnum(did)returns the number of entries in the directory; we loop from 1 to that count with aDOloop.dread(did, i)returns the name of the i-th entry;cats(folder, "\", filename)builds the full path.- To split basename and extension, we check for a dot with
index(filename, ".");scan(filename, -1, ".")grabs the last piece (extension), andsubstr()trims it off the rest. dclose(did)closes the directory when the loop finishes;dropremoves the helper variables before writing the final dataset.- We run and check FILE_LIST has one row per file with the four parts correctly separated.