Sample 24619: Determine the week number of the year
Use the INTNX and INTCK functions to determine the
week of the year (1 through 52 or 53) for a specified
date.
Note:
The INTCK function returns the integer number of time intervals in a given time span. By default, Sunday is the beginning of the week interval. Use 'shift intervals' to change the starting day for the interval. See the SAS Language Reference, Dictionary for under functions for more details.
The INTNX function increments (either forwards or
backwards) a date, time or datetime value by a specified
interval.
See the SAS Language Reference, Dictionary under
Functions for more details.
Beginning in SAS 9.1, the WEEK function can be used as an alternative approach and is documented in the SAS NLS User's Guide.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create sample data */
data test;
input date :mmddyy6.;
format date date9.;
datalines;
010106
010406
041806
081806
123106
;
data getweek;
set test;
/* Use INTNX to roll DATE back to the first of the year. */
/* Pass the result as the 'start' parameter to INTCK. */
week=intck('week',intnx('year',date,0),date)+1;
/* If you are in SAS 9.1, you can use the WEEK function. */
/* The WEEK function has an optional second argument that */
/* can be used to specify whether a week starts on Sunday */
/* or Monday. For more information, please refer to the */
/* SAS NLS User's Guide. */
/* U indicates Sunday is the first day of the week */
week_function=week(date,'u');
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
week_
Obs date week function
1 01JAN2006 1 1
2 04JAN2006 1 1
3 18APR2006 16 16
4 18AUG2006 33 33
5 31DEC2006 53 53
Use the INTNX and INTCK functions to determine the week of the year (1 through 52 or 53) for a specified date.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Date and Time
|
Date Modified: | 2006-12-09 03:02:51 |
Date Created: | 2004-09-30 14:08:58 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |