BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Saba1
Quartz | Level 8

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
Ksharp
Super User

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;

View solution in original post

4 REPLIES 4
SASKiwi
PROC Star

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.

 

 

Ksharp
Super User
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;
Ksharp
Super User

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;
Saba1
Quartz | Level 8
Thank you so much

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2251 views
  • 1 like
  • 3 in conversation