BookmarkSubscribeRSS Feed
bijayadhikar
Quartz | Level 8

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;

9 REPLIES 9
ballardw
Super User

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.

 

 

bijayadhikar
Quartz | Level 8

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

 

Kurt_Bremser
Super User

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?

bijayadhikar
Quartz | Level 8

@Kurt_Bremser 

Exactly that's what I wanted. Read the latest file from a folder. File name structure is e.g. 19JAN2022_report

bijayadhikar
Quartz | Level 8

@Kurt_Bremser 

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

Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 683 views
  • 0 likes
  • 4 in conversation