DATA Step, Macro, Functions and more

8 Day interval starting on Friday

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

8 Day interval starting on Friday

Hi, 

I'm trying to create a 8 day interval that starts on Friday in order to get the previous Friday date to then select data from a Table with dates that go from Friday to Friday independently of the day the process runs.

Im trying this code:

 

data test;
sexta = intnx('day8.6', '05Jan2017'd, 0,'BEGIN');
format sexta weekdate.;
run;

When I use the date specified is the current date, but if I use the diferent dates (like the ones in the code above) it gets completly diffent days ( in the exemple I get a Monday).

 

Any idea why this happens?

 


Accepted Solutions
Solution
‎04-06-2017 10:32 AM
Super User
Posts: 17,905

Re: 8 Day interval starting on Friday

Use 

 

first of current week

 

intnx('week', today(), 0, 'b');

 

subtract 2 for Friday. 

 

Last_friday = intnx('week', today(), 0, 'b') - 2;

View solution in original post


All Replies
Super User
Super User
Posts: 7,413

Re: 8 Day interval starting on Friday

The function does not work the way you think it does.  Refer to the manual.  If you want one observation per day for each of the eight days then you need a loop from date-8 to date, and an output:

data test;
  do sexta='05Jan2017'd - 7 to '05jan2017'd;
    output;
  end;
  format sexta weekdate.;
run;
Super User
Posts: 17,905

Re: 8 Day interval starting on Friday

That's a really uncommon calculation to the point I suspect it may not be correct. 

 

Can you explain more what you're trying to do? And remember that dates are numbers so we can do subtraction to get dates of interest. 

Occasional Contributor
Posts: 12

Re: 8 Day interval starting on Friday

I trying to create a SAS DI studio job that will run every week, typically on a Friday. But there are things that might change the day the job will execute (like holidays for exemple). The job will get data from tables and is supposed to get the data from the previous Friday until the run day. So I was trying to use the INTNX function to get the previous friday no matter what day it runs, that is why I indicated a 8 day interval that starts on Friday.

Solution
‎04-06-2017 10:32 AM
Super User
Posts: 17,905

Re: 8 Day interval starting on Friday

Use 

 

first of current week

 

intnx('week', today(), 0, 'b');

 

subtract 2 for Friday. 

 

Last_friday = intnx('week', today(), 0, 'b') - 2;

Occasional Contributor
Posts: 12

Re: 8 Day interval starting on Friday

that'll do it yes, thanks
Super User
Super User
Posts: 7,413

Re: 8 Day interval starting on Friday

Then simply:

data want;
  set have;
  where today() - 8 <= date <= today();
run;

Will get up to 8 days prior based on date.

Occasional Contributor
Posts: 12

Re: 8 Day interval starting on Friday

[ Edited ]

I thought about that, but I don't need 8 days, I need exactly Friday.
Reeza's response will do it, because using 'week'  as the interval I seem to always get sunday.

Super User
Posts: 17,905

Re: 8 Day interval starting on Friday


Ricardo_Neves wrote:

I thought about that, but I don't need 8 days, I need exactly Friday.
Reeza's response will do it, because using 'week'  as the interval I seem to always get sunday.


This is more due to using a 0 and the alignment parameter of 'b' to align at the beginning of the week which is Sumday. 

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 123 views
  • 1 like
  • 3 in conversation