BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

I would like to count the weeks in the same way like the function no.semaine in Excel.

For exemple, in my code, I'm applying the sas sas function week for 01/01/2024 and I get the week 0, but I would like to have the week 1. The 01/01/2024 starts in Monday, so I would like that the weeks starts on monday and has the values from 1 to 53.

 

Thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
SASdevAnneMarie
Barite | Level 11
Thank you, but I tried all descriptors, I don't have the right values. With 'V' descriptor I have the 30 JUN 2024 as week 26, but with Excel function it is the week 27. 😞 I don't know how to get the same values as in Excel function, please.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

You could use the week function with the 'V' descriptor, according to the documentation

 

specifies the number-of-the-week whose value is represented as a decimal number in the range 1–53. Monday is considered the first day of the week and week 1 of the year is the week that includes both January 4 and the first Thursday of the year. If the first Monday of January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year.

so this seems as if it will do what you want.

 

If you want something else, it seems as if that is not defined in SAS, but should be easy to program based on the results of the above and whatever your specific requirements are.

--
Paige Miller
SASdevAnneMarie
Barite | Level 11
Thank you, but I tried all descriptors, I don't have the right values. With 'V' descriptor I have the 30 JUN 2024 as week 26, but with Excel function it is the week 27. 😞 I don't know how to get the same values as in Excel function, please.
PaigeMiller
Diamond | Level 26

For certain days of the week, you add 1 to the week number. You should be able to compare the SAS output to the Excel output and see what days the week numbers differ, and then fix it for those days of the week by adding 1.

--
Paige Miller
s_lassen
Meteorite | Level 14

If you are calling the WEEKNUM function in Excel without any "Return type" (the second parameter), the result should be like the WEEK function in SAS with the 'U' modifier, except that the first week in Excel always has number 1. So, if the first day of the year is a Sunday, the two calls should return the same values for that year, but if it is not, the Excel function will return a value that is one greater than the SAS function.

 

You should be able to get the same result as in Excel like this:

week_no=week(date,'U')+(weekday(mdy(1,1,year(date)))>1);

- assuming your date variable is called DATE.

 

If you implement this code, I would recommend putting in a comment (you can steal the text I wrote above, be my guest), as the code by itself is pretty cryptic.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1947 views
  • 2 likes
  • 3 in conversation