Desktop productivity for business analysts and programmers

Filter last month

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Filter last month

Hi! I'm trying to filter a data set that contains information from 2010 until yesterday. However, the goal here is to filter only data from the last month, i.e., suppose i have transactions from 10jan2018:18:29:45 to 11mar2018:14:23:39, and i only need those from 01feb2018:0:0:0 to 01mar2018:0:0:0. How can I do this? Notice I already used prompts but since this is going to be an automatic report, i need to have it hard coded without waiting for a user response in the middle of the project. Any help will be much appreciated!

Accepted Solutions
Solution
‎03-12-2018 06:13 PM
SAS Super FREQ
Posts: 830

Re: Filter last month

Posted in reply to GuillermoPH

Hi

 

You can use the INTNX function to do this, depending on how you want to use it, you could use the SAS Macro language to create the start end end values. Please note the macro variables currently just contain the correct string representation to actually make a proper date value out of it, you need to put them into quotes and add the DT so that in the end, you get "01FEB2018:00:00:00"dt

 

%let currentDT = %sysfunc( datetime() );
%let prevMonthStart = %sysfunc( intnx(DTMONTH, &currentDT, -1, B), datetime19.);
%let prevMonthEnd = %sysfunc( intnx(DTMONTH, &currentDT, 0, B), datetime19.);


%put &=prevMonthStart &=prevMonthEnd;

or your filter expression in a query looks like

 

t1.dt BETWEEN intnx("DTMONTH", datetime(), -1, "B") AND intnx("DTMONTH", datetime(), 0, "B")

View solution in original post


All Replies
Solution
‎03-12-2018 06:13 PM
SAS Super FREQ
Posts: 830

Re: Filter last month

Posted in reply to GuillermoPH

Hi

 

You can use the INTNX function to do this, depending on how you want to use it, you could use the SAS Macro language to create the start end end values. Please note the macro variables currently just contain the correct string representation to actually make a proper date value out of it, you need to put them into quotes and add the DT so that in the end, you get "01FEB2018:00:00:00"dt

 

%let currentDT = %sysfunc( datetime() );
%let prevMonthStart = %sysfunc( intnx(DTMONTH, &currentDT, -1, B), datetime19.);
%let prevMonthEnd = %sysfunc( intnx(DTMONTH, &currentDT, 0, B), datetime19.);


%put &=prevMonthStart &=prevMonthEnd;

or your filter expression in a query looks like

 

t1.dt BETWEEN intnx("DTMONTH", datetime(), -1, "B") AND intnx("DTMONTH", datetime(), 0, "B")
Occasional Contributor
Posts: 15

Re: Filter last month

Posted in reply to Bruno_SAS
Thanks a lot Bruno_SAS! It worked beautifully, I used the second option since I'm working on queries. Regards,
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 191 views
  • 1 like
  • 2 in conversation