Quartz | Level 8

## Finding Week Number of a Month

Hi

I have a dataset with weekly observations for all variables. The format of "Date_week" is "mmddyy10." For every variable, I have one observation per week, but it is not necessarily the last working day of the week.

I want to find out "week number" for every month. I have used following code, but it does not give me the exact week number for the dates that are not the last working day (Friday) of a week.

Please guide me in this regard. Thanks.

``````data tmp1;
set tmp;
Week = ceil( day(Date_week) / 7); run;``````
1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Finding Week Number of a Month

Or you could count the number of Sunday.

``````data _null_;
n=0;
date='26dec2019'd;
do i=intnx('month',date,0) to date;
if weekday(i)=1 then n+1;
end;
put n=;
run;``````
4 REPLIES 4
PROC Star

## Re: Finding Week Number of a Month

If you have weekly data then it may be more useful and definitely easier to explore the SAS WEEK function which will give you the week number in a year. There are different definitions for this so you can choose the one which is most appropriate to your requirements:

https://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n1ka2ulrvrjlasn0z7beco2yrgas.htm...

Week number in a month is likely to cause boundary problems because of the varying number of days in each month. The WEEK function takes care of those types of problems, including dealing with leap years.

Super User

## Re: Finding Week Number of a Month

``````data _null_;
date='26dec2019'd;
n=week(date,'u')-week(intnx('month',date,-1,'e'),'u');
/*
n=week(date,'v')-week(intnx('month',date,-1,'e'),'v');
*/
put n=;
run;``````
Super User

## Re: Finding Week Number of a Month

Or you could count the number of Sunday.

``````data _null_;
n=0;
date='26dec2019'd;
do i=intnx('month',date,0) to date;
if weekday(i)=1 then n+1;
end;
put n=;
run;``````
Quartz | Level 8

## Re: Finding Week Number of a Month

Thank you so much
Discussion stats
• 4 replies
• 2034 views
• 1 like
• 3 in conversation