Date: Wed, 8 Jan 2003 16:52:50 -0500
Reply-To: Ian Whitlock <WHITLOI1@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <WHITLOI1@WESTAT.COM>
Subject: Re: Weeknumbers for Europe / The Netherlands
Content-Type: text/plain; charset="iso-8859-1"
Stef,
I messed up the last step in my previous message. It always uses the end of
the week in calculating EWEEK. This was fine for current dates where the
first part of the year is in the first week. However, in 1993 the first
part of the year was in the last week of 1992, i.e. the reverse problem.
There the code yields week 0 instead of 53 in this case.
The algorithm to decide when to base things on the beginning of the week and
when to use the end of the week takes some work, so I decided to write a
macro that could be called as a DATA step function to mean I would not have
to reconsider the problem again. I added a parameter TYPE that can be set
to US or EURO so that it will calculate weeks in either form.
Here is the macro with sufficient test code to verify what it does. I added
the variable, SASWEEK, to compare with mine. They are the same except for
the handling of the week cutting across years. SASWEEK is always for the
year of the given date. The consequence is that week will not always be 7
days. I also added my original code for European weeks that makes the week
0 when it should have been at the end of the previous year.
%macro WeekNum ( date
, type = EURO /* or US */
) ;
/*
U.S.: FDoW = Sunday and FWoY = The week containing Jan. 1st
Europe: FDoW = Monday and FWoY = The week containing Jan.4th
Given DATE count week boundaries from begining of year to date
Problem: Given date may belong to previous or next year
For example using TYPE=EURO:
30dec2002 is in week 1 of 2003
3jan1993 is in week 52 of year 1992
Note: the problem is inherent in the definition of FW0Y
*/
%local fday keyday pos ;
%if %upcase(&type) = EURO %then
%do ;
%let fday = 2 ;
%let keyday = 4 ;
%end ;
%else
%if %upcase(&type) = US %then
%do ;
%let fday = 1 ;
%let keyday = 1 ;
%end ;
%else
%do ;
%put ERROR: Unexpected type, &type ;
%goto mexit ;
%end ;
%let pos = (max ((intnx( "week.&fday", mdy(1,4,year(&date)+1), 0 )
= intnx ( "week.&fday", &date, 0 ))
, (intnx ( "week.&fday", mdy(1,4,year(&date)) , 0 )
= intnx ( "week.&fday" , &date , 0 ))
) + 1) ;
/* return value */
(intck("week.&fday"
,intnx("year"
,intnx("week.&fday",&date,0,substr("BE",&pos,1))
,0)+%eval(&keyday-1)
,&date)+1)
%mexit:
%mend weeknum ;
/* test code */
data _null_ ;
yd = "1jan1990"d ;
do i = 0 to 15 ;
do datevar = intnx("year",yd,i) -10 to
intnx("year",yd,i) + 9 ;
eweek=intck('week.2'
,intnx('year'
,intnx("week.2",datevar,0,"e")
,0)+3
,datevar)+1;
EuroWeek = %weeknum ( datevar ) ;
USweek = %weeknum ( datevar , type = US ) ;
SASweek=intck('week',intnx('year',datevar,0),datevar)+1;
put datevar= Eweek= Euroweek= USweek= SASweek= ;
end ;
end ;
run ;
Finally I should note that for European weeks my macro agrees with the one
submitted by Gerhard Hellriegel [ghellrieg@T-ONLINE.DE]. For the case of
interest to me, his macro would reduce to
%macro nweeks ( datum ) ;
int((juldate(&datum-weekday(&datum-1)+4)-
juldate(intnx('YEAR',&datum-weekday(&datum-1)+4,0)))/7)
+1
%mend nweeks ;
Hence it provides much the most compact form for obtaining the week number.
IanWhitlock@westat.com
-----Original Message-----
From: Ian Whitlock
Sent: Wednesday, January 08, 2003 11:55 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: Weeknumbers for Europe / The Netherlands
Stef,
The SAS site listed gave for an American week
data getweek;
datevar=today();
week=intck('week',intnx('year',datevar,0),datevar)+1;
run;
If you were satisfied with this answer then for the European week use
data getweek;
datevar=today();
eweek=intck('week.2',intnx('year',datevar,0)+3,datevar)+1;
run ;
However be aware of an anomally. 31dec2002 and 1jan2003 are in the same
calendar week, but one date gives week 53 and the other gives week 1 for
either of the above steps.
Another possibility is
data getweek;
datevar=today();
eweek=intck('week.2'
,intnx('year'
,intnx("week.2",datevar,0,"e")
,0)+3
,datevar)+1;
run ;
However, you now have another anomaly. The first day of the week is not in
the same year as the week.
Of course, you must accept some anomaly if you insist on counting weeks in a
year. It is a simple consequence of what happens when the programmer is
rushed. There are times when I wish the world was created in 8 days (maybe
10) and some of the time/date units worked out evenly.
IanWhitlock@westat.com
-----Original Message-----
From: Stef Boreel [mailto:stef.boreel@ACHMEA.NL]
Sent: Wednesday, January 08, 2003 10:22 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Weeknumbers for Europe / The Netherlands
Dear you all,
Hereby a question about weeknumbers. I found out (see the link:
http://home.t-online.de/home/PeterJHaas/DateTime/WeekNumber_en.htm)
that there are different ways to determine the First Day of the Week (FDoW)
and the First Week of the Year (FWoY ).
On the SAS site
(http://www.sas.com/service/techsup/faq/data_step/logifunc247.html) I found
a way to determine the US FDoW and FWoY,
but now I am looking for a way to code it in the Europe way.
U.S.: FDoW = Sunday and FWoY = The week containing Jan. 1st
Europe: FDoW = Monday and FWoY = The week containing Jan.4th
My question: does anybody know how to code to retrieve the correct
weeknumber for Europe?
Thank you in advance,
Stef Boreel
********************* DISCLAIMER *********************
De informatie in dit e-mail bericht is uitsluitend
bestemd voor de geadresseerde. Verstrekking aan
en gebruik door anderen is niet toegestaan.
Door de electronische verzending van het bericht
kunnen er geen rechten worden ontleend aan de
informatie.
************************************************************