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

Hi everyone!

I need to code a date variable by grouping the data that belong to the same week, using SAS Visual Analytics.

I found the "WeekNumber" function, that returns the number of the week in the current year. The problem is that I need the weeks to start on Mondays and to end on Sundays, whereas the SAS encoding is from Sunday to Saturday (the US encoding). Is there an easy way to prevent this problem to occur?

Giorgio

1 ACCEPTED SOLUTION

Accepted Solutions
Cameron
Fluorite | Level 6

When building your report you could create a conditional calculated numeric data item that returns WeekNumber for Monday to Saturday and  WeekNumber - 1 for Sunday.  For example, this calculated item text returns the WeekNumber of the variable Date for weeks starting on a Monday :

IF ( DayOfWeek('Date'n) = 1 )

RETURN ( WeekNumber('Date'n) - 1 )

ELSE WeekNumber('Date'n)

The only issue with this calculation is that the days before the first Sunday (or Monday) of the year have a WeekNumber of zero (0) - but that is the same whether your week starts on a Sunday or a Monday as the SAS Visual Analytics WeekNumber function returns 0 for the days between New Year's Day and the first Sunday of the year if New Year's Day is not a Sunday.  If you want New Year's Day to always be in the first week of the year then you also need to test what day New Year's Day falls on and which WeekNumber it is in too and then add 1 to the result of the WeekNumber function for all the other dates that year if New Year's Day would fall in WeekNumber zero (0) or was a Sunday.

View solution in original post

12 REPLIES 12
MichelleHomes
Meteorite | Level 14

Hi Giorgio,

I'm not aware of a way to change the start of the week based on locale. This is similar to discussions in the group about having financial year calculations.

For your week number requirement this can be achieved using SAS data step manipulation as part of the data preparation prior to loading the table into SAS Visual Analytics. There is a function, intck that can return the week number shifted by an interval so you can have it start on a Monday by using week.2 as the interval. Have a look at the user guide at SAS(R) 9.4 Functions - Intck

For example, using something like INTCK("week.2", MDY(1,1,year(today())),date_column). You may need to change the start date depending on your requirements and also add 1 to the result if you don't want the first partial week to have a result of 0.

Kind Regards,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
BrunoMueller
SAS Super FREQ

Hi Giorgio

As Michelle mentioned it is probably best to prepare the data first. SAS has a WEEK Function that will allow you to compute the week according to different rules. The V option is the way we are used to in Europe.

Bruno

MichelleHomes
Meteorite | Level 14

Ahhhh good point to use the Week function if the start is the beginning of the calendar year. I find the INTCK and INTNX functions are my favourites as they are so versatile. 🙂

Cheers,

Michelle

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
data_ipr
Calcite | Level 5

Unfortunately, I do not have a SAS licence (but only a Visual Analytics one). Moreover, in order to implement an automatic flow, I need to run all my analysis based on SAS Visual Analytics, where this feature is absent.

Cameron
Fluorite | Level 6

After some more research, I found that if you have Data Administrator rights, so that you can prepare data using the SAS Visual Data Builder, then you can add calculated column(s) that use SAS functions as part of the expression formula.  However, the WEEK function is not one of the formally listed "FUNCTIONS" keywords in the expression builder. So you would have to manually type the formula expression, using SAS syntax, rather than the Visual Analytics formula syntax that is used in the report designer.  This works for both SAS source tables as well as imported data because Visual Analytics uses a SAS macro (and the SQL Procedure) to prepare the data tables that ultimately get loaded into the SAS LASR Server. Unfortunately, not everyone can be a Data Administrator, so this option may not always be available when designing a report.  Also you might not want to go back and have to load (or reload) a different version of the table just to get something working on a single report, as this could use additional memory or impact on reports that have been built previously.  A better place to address the problem would be in the source data table(s) being loaded into Visual Analytics.

Cameron
Fluorite | Level 6

When building your report you could create a conditional calculated numeric data item that returns WeekNumber for Monday to Saturday and  WeekNumber - 1 for Sunday.  For example, this calculated item text returns the WeekNumber of the variable Date for weeks starting on a Monday :

IF ( DayOfWeek('Date'n) = 1 )

RETURN ( WeekNumber('Date'n) - 1 )

ELSE WeekNumber('Date'n)

The only issue with this calculation is that the days before the first Sunday (or Monday) of the year have a WeekNumber of zero (0) - but that is the same whether your week starts on a Sunday or a Monday as the SAS Visual Analytics WeekNumber function returns 0 for the days between New Year's Day and the first Sunday of the year if New Year's Day is not a Sunday.  If you want New Year's Day to always be in the first week of the year then you also need to test what day New Year's Day falls on and which WeekNumber it is in too and then add 1 to the result of the WeekNumber function for all the other dates that year if New Year's Day would fall in WeekNumber zero (0) or was a Sunday.

data_ipr
Calcite | Level 5

Thank you so much, it was what I was looking for!

BrunoMueller
SAS Super FREQ

Giorgio

You can use the following expression to calculate the week number as it is used in Europe

Format(TreatAs(_Number_, Trunc(( ( ( TreatAs(_Number_, 'startDate'n) - TreatAs(_Number_, DateFromMDY(1, 3, Year(TreatAs(_Date_, ( ( TreatAs(_Number_, 'startDate'n) - DayOfWeek(TreatAs(_Date_, ( TreatAs(_Number_, 'startDate'n) - 1 ))) ) + 4 ))))) ) + DayOfWeek(DateFromMDY(1, 3, Year(TreatAs(_Date_, ( ( TreatAs(_Number_, 'startDate'n) - DayOfWeek(TreatAs(_Date_, ( TreatAs(_Number_, 'startDate'n) - 1 ))) ) + 4 ))))) + 5 ) / 7 ))), 'Z2.0')

Bruno

Refsgaard1
Calcite | Level 5

Hi Bruno

 

I also have the problem med week-number and maked a copy of your expression.

 

But I ha' still a problem. Can you see what my problem is??

 

'Cosmic Kontakt start' is my 'startdate'

 

 

Format(TreatAs(_Number_, Trunc(( ( ( TreatAs(_Number_, ('Cosmic Kontakt start') - TreatAs(_Number_, DateFromMDY(1, 3, Year(TreatAs('Cosmic Kontakt start', ( ( TreatAs(_Number_, 'Cosmic Kontakt start') - DayOfWeek(TreatAs('Cosmic Kontakt start', ( TreatAs(_Number_, 'Cosmic Kontakt start') - 1 ))) ) + 4 ))))) ) + DayOfWeek(DateFromMDY(1, 3, Year(TreatAs('Cosmic Kontakt start', ( ( TreatAs(_Number_, 'Cosmic Kontakt start') - DayOfWeek(TreatAs('Cosmic Kontakt start', ( TreatAs(_Number_, 'Cosmic Kontakt start') - 1 ))) ) + 4 ))))) + 5 ) / 7 ))), 'Z2.0')

BO1
Fluorite | Level 6 BO1
Fluorite | Level 6
This solved for us a deffect, affecting 20 dashboards. THANKS
GenDemo
Quartz | Level 8

Hi All

 

same issue here... I just use this :  Week_Day = ifn(weekday(Date) = 1, 7, weekday(Date)-1);

GenDemo
Quartz | Level 8

or:

Weekday = input(put(Loan_Entry_Date, weekday2.),2.);

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!
Tips for filtering data sources in SAS Visual Analytics

See how to use one filter for multiple data sources by mapping your data from SAS’ Alexandria McCall.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 12 replies
  • 15114 views
  • 8 likes
  • 7 in conversation