BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chris2377
Quartz | Level 8

Hi,

 

I have a long series of data, recorded daily from Monday to Friday. I'd like to add a variable that numbers consecutive weeks - it will equal 1 for the first 5 obs, 2 for observations 6-10 etc. (like in the example below). How to do it?

 

data have;
		input date :yymmdd10.;
		format date yymmdd10.;
		datalines;
		2020-01-06
		2020-01-07
		2020-01-08
		2020-01-09
		2020-01-10
		2020-01-13
		2020-01-14
		2020-01-15
		2020-01-16
		2020-01-17
		2020-01-20
		2020-01-21
		2020-01-22
		2020-01-23
		2020-01-24
		;
	run;

	data want;
		input date :yymmdd10. week_no;
		format date yymmdd10.;
		datalines;
		2020-01-06 1
		2020-01-07 1
		2020-01-08 1
		2020-01-09 1
		2020-01-10 1
		2020-01-13 2
		2020-01-14 2
		2020-01-15 2
		2020-01-16 2
		2020-01-17 2
		2020-01-20 3
		2020-01-21 3
		2020-01-22 3
		2020-01-23 3
		2020-01-24 3
		;
	run;

Best

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input date :yymmdd10.;
format date yymmdd10.;
datalines;
2020-01-06
2020-01-07
2020-01-08
2020-01-09
2020-01-10
2020-01-13
2020-01-14
2020-01-15
2020-01-16
2020-01-17
2020-01-20
2020-01-21
2020-01-22
2020-01-23
2020-01-24
;

data want;
   set have;
   if mod(_N_, 5) = 1 then weekno + 1;
run; 

View solution in original post

6 REPLIES 6
data_null__
Jade | Level 19
w = week(date);
chris2377
Quartz | Level 8
@data_null__
I've tried that first, but it doesn't work properly when there's a year change
PeterClemmensen
Tourmaline | Level 20

How about

 

data have;
input date :yymmdd10.;
format date yymmdd10.;
datalines;
2020-01-06
2020-01-07
2020-01-08
2020-01-09
2020-01-10
2020-01-13
2020-01-14
2020-01-15
2020-01-16
2020-01-17
2020-01-20
2020-01-21
2020-01-22
2020-01-23
2020-01-24
;

data want;
   set have;
   if mod(_N_, 5) = 1 then weekno + 1;
run; 
PeterClemmensen
Tourmaline | Level 20

Anytime. My code is a direct solution to your specific problem. I must address, that you should take advatage of the week() function if applicable.

PaigeMiller
Diamond | Level 26

I guess I have concerns about both of the above solutions, as it could be possible that they fail in certain situations. In particular, relying on _N_ requires that there always be five days listed for each week; if a day isn't listed because its a holiday, then this method fails. The WEEK function works fine within a year, but if the data set is 53 or more weeks long, then (for example), Jan 3 2020 and Jan 3 2021 would be the same week.

 

I would use the INTCK function to determine the number of weeks after the first date.

--
Paige Miller

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
  • 6 replies
  • 674 views
  • 0 likes
  • 4 in conversation