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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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