BookmarkSubscribeRSS Feed
Whitlea
Obsidian | Level 7

Is there a way to specify a different start date so that week 1 would = the first week in July of each year instead of the first week in January? 

data My_data;
 set data;
week=week(Paydt);
 run;

Thanks! 

3 REPLIES 3
ballardw
Super User

"Week" is a pretty flexible term, which is why the default SAS WEEK function has three optional parameters to work with start of year.

 

You  can use INTCK function to get the number of intervals between two dates:

 

week = intck('week','01JUL2023'd,paydt,'C')

If you have many years of data to do this with you would have to adjust the start date.

Provide some examples as needed.

Note the INTCK function above will assume Saturday is the first day of the week. If that data not meet you need then provide more details.

 

mkeintz
PROC Star

So your year always starts at the beginning of July.  And you want to generate the week number of your date (variable MYDATE) versus the start of the most recent July.  For each date you could do a two line code:

  1. Get the date value for the most recent July 1:
      year_beg_date=intnx('year.7',date,0,'beg');
  2. Count the number of weeks since that date (and add 1, so you don't start with week zero):
    weeknum=intck('week',year_beg_date,mydate)+1;

Or in a single line:

  weeknum=intck('week',intnx('year.7',mydate,0,'beg'),mydate)+1;

The possible problem in the above is that you have to accept that each week starts with Sunday.  So if a July 1st occurs on Saturday, then July 2nd, being a Sunday, would be in week 2.

 

If you want count weeks as starting on the weekday of July 1st, then a simple subtraction  of MYDATE minus the most recent June 30th, then divided by 7 and rounded up:

  weeknum=ceil((mydate-intnx('year.7',mydate,-1,'end'))/7)  ;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
FreelanceReinh
Jade | Level 19

Another approach would be to use the WEEK function with a suitable offset. Suppose you want to mimic week numbers like WEEK(date, 'V'), but with July 1st playing the role of January 1st. Then you could define the offset as

%let d=%sysevalf('01JUL2023'd-'01JAN2000'd);

and compute the week number w for a date as

w=week(date-&d,'V');

The rationale of the offset definition is: 01JUL2023 and 01JAN2000 were the same day of the week (Saturday) and the 12 months following these two dates were leap years (i.e., contained February 29th), so the definition of the week number is consistent for several decades before and after 01JUL2023.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 618 views
  • 4 likes
  • 4 in conversation