BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
whs278
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

6 REPLIES 6
ballardw
Super User

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.

Ksharp
Super User
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;
Tom
Super User Tom
Super User

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

 

AllanBowe
Barite | Level 11

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

/Allan
MacroCore library for app developers
Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
whs278
Quartz | Level 8

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. 

Ksharp
Super User
If you could use OS command, that is the best choice !

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1891 views
  • 6 likes
  • 5 in conversation