BookmarkSubscribeRSS Feed
jnivi
Calcite | Level 5

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

4 REPLIES 4
Hao_Luo
SAS Employee
"29FEB2021" may not be a valid date.
john_mccall
SAS Employee

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;

Hao_Luo
SAS Employee
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;

3.png

Patrick
Opal | Level 21

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;

 

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!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 635 views
  • 1 like
  • 4 in conversation