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!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.