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
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.
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.
Thank you for your valuable feedback. I appreciate it. I will code and let you know how it went.
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.
Thank you. Appreciate your feedback.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.