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

 Hello,

 

I have SAS date for various events and for each observation I want to determine which week of the year the event happen. However, i want that the week would start on Friday, not Sunday. How can i do this?

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Again:

data want;
set your_dataset;
week=intck('week.6', intnx('year', your_sasdatevar, 0), your_sasdatevar)+1;
run;

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

You need to use the INTCK function. You can specify what day of the week the function should start from, in your case Friday.

 

See: https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=p...

--
Paige Miller
Astounding
PROC Star

If January 1 is a Thursday, do you want the first week of the year to contain just a single day?  Then January 2 through January 8 would be week 2?

viollete
Calcite | Level 5

yes, for example in 2018 1 week would be from 1 January till 4 January, second from 5 to 8 and so on...

 

I looked at INTCK function, however i need to give start and end dates, but I have a variable and i do not  want to look what time period it consists every time i use it.

FreelanceReinh
Jade | Level 19

Hi @viollete,

 

Try this:

data have;
input d yymmdd8.;
format d weekdatx.;
cards;
20151231
20160101
20171228
20171229
20171231
20180101
20180104
20180105
20180907
;

data want;
set have;
wk=intck('week.6', intnx('year',d,0), d)+1;
run;

proc print data=want;
run;
ballardw
Super User

@viollete wrote:

yes, for example in 2018 1 week would be from 1 January till 4 January, second from 5 to 8 and so on...

 

I looked at INTCK function, however i need to give start and end dates, but I have a variable and i do not  want to look what time period it consists every time i use it.


One hopes that you meant the second week is 5 to 11 or we have another issue involved.

 

Again I ask, HOW are your intending to use this? On what duration of data? Please provide an explicit example of some date values and how you are using "Friday starting week". It likely isn't very difficult but without knowing if what you want is shift for the WEEK function, using a variation of one of the WEEKU, WEEKV or WEEKW formats, or something else it is very difficult to provide an actual solution to a-not-well-defined problem.

PaigeMiller
Diamond | Level 26

INTCK

 

start date is '01JAN18'd

 

end date is the date variable value

 

This gives you the number of weeks since January 1, 2018, and when you do it properly, it counts weeks starting on Friday

--
Paige Miller
ballardw
Super User

@viollete wrote:

 Hello,

 

I have SAS date for various events and for each observation I want to determine which week of the year the event happen. However, i want that the week would start on Friday, not Sunday. How can i do this?

 

Thank you


You should provide some example of how this start of week on Friday is going to be used.

 

For example read the documentation on the WEEK function and the uses of the U, V and W descriptors on determining week number of year values returned to see some things that may need to be considered for your specific usage. For instance what if Friday is in a different calendar year? Is it still the start of your week?

viollete
Calcite | Level 5

I have at least 10 years period and over several millions observation. For each year, I want to find in which week the observation happened. So each year will have 1-52/53 weeks.

if sas date is between 1st January 2018 and 4th January, week=1, if between 5th January 2018 and 11th then week=2

if sa date is between 1st January 2017 and 5th January, week=1, if between 6th January 2017 and 12th then week=2

and so on...

FreelanceReinh
Jade | Level 19

Again:

data want;
set your_dataset;
week=intck('week.6', intnx('year', your_sasdatevar, 0), your_sasdatevar)+1;
run;

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
  • 9 replies
  • 2962 views
  • 0 likes
  • 5 in conversation