Is there a way to look at all files in directory using SAS code similar to os.listdir() in python or list.files() in R? I know you in SAS EG, you point and clock File-Open Data, but I was just curious if there was some SAS code that could do the same thing.
data _null_;
rc=filename('x','c:\temp\');
did=dopen('x');
do i=1 to dnum(did);
fname=dread(did,i);put fname=;
end;
did=dclose(did);
run;
In SAS there are a number of functions related to manipulating external, i.e. Non-SAS files.
You can search in the documentation for Functions and look at the External Files section. https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lefunctionsref/n01f5qrjoh9h4hn1olbdpb5pr2td.h...
There are examples in the documentation using the functions.
I would strongly recommend creating practice data to work with these functions, as in a directory or two with practice files you don't mind losing as it is relatively easy to delete files or change contents and practice with known and expendable data is a good idea.
data _null_;
rc=filename('x','c:\temp\');
did=dopen('x');
do i=1 to dnum(did);
fname=dread(did,i);put fname=;
end;
did=dclose(did);
run;
In general it is very easy to just read the output of whatever operating system command generates a list of files on your machine.
data files;
infile 'ls -d /mydir/*' pipe truncover;
input filename $200.;
run;
data files;
infile 'dir /b c:\mydir' pipe truncover;
input filename $200.;
run;
If you are stuck running on a SAS session that has disabled your ability to run operating system commands then use the DOPEN() and DREAD() function.
For example as done with this macro:
https://github.com/sasutils/macros/blob/master/dirtree.sas
Of course - here is a macro to give a directory listing (it also works recursively, and does not require XCMD): https://core.sasjs.io/mp__dirlist_8sas.html
This is nice. I will have to try it out. I was hoping for a one line solution similar to the functions that exist in R and Python.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.