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

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 

       

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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.

SteelersPitts
Obsidian | Level 7

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

PaigeMiller
Diamond | Level 26

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
SteelersPitts
Obsidian | Level 7

Thank you. Appreciate your feedback. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 8808 views
  • 3 likes
  • 3 in conversation