DATA Step, Macro, Functions and more

Read Latest File From A Folder

Accepted Solution Solved
Reply
Highlighted
Contributor
Posts: 24
Accepted Solution

Read Latest File From A Folder

Hi All,

 

First off, I'm a Tableau professional finding his foot in SAS world. So if my questions are naive, I apologize in advance to the group.

 

I've a requirement where I have to read latest file from last from a folder. So the requirements is something like this:

 

Requirements:

1. Folder A stores files for sales as:

           Sales_062018_06102018.txt

           Sales_062018_06132018.txt

           Sales_062018_06152018.txt

           Sales_062018_06182018.txt

 

2. I've to go to the folder and pick the latest file (In this case Sales_062018_06182018.txt)

 

3. Any suggestions on how I should go and read the files and get the latest file? The number of files in the folder can be more than four. 

 

Any pseudo code or code structure would be very helpful. Thank you 

       

 


Accepted Solutions
Solution
Wednesday
Super User
Posts: 10,280

Re: Read Latest File From A Folder

[ Edited ]
Posted in reply to SteelersPitts

You can make your life easier by orders of magnitude by using an intelligent format for the dates. MMDDYYYY is dumb, YYYYMMDD (as used in the ISO 8601 standard) is intelligent.

I'd then get the last file by doing that (UNIX):

filename oscmd pipe "ls &path./Sales_&month.&year._*.txt|tail -1";

data _null_;
infile oscmd truncover;
input filename $100.;
call symput('infile',filename);
run;

data want;
infile "&infile" .........

since UNIX ls orders automatically.

With your filenames, you need to work harder:

filename oscmd pipe "ls &path./Sales_&month.&year._*.txt";

data files;
infile oscmd truncover;
input filename $100.;
date = input(scan(scan(scan(filename,-1,'/'),1,'.'),3,'_'),mmddyy8.);
run;

proc sort data=files;
by descending date;
run;

data _null_;
set files;
call symput('infile',filename);
stop; *get only first obs;
run;

 

Edit: added the pipe engine in the filename statements.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
Wednesday
Super User
Posts: 10,280

Re: Read Latest File From A Folder

[ Edited ]
Posted in reply to SteelersPitts

You can make your life easier by orders of magnitude by using an intelligent format for the dates. MMDDYYYY is dumb, YYYYMMDD (as used in the ISO 8601 standard) is intelligent.

I'd then get the last file by doing that (UNIX):

filename oscmd pipe "ls &path./Sales_&month.&year._*.txt|tail -1";

data _null_;
infile oscmd truncover;
input filename $100.;
call symput('infile',filename);
run;

data want;
infile "&infile" .........

since UNIX ls orders automatically.

With your filenames, you need to work harder:

filename oscmd pipe "ls &path./Sales_&month.&year._*.txt";

data files;
infile oscmd truncover;
input filename $100.;
date = input(scan(scan(scan(filename,-1,'/'),1,'.'),3,'_'),mmddyy8.);
run;

proc sort data=files;
by descending date;
run;

data _null_;
set files;
call symput('infile',filename);
stop; *get only first obs;
run;

 

Edit: added the pipe engine in the filename statements.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 24

Re: Read Latest File From A Folder

Posted in reply to KurtBremser

Thank you for your valuable feedback. I appreciate it. I will code and let you know how it went.

Respected Advisor
Posts: 3,061

Re: Read Latest File From A Folder

Posted in reply to SteelersPitts

There are a lot of examples here in the SAS Communities of code to read the names of files in a specific folder. This is one such example: https://communities.sas.com/t5/Base-SAS-Programming/how-to-read-filenames-of-a-directory-into-a-file.... Once you have read the file names into a data set, then finding the one with the biggest date and time in the file name is simple.

 

Searching via your favorite search engine will find many more examples.

--
Paige Miller
Contributor
Posts: 24

Re: Read Latest File From A Folder

Posted in reply to PaigeMiller

Thank you. Appreciate your feedback. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 63 views
  • 3 likes
  • 3 in conversation