BookmarkSubscribeRSS Feed
Almoha
Calcite | Level 5


Hi all,


i have the following , how will i be able to create wk variable and assign week to
each record without writing if conditions or proc format  i.e without having to change the code if any other days are added to the dataset
sub day
111 3
111 12
111 18


sub day wk
111 3 wk1
111 12 wk2
111 18 wk3

Thanks
Al

3 REPLIES 3
novinosrin
Tourmaline | Level 20
data have;
input sub day ;
cards;
111 3 
111 12 
111 18
;

data want;
set have;
wk=cats('wk',ceil(day/7));
run;
PeterClemmensen
Tourmaline | Level 20
data have;
input sub day ;
cards;
111 3 
111 12 
111 18
;

data want;
   set have;
   wk=cats('wk',week(day));
run;
ballardw
Super User

@Almoha wrote:


Hi all,


i have the following , how will i be able to create wk variable and assign week to
each record without writing if conditions or proc format  i.e without having to change the code if any other days are added to the dataset
sub day
111 3
111 12
111 18


sub day wk
111 3 wk1
111 12 wk2
111 18 wk3

Thanks
Al


You may need to define what you mean by "week". There are several common definitions that revolve around what day of the week starts the week and when to consider the start of the week at a year boundary. Given an actual SAS date value the WEEK function with a descriptor of 'U', 'V' or 'W' considers different rules.

 

U

specifies the number-of-the-week within the year. Sunday is considered the first day of the week. The number-of-the-week value is represented as a decimal number in the range 0–53. Week 53 has no special meaning. The value of week('31dec2006'd, 'u') is 53. U is the default value.

Tip The U and W descriptors are similar, except that the U descriptor considers Sunday as the first day of the week, and the W descriptor considers Monday as the first day of the week.
See The U Descriptor

V

specifies the number-of-the-week whose value is represented as a decimal number in the range 1–53. Monday is considered the first day of the week and week 1 of the year is the week that includes both January 4 and the first Thursday of the year. If the first Monday of January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year.

See The V Descriptor

W

specifies the number-of-the-week within the year. Monday is considered the first day of the week. The number-of-the-week value is represented as a decimal number in the range 0–53. Week 53 has no special meaning. The value of week('31dec2006'd, 'w') is 52.

Tip The U and W descriptors are similar except that the U descriptor considers Sunday as the first day of the week, and the W descriptor considers Monday as the first day of the week.
See The W Descriptor
Default U

Since SAS date values are numeric 3 could be considered as 3JAN1960 and for your example values the week number returned is as you request.

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
  • 821 views
  • 0 likes
  • 4 in conversation