Hi Everyone
I need to automatically extract data from the database on a specific day (5th of each month) for the previous month.
I thought of using the following macros for dates but the problem is the months days are not constant
%let Month_end_date = %sysfunc(putn(%sysfunc(intnx(day, %sysfunc(today()), -5, same)), date9.));
%let Month_Begin_date = %sysfunc(putn(%sysfunc(intnx(day, %sysfunc(today()), -34, same)), date9.));
This will only be applicable for the February in a leap year.
Please help
Thank you
The solution worked
I just formatted the values to date by including a date9. just before the last closing parenthesis:
%let Month_end_date = %sysfunc(intnx(month, &today, -1, e));
%let Month_Begin_date = %sysfunc(intnx(month, &today, -1, b));
Thank you so much once again PaigeMiller
Use the INTNX function differently. SAS has done the hard work of figuring out what the starting and ending dates of each month are, leap year or not. So you don't have to figure it out somehow.
Do NOT format macro variables when using them to extract data from data bases and the macro variable represent days. These values are the number of days since 01JAN1960, which is how SAS represents days and will work when you extract from the database. Using values like 01JAN2024 will not work when extracting data from a database.
%let today=%sysfunc(today());
%let Month_end_date = %sysfunc(intnx(month, &today, -1, e));
%let Month_Begin_date = %sysfunc(intnx(month, &today, -1, b));
%put &=today;
%put &=month_end_date;
%put &=month_begin_date;
Thank you
The solution worked
I just formatted the values to date by including a date9. just before the last closing parenthesis:
%let Month_end_date = %sysfunc(intnx(month, &today, -1, e));
%let Month_Begin_date = %sysfunc(intnx(month, &today, -1, b));
Thank you so much once again PaigeMiller
If you are using these values to extract data from databases, formatting these macro variables is not the correct thing to do. You would use the macro variables un-formatted.
Saying that you can't get it right provides us no information that would help us solve your problem.
If there are ERRORs in the log, show us the log. If there are no errors but the results are wrong, show us your code and the incorrect results.
Thank you
ERROR: Cannot open %INCLUDE file EXECU1.
This has nothing to do with your macro variables. You are trying to use a file that has not been properly identified.
SAS does not know what EXECU1 is. You need a RUN; to close the DATA step, and then the file is created properly and will then SAS will be able to %include it.
When you create macro variables to contain dates, you still have to use proper SAS code.
put "'" start_date +(-1) "'d and" ;
put "'" end_date "'d";
Start_date and end_date are not variables, they are macro variables. So you want to include an ampersand before the variable name. In addition, if the macro variables are unformatted as I suggested, then you want
put "&start_date and &end_date";
How simple is that? I am not sure why you are adding -1 in your code, but if you think that's still needed, modify the code above to subtract 1.
Question: why is this a macro anyway? Why not just have DATA _NULL_ followed by %include outside of a macro?
So where are the macro variables used?
What is a typical value of start_date and end_date? Is it numeric or character according to PROC CONTENTS? How is it formatted according to PROC CONTENTS?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.