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!
"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.
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:
year_beg_date=intnx('year.7',date,0,'beg');
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) ;
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.