Date: Thu, 24 May 2001 06:43:49 -0700
Reply-To: Pete Lund <pete.lund@NWCSR.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Pete Lund <pete.lund@NWCSR.COM>
Subject: Re: week number counting?
In-Reply-To: <3B0CCD5C.858DF77A@vyh.fi>
Content-Type: text/plain; charset="us-ascii"
Arjen-
As you know, INTNX will always return the first date of the period you
reference. With INTNX('WEEK',<date>,0) you'll get the first day of the week
that contains Jan 1. Using WEEK.5 gives you the Thursday of that week.
However, as you found, if the first of January is on a Friday or Saturday
then the Thursday of that week will be in the previous year. It sounds like
what you really need is an offset for the SAS week to account for that. If
you're using V8 you can use a PICTURE format to get the week number and use
that to create an offset to use in your INTNX function.
proc format;
picture WeekNum
low - high = '%U' (datatype=date);
run;
data _null_;
baseweek = put(intnx('WEEK.5',"01JAN&year"d,0),WeekNum.) ne '0';
firstthursday=put(intnx('WEEK.5',"01JAN&year"d,BaseWeek), eurdfdd10.);
put baseweek= firstthursday=;
run;
Now, BASEWEEK will be an offset you can use in your "first thursday"
calculation. Let's see how it works:
- INTNX('WEEK.5',"01JAN&YEAR"d,0) will return the Thursday of the first
week of the year (see above)
- The WeekNum. format will return the week number of that date - either 0
or 52 (if the Thursday is in the previous year)
- Now, just compare that result to '0' - if it is '0' we're in the right
year and the "ne 0" will return 0. If it's 52, the "ne 0" will return 1.
So, we now have an offset value that can be used in your original
FirstThursday calculation. Just replace the "0" with "BaseWeek" and you
should be the date of the first Thursday that's really in the year in
question.
You could also create a macro variable containing the BaseYear value and you
wouldn't have to recreate it over and over:
%let BaseWeek = %eval(%sysfunc(intnx(week.5,"01jan&year"d,0),WeekNum.) ne
'0');
Then, your datastep line would be:
firstthursday=put(intnx('WEEK.5',"01JAN&year"d,&BaseWeek), eurdfdd10.);
Hope this helps-
----------------------------------------------------------------------------
---
Pete Lund
Northwest Crime and Social Research
313-D Fifth Ave SE
Olympia, WA 98501
(360) 528-8970 - voice
(360) 280-4892 - cell
(360) 570-7533 - fax
pete.lund@nwcsr.com
www.nwcsr.com
----------------------------------------------------------------------------
---
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU]On Behalf Of
Arjen Raateland
Sent: Thursday, May 24, 2001 1:59 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: week number counting?
In Finland the weeks of the year are numbered starting with the week
that has the first thursday of the new year. SAS seems to number weeks
differently, as seen from the following:
%let year=1998;
data _null_;
firstthursday=put(intnx('WEEK.5',"01JAN&year"d,0), eurdfdd10.);
put firstthursday;
run;
%let year=1999;
data _null_;
firstthursday=put(intnx('WEEK.5',"01JAN&year"d,0), eurdfdd10.);
put firstthursday;
run;
2132 %let year=1998;
...
01.01.1998
2139 %let year=1999;
...
31.12.1998
The latter date is in the previous year ...
I'm looking for a formula that produces the date of Wednesday in the
week with a particular week number (according to the above definition),
but I can't seem to figure it out. Help will be appreciated.
TIA,
--
Arjen Raateland
Finnish Environment Institute
SAS Support
phone +358 9 4030 0350