BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SteelersPitts
Obsidian | Level 7

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 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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! 


 

View solution in original post

6 REPLIES 6
SteelersPitts
Obsidian | Level 7

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! 

ChanceTGardener
SAS Employee
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. 

 

 

SteelersPitts
Obsidian | Level 7

Thank you for your prompt response! 

novinosrin
Tourmaline | Level 20

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. 

 

 

SteelersPitts
Obsidian | Level 7

The files are excel worksheet. Sorry, I missed that information. I've updated the post. 

Reeza
Super User

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! 


 

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1573 views
  • 4 likes
  • 4 in conversation