HYYWD: Returning the Year and Week Number From a Date-Time Value

How to:

The week number returned by HNAME and HPART can actually be in the year preceding or following the input date.

The HYYWD function returns both the year and the week number from a given date-time value.

The output is edited to conform to the ISO standard format for dates with week numbers, yyyy-Www-d.

Syntax: How to Return the Year and Week Number From a Date-Time Value

HYYWD(dtvalue, output)

where:

dtvalue

Date-time

Is the date-time value to be edited, the name of a date-time field that contains the value, or an expression that returns the value.

output

Alphanumeric

Is the field that contains the result, or the format of the output value enclosed in single quotation marks.

The output format must be at least 10 characters long. The output is in the following format:

yyyy-Www-d

where:

yyyy

Is the four-digit year.

ww

Is the two-digit week number (01 to 53).

d

Is the single-digit day of the week (1 to 7). The d value is relative to the current WEEKFIRST setting. If WEEKFIRST is 2 or ISO2 (Monday), then Monday is represented in the output as 1, Tuesday as 2.

Using the EDIT function, you can extract the individual subfields from this output.

Example: Returning the Year and Week Number From a Date-Time Value

The following request against the VIDEOTR2 data source calls HYYWD to convert the TRANSDATE date-time field to the ISO standard format for dates with week numbers. WEEKFIRST is set to ISO2, which produces ISO standard week numbering:

SET WEEKFIRST = ISO2
TABLE FILE VIDEOTR2
SUM TRANSTOT QUANTITY
COMPUTE ISODATE/A10 = HYYWD(TRANSDATE, 'A10');
BY TRANSDATE
WHERE QUANTITY GT 1
END

The output is:

TRANSDATE         TRANSTOT  QUANTITY  ISODATE   
---------         --------  --------  -------   
1991/06/24 04:43     16.00         2  1991-W26-1
1991/06/25 01:17      2.50         2  1991-W26-2
1991/06/27 02:45     16.00         2  1991-W26-4
1996/08/17 05:11      5.18         2  1996-W33-6
1998/02/04 04:11     12.00         2  1998-W06-3
1999/01/30 04:16     13.00         2  1999-W04-6
1999/04/22 06:19      3.75         3  1999-W16-4
1999/05/06 05:14      1.00         2  1999-W18-4
1999/08/09 03:17     15.00         2  1999-W32-1
1999/09/09 09:18     14.00         2  1999-W36-4
1999/10/16 09:11      5.18         2  1999-W41-6
1999/11/05 11:12      2.50         2  1999-W44-5
1999/12/09 09:47      5.18         2  1999-W49-4
1999/12/15 04:04      2.50         2  1999-W50-3

Example: Extracting a Component From a Date Returned by HYYWD

The following request against the VIDEOTR2 data source calls HYYWD to convert the TRANSDATE date-time field to the ISO standard format for dates with week numbers. It then uses the EDIT function to extract the week component from this date. WEEKFIRST is set to ISO2, which produces ISO standard week numbering:

SET WEEKFIRST = ISO2
TABLE FILE VIDEOTR2
SUM TRANSTOT QUANTITY
COMPUTE ISODATE/A10 = HYYWD(TRANSDATE, 'A10');
COMPUTE WEEK/A2 = EDIT(ISODATE, '$$$$$$99$$');
BY TRANSDATE 
WHERE QUANTITY GT 1 AND DATE EQ 1991
END

The output is:

TRANSDATE         TRANSTOT  QUANTITY  ISODATE     WEEK
---------         --------  --------  -------     ----
1991/06/24 04:43     16.00         2  1991-W26-1  26
1991/06/25 01:17      2.50         2  1991-W26-2  26
1991/06/27 02:45     16.00         2  1991-W26-4  26