Hello,
I need a macro variable that I can put in the filter to put my date between the first and last day of the previous month.
For example, I want to take a column of some table and in a filter to put that column and between &first_day_previous_month and &last_day_previous_month
Example:
Thanks in advance!
OPEN_DATE is format 20DEC2002:00:00:00
This is the key piece of information that was missing. Your variable OPEN_DATE is not a DATE at all (I work with databases with the same problem). It is a date/time value, and so the macro variables created must contain date/time values.
%let first_day_previous_month = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1,b));
%let last_day_previous_month = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1,e));
%let first_day_previous_month = %sysfunc(intnx(month,%sysfunc(today()),-1,b));
%let last_day_previous_month = %sysfunc(intnx(month,%sysfunc(today()),-1,e));
Yes. The macro variables contain the raw date value (count of days from 1960-01-01) and can be used in calculations and comparisons.
@tijanagilic wrote:
What do I put in the filter?
%first_day_previous_month and %last_day_previous_month ?
I think you have to run the code provided by @Kurt_Bremser first, before you can use these variables to filter.
Then, in the filter dialog box, you want
&first_day_previous_month
&last_day_previous_month
Is open_date a true SAS date value? Please run PROC CONTENTS on your dataset and show the output for the variable.
@tijanagilic wrote:
I've done it, but it's not giving me data, just an empty table.
In addition to the advice from @Kurt_Bremser to show PROC CONTENTS output on this variable, I suppose it is possible that the correct answer is an empty table. Can you confirm somehow (but searching the table "manually") that there are record(s) that should be selected by this filter?
Are you using Enterprise Guide? If so you can create a code block and run it so that the macro variables exist.
Can you explain more completely what you are doing that is prompting you for start and stop datetimes? Can you manually enter values into the prompts and get it to work? What do the values you entered look like?
OPEN_DATE is format 20DEC2002:00:00:00
This is the key piece of information that was missing. Your variable OPEN_DATE is not a DATE at all (I work with databases with the same problem). It is a date/time value, and so the macro variables created must contain date/time values.
%let first_day_previous_month = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1,b));
%let last_day_previous_month = %sysfunc(intnx(dtmonth,%sysfunc(datetime()),-1,e));
You need to know your data.
Do any of the values in the dataset have non zero time of day parts?
Note that '31DEC2023:08:00:00'dt is greater than '31DEC2023:00:00:00'dt so filtering on values between '01DEC2023:00:00:00'dt and '31DEC2023:00:00:00t'dt will miss most of the values on the last day of the month.
@Tom wrote:
You need to know your data.
Do any of the values in the dataset have non zero time of day parts?
Note that '31DEC2023:08:00:00'dt is greater than '31DEC2023:00:00:00'dt so filtering on values between '01DEC2023:00:00:00'dt and '31DEC2023:00:00:00t'dt will miss most of the values on the last day of the month.
For the above solution, I point out that the value of &last_day_of_previous_month is 2019686399 which turns out to be 31DEC2023:23:59:59 (if run on 03JAN2024, so this handles the case you mention.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.