Previous Page | Next Page

Functions and CALL Routines

WEEK Function



Returns the week-number value.
Category: Date and Time

Syntax
Arguments
Details
The Basics
The U Descriptor
The V Descriptor
The W Descriptor
Comparisons of Descriptors
Examples
See Also

Syntax

WEEK(<sas-date>, <'descriptor'>)


Arguments

sas-date

specifies the SAS data value. If the sas-date argument is not specified, the WEEK function returns the week-number value of the current date.

descriptor

specifies the value of the descriptor. The following descriptors can be specified in uppercase or lowercase characters.

U (default)

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.

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 4th 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 53.

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

Details


The Basics

The WEEK function reads a SAS date value and returns the week number. The WEEK function is not dependent on locale, and uses only the Gregorian calendar in its computations.


The U Descriptor

The WEEK function with the U descriptor reads a SAS date value and returns the number of the week within the year. The number-of-the-week value is represented as a decimal number in the range 0-53, with a leading zero and maximum value of 53. Week 0 means that the first day of the week occurs in the preceding year. The fifth week of the year is represented as 05.

Sunday is considered the first day of the week. For example, the value of week('01jan2007'd, 'u') is 0.


The V Descriptor

The WEEK function with the V descriptor reads a SAS date value and returns the week number. The number-of-the-week is represented as a decimal number in the range 01-53. The decimal number has a leading zero and a maximum value of 53. Weeks begin on a Monday, and week 1 of the year is the week that includes both January 4th 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. In the following example, 01jan2006 and 30dec2005 occur in the same week. The first day (Monday) of that week is 26dec2005. Therefore, week('01jan2006'd, 'v') and week('30dec2005'd, 'v') both return a value of 52. This means that both dates occur in week 52 of the year 2005.


The W Descriptor

The WEEK function with the W descriptor reads a SAS date value and returns the number of the week within the year. The number-of-the-week value is represented as a decimal number in the range 0-53, with a leading zero and maximum value of 53. Week 0 means that the first day of the week occurs in the preceding year. The fifth week of the year would be represented as 05.

Monday is considered the first day of the week. Therefore, the value of week('01jan2007'd, 'w') is 1.


Comparisons of Descriptors

U is the default descriptor. Its range is 0-53, and the first day of the week is Sunday. The V descriptor has a range of 1-53 and the first day of the week is Monday. The W descriptor has a range of 0-53 and the first day of the week is Monday.

The following list describes the descriptors and an associated week:


Examples

The following example shows the values of the U, V, and W descriptors for dates near the end of certain years and the beginning of the next year. Examining the full data set illustrates how the behavior can differ between the various descriptors depending on the day of the week for January 1. The output displays the first 20 observations:

options pageno=1 nodate ls=80 ps=64;

title 'Values of the U, V, and W Descriptors';
data a(drop=i date0 date1 y);
     date0 = '20dec2005'd;
     do y = 0 to 5;
        date1 = intnx("YEAR",date0,y,'s');
        do i = 0 to 20;
          date = intnx("DAY",date1,i);
          year = YEAR(date);
          week   = week(date); 
          week_u = week(date, 'u'); 
          week_v = week(date, 'v'); 
          week_w = week(date, 'w'); 
          output;
        end;
     end;
     format date WEEKDATX17.;
run;
proc print;
run;

Results of Identifying the Values of the U, V, and W Descriptors

                     Values of the U, V, and W Descriptors                     1

     Obs                 date    year    week    week_u    week_v    week_w

       1     Tue, 20 Dec 2005    2005     51       51        51        51  
       2     Wed, 21 Dec 2005    2005     51       51        51        51  
       3     Thu, 22 Dec 2005    2005     51       51        51        51  
       4     Fri, 23 Dec 2005    2005     51       51        51        51  
       5     Sat, 24 Dec 2005    2005     51       51        51        51  
       6     Sun, 25 Dec 2005    2005     52       52        51        51  
       7     Mon, 26 Dec 2005    2005     52       52        52        52  
       8     Tue, 27 Dec 2005    2005     52       52        52        52  
       9     Wed, 28 Dec 2005    2005     52       52        52        52  
      10     Thu, 29 Dec 2005    2005     52       52        52        52  
      11     Fri, 30 Dec 2005    2005     52       52        52        52  
      12     Sat, 31 Dec 2005    2005     52       52        52        52  
      13      Sun, 1 Jan 2006    2006      1        1        52         0  
      14      Mon, 2 Jan 2006    2006      1        1         1         1  
      15      Tue, 3 Jan 2006    2006      1        1         1         1  
      16      Wed, 4 Jan 2006    2006      1        1         1         1  
      17      Thu, 5 Jan 2006    2006      1        1         1         1  
      18      Fri, 6 Jan 2006    2006      1        1         1         1  
      19      Sat, 7 Jan 2006    2006      1        1         1         1  
      20      Sun, 8 Jan 2006    2006      2        2         1         1  
 

See Also

Functions:

INTNX Function

Formats:

WEEKUw. Format

WEEKVw. Format

WEEKWw. Format

Informats:

WEEKUw. Informat

WEEKVw. Informat

WEEKWw. Informat

Previous Page | Next Page | Top of Page