BookmarkSubscribeRSS Feed
Boswser
Obsidian | Level 7
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!
3 REPLIES 3
Reeza
Super User

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:

  • Interval - specifies a character constant, variable, or expression that contains a time interval such as WEEK, SEMIYEAR, QTR, or HOUR. Interval can appear in uppercase or lowercase.
  • Start From - specifies a SAS expression that represents a SAS date, time, or datetime value that identifies a starting point.
  • Increment - specifies a negative, positive, or zero integer that represents the number of date, time, or datetime intervals. Increment is the number of intervals to shift the value of start-from.
  • Alignment - controls the position of SAS dates within the interval. You must enclose alignment in quotation marks

So for your function you have the following:

 

  • Interval = "WEEK"
  • Start FROM = TODAY()
  • Increment = -1

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!

 

Tom
Super User Tom
Super User

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.

 

 

ballardw
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 438 views
  • 2 likes
  • 4 in conversation