Hi there,
I have a fairly large dataset (data=have) that spans a time period from February 21, 2021 to December 31, 2022. I was wondering if there is an automated function to assign a week number to each date in my dataset. I know SAS usually assigns week numbers by the start of the Calendar year, however I would like Week #1 to be 21FEB2021 to 27FEB2021, Week 2 to 28FEB2021 to 06MAR2021, and so forth. I have included a sample dataset that i would like below (data=want). Thank you once again for any help you might be able to offer!
OBS ID DATE WEEK
1 1001 22FEB2021 1
2 8997 05APR2021
3 5654 30SEP2022
4 3000 07MAR2022 3
5 1002 31OCT2021
6 7984 29FEB2021 2
Consider this example using INTNX.
Data Original;
infile datalines ;
Input ID :4. Date :date9.;
format date date9.;
datalines;
1001 22FEB2021
8997 05APR2021
5654 30SEP2022
3000 07MAR2022
1002 31OCT2021
7984 28FEB2021
;
run;
Data Weeks;
Retain Date1 0;
set original ;
if _n_=1 then Date1='21FEB2021'd;
/* Use INTNX to count weeks between 21FEB2021 and dates in the table*/
Weeks=intck('week', date1,Date, 'continuous');
format date1 date9.;
run;
data have;
input id$ date date9.;
format date date9.;
cards;
1001 22FEB2021
8997 05APR2021
5654 30SEP2022
3000 07MAR2022
1002 31OCT2021
7984 28FEB2021
;
run;
data want;
set have;
week=ceil((date-'21feb2021'd+1)/7);
run;
Assuming you're after some sort of a study week number below should work.
data have;
format date date9.;
do date='21FEB2021'd to '31dec2022'd;
output;
end;
run;
data want;
set have;
study_week=intck('week','21FEB2021'd,date)+1;
run;
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.