Let's look at the documentation for starters:
INTNX(interval <multiple><.shift-index>, start-from, increment <, 'alignment'>)
This lists the parameters for the function, specifically:
So for your function you have the following:
This means you're doing -1 week. So if you want -2 weeks, change the Increment to -2 to find the date that is two weeks ago.
But will that be the start of the week, the end of the week, the same day??? That's what the fourth parameter the alignment controls but it's not specified. So then what is the default?
According to the SAS documentation that is 'Beginning' so it will be the beginning of the two week interval.
Now you need to factor in the ENDDATE? That will be either + 13 or +6 depending on exactly what you want.
I'll let you figure out that portion 🙂
Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...
@Boswser wrote:
Hi everyone. I have a run I inherited with a date macro that starts as follows. This run runs every Thursday with the intent to grab a week’s worth of data.
DATA _NULL_;
DT = TODAY();
STARTDT = INTNX(‘WEEK’,DT,-1)-1;
ENDDT = STARTDT + 6;
I’m struggling to wrap my head around the INTNX function even after a lot of googling. I will be missing a week of work soon and will need to have this run go back two weeks instead of just one as it does.
So my question—how do I make it grab two weeks? Should I change the first -1 to a -2, and also change the +6 to a +13 so it grabs 7 more days?
Thanks for any help!
Unless you data source has dates in the future you should move the start back a week and then add a week to the end to make a two week interval.
But you can just try it and see what happens.
1062 DATA _NULL_;
1063 do dt= today(), '31DEC2020'd,'01JAN2021'd ;
1064 STARTDT = INTNX('WEEK',DT,-2)-1;
1065 ENDDT = STARTDT + 13;
1066 put (startdt enddt dt) (weekdate. +2 );
1067
1068 end;
1069 run;
Saturday, July 24, 2021 Friday, August 6, 2021 Thursday, August 12, 2021
Saturday, December 12, 2020 Friday, December 25, 2020 Thursday, December 31, 2020
Saturday, December 12, 2020 Friday, December 25, 2020 Friday, January 1, 2021
I am not sure how Thursday comes into the calculations.
It will help a lot if you can provide an example of things like 1) the actual date the program will run and 2) the start/end dates of the desired interval that you want.
Weeks are tad flaky because when year boundaries are encountered. You might be looking for some of the options like 'B' and 'E' to get the beginning of an interval, possibly coupled with a shift.
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.