Is there a way to read file with date stamp for previous day if today's file is missing/not available? I do read file with date with date stamp with macro code as below for current day (or day's file). It does work whether to read to day's file or any previous day (with subtraction in date). However, my question is: how to read yesterday's file IF today's file is missing/not exist.
This is a Unix environment.
%let Date = %sysfunc(date(),Date9.);
%put &Date;
libname dat '/.........../folder_name';
data new;
set dat._&Date._merged_line_list;
run;
Basic steps: Determine if the file exists. The SAS function FEXIST does this if used correctly for EXTERNAL files or EXIST to check on the existence of a SAS data set or view.
If that does not exist then generate the name for the "previous day" file. Check if it exists.
<Repeat as needed>
When you have verified the file exist then process. If none of the desired files exist then write some output that says so and terminate that portion of your program.
@Kurt_Bremser good question, yes it is possible that someday a file may not be available.
@ballardw Thank you for offering this logics. I will try to see if I can figure out.
So could we rephrase your question as: read the latest dataset available, going back from a certain date (today)?
Or should we, if neither today's and yesterday's dataset is available, issue an error message and terminate?
Exactly that's what I wanted. Read the latest file from a folder. File name structure is e.g. 19JAN2022_report
A name that starts with a digit is not valid for SAS datasets; are we talking datasets or files?
I was talking about datasets (also excel files). With macro date $Date function datasets are created or read. However I am re-thinking as it is not straightforward because I realize that some days files are missing or date gap, so I can leave as is for now.
Thank you all for offering support
Excel files are NOT SAS datasets.
By defining a LIBNAME for an Excel file, it is possible to use spreadsheets in that file like datasets.
So please describe in detail where you need to apply that date.
@bijayadhikar wrote:
...
Exactly that's what I wanted. Read the latest file from a folder. File name structure is e.g. 19JAN2022_report
Word of advice, do not use DATE (or MDY or DMY order ) style dates in filenames. They will NOT sort properly.
Use YMD order date strings in names. Then the filenames will sort in chronological order.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.