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
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

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 !

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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