BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GuillermoPH
Obsidian | Level 7
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!
1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

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

2 REPLIES 2
BrunoMueller
SAS Super FREQ

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")
GuillermoPH
Obsidian | Level 7
Thanks a lot Bruno_SAS! It worked beautifully, I used the second option since I'm working on queries. Regards,

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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