Hi All,
I've a requirement to read files from a folder every month. So for example if I run the program in June, it should read the file from May. Likewise if I run the program in July, it should read the file from June.
The data file (Excel worksheet) is stored as:
XYZ_050118
XYZ_040118
XYZ_030118
Can anyone suggest how I can read the latest file based on the "timestamp" on the file and choose the latest file every month?
Any help is greatly appreciated. Thank you!
1. Calculate the file stamp of the file you need. This creates a macro variable that you can use in the path of your file name in the import.
%let prev_month = %sysfunc(intnx(month,%sysfunc(date()),-1),mmddyy6.);
%put &prev_month;
2. Use FEXIST() to check if the file exists first.
3. Import file as desired.
proc import out=want datafile="path to file/XYZ_&prev_month..xlsx" dbms=xlsx replace;
run;
@SteelersPitts wrote:
Hi All,
I've a requirement to read files from a folder every month. So for example if I run the program in June, it should read the file from May. Likewise if I run the program in July, it should read the file from June.
The data file (Excel worksheet) is stored as:
XYZ_050118
XYZ_040118
XYZ_030118
Can anyone suggest how I can read the latest file based on the "timestamp" on the file and choose the latest file every month?
Any help is greatly appreciated. Thank you!
Hi All,
I've a requirement to read files from a folder every month. So for example if I run the program in June, it should read the file from May. Likewise if I run the program in July, it should read the file from June.
The data file is stored as:
XYZ_050118
XYZ_040118
XYZ_030118
Can anyone suggest how I can read the latest file based on the "timestamp" on the file and choose the latest file every month?
Any help is greatly appreciated. Thank you!
data _null_;
lastmonth=put(intnx('month',"%sysfunc(today(),DATE9.)"d,-1),MMYYC5.);
mm=scan(lastmonth,1,':');
yy=scan(lastmonth,2,':');
fullname='XYZ_'||strip(mm)||'01'||strip(yy);
call symputx('filestr',fullname);
run;
%put Last months file = &filestr;
Assuming the folder you're referring to is a SAS library, use the libname and &filestr macro var to access last month's file.
Thank you for your prompt response!
If they are SAS datasets stored in a library, You could query dictionary tables aka sashelp.vtable and filter using datecreated variable for the previous month.
The files are excel worksheet. Sorry, I missed that information. I've updated the post.
1. Calculate the file stamp of the file you need. This creates a macro variable that you can use in the path of your file name in the import.
%let prev_month = %sysfunc(intnx(month,%sysfunc(date()),-1),mmddyy6.);
%put &prev_month;
2. Use FEXIST() to check if the file exists first.
3. Import file as desired.
proc import out=want datafile="path to file/XYZ_&prev_month..xlsx" dbms=xlsx replace;
run;
@SteelersPitts wrote:
Hi All,
I've a requirement to read files from a folder every month. So for example if I run the program in June, it should read the file from May. Likewise if I run the program in July, it should read the file from June.
The data file (Excel worksheet) is stored as:
XYZ_050118
XYZ_040118
XYZ_030118
Can anyone suggest how I can read the latest file based on the "timestamp" on the file and choose the latest file every month?
Any help is greatly appreciated. Thank you!
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.
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.