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.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 9610 views
  • 0 likes
  • 3 in conversation