BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Is there a way SAS can scan a directory for a filename?

I'm trying to automate a monthly reporting process. One of the files I need to use is only updated quarterly. They are kind enough to put the year and month in the file name (0512) and leave the rest of the file name identical. What I would like to do is scan the directory for the files aa0509bb.dbf aa0512bb.dbf and automatically determine which is the most recent and use that in my proc sql to link to other datasets.

Thanks.
3 REPLIES 3
Vince_SAS
Rhodochrosite | Level 12
Bob-

This forum is intended for the discussion of issues pertaining to SAS Stored Processes:

http://support.sas.com/rnd/itech/doc9/dev_guide/stprocess/index.html

I believe the External File class of the SAS/AF product, or other functions of SAS/AF, provide a fairly straightforward way of getting a list of files in a directory.

In the DATA Step, most people use a pipe+host directory command to get a list of files.

Either way, once you get a list of files, you can put them into a SAS table and sort them, and/or use regular expressions or other character functions to manipulate the file names as you see fit.

I hope that this helps you along the way.

Best regards,

Vince DelGobbo
SAS R&D
deleted_user
Not applicable
Vince@SAS: If you don't give people a forum to discuss the pertinent issue above, you will get people posting off topic.

So put a forum topic in place to capture these demands, and you will get pure topic postings.
peter
SAS Employee
Hi Bob,
There are some useful file functions which you can use within a datastep. The following example reads only XLS files from a certain place. Cheers Peter

DATA save.input_files (KEEP = filename filepath id);
LENGTH filename filepath $256;
rc=FILENAME("readdir","&filepath");
did=DOPEN("readdir");
memcount=DNUM(did);
DO i = 1 TO memcount;
filename = DREAD(did,i);
IF UPCASE(scan(filename,-1,".")) = "XLS" THEN
DO;
filepath = "&filepath\" !! TRIM(LEFT(filename));
id = i;
output;
END;
END;
rc=DCLOSE(did);
RUN;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 9233 views
  • 0 likes
  • 3 in conversation