DATA Step, Macro, Functions and more

Select latest dataset file every month

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Select latest dataset file every month

[ Edited ]

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! 


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 23,663

Re: Select latest dataset file every month

Posted in reply to SteelersPitts

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


All Replies
PROC Star
Posts: 1,769

Re: Select latest dataset file every month

[ Edited ]
Posted in reply to SteelersPitts

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. 

 

 

Occasional Contributor
Posts: 18

Re: Select latest dataset file every month

Posted in reply to novinosrin

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

Occasional Contributor
Posts: 18

SELECT MONTHLY DATA FILES

Posted in reply to SteelersPitts

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! 

SAS Employee
Posts: 6

Re: SELECT MONTHLY DATA FILES

Posted in reply to SteelersPitts
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. 

 

 

Occasional Contributor
Posts: 18

Re: SELECT MONTHLY DATA FILES

Posted in reply to ChanceTGardener

Thank you for your prompt response! 

Solution
2 weeks ago
Super User
Posts: 23,663

Re: Select latest dataset file every month

Posted in reply to SteelersPitts

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! 


 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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