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

Hi,
Is there a way to pick a specific date of the given week depending on the date.

If I have records as:

Name Date
AD Sept 11
DF Sept 16

I want to pick be able to pick the Wednesday of the week and report it. Output would look like:

Name   Date       Week_date
AD       Sept 12        Sep 11
DF        Sept 16        Sep 18

I know i can just define the ranges in a data step but I wanted to know if there way a different way to do this... The week would be Sun - Sat

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

The weekday() function yields a number between 1 and 7 that specifies the day in the week (1 = Sunday, 7 = Saturday).

This can be used in a rather simple formula:

data want;
format date wednesday weekdatx.;
do date = today() - 7 to today() + 7;
  wednesday = date + 4 - weekday(date);
  output;
end;
run;

proc print data=want noobs;
run;

Result:

date	                        wednesday
Wednesday, 18 September 2019	Wednesday, 18 September 2019
Thursday, 19 September 2019	Wednesday, 18 September 2019
Friday, 20 September 2019	Wednesday, 18 September 2019
Saturday, 21 September 2019	Wednesday, 18 September 2019
Sunday, 22 September 2019	Wednesday, 25 September 2019
Monday, 23 September 2019	Wednesday, 25 September 2019
Tuesday, 24 September 2019	Wednesday, 25 September 2019
Wednesday, 25 September 2019	Wednesday, 25 September 2019
Thursday, 26 September 2019	Wednesday, 25 September 2019
Friday, 27 September 2019	Wednesday, 25 September 2019
Saturday, 28 September 2019	Wednesday, 25 September 2019
Sunday, 29 September 2019	Wednesday, 2 October 2019
Monday, 30 September 2019	Wednesday, 2 October 2019
Tuesday, 1 October 2019	        Wednesday, 2 October 2019
Wednesday, 2 October 2019	Wednesday, 2 October 2019

 

View solution in original post

6 REPLIES 6
TheNovice
Quartz | Level 8

Sorry for the shoddy post Kurt...

 

Yes I will have the full date with year in there.

Kurt_Bremser
Super User

The weekday() function yields a number between 1 and 7 that specifies the day in the week (1 = Sunday, 7 = Saturday).

This can be used in a rather simple formula:

data want;
format date wednesday weekdatx.;
do date = today() - 7 to today() + 7;
  wednesday = date + 4 - weekday(date);
  output;
end;
run;

proc print data=want noobs;
run;

Result:

date	                        wednesday
Wednesday, 18 September 2019	Wednesday, 18 September 2019
Thursday, 19 September 2019	Wednesday, 18 September 2019
Friday, 20 September 2019	Wednesday, 18 September 2019
Saturday, 21 September 2019	Wednesday, 18 September 2019
Sunday, 22 September 2019	Wednesday, 25 September 2019
Monday, 23 September 2019	Wednesday, 25 September 2019
Tuesday, 24 September 2019	Wednesday, 25 September 2019
Wednesday, 25 September 2019	Wednesday, 25 September 2019
Thursday, 26 September 2019	Wednesday, 25 September 2019
Friday, 27 September 2019	Wednesday, 25 September 2019
Saturday, 28 September 2019	Wednesday, 25 September 2019
Sunday, 29 September 2019	Wednesday, 2 October 2019
Monday, 30 September 2019	Wednesday, 2 October 2019
Tuesday, 1 October 2019	        Wednesday, 2 October 2019
Wednesday, 2 October 2019	Wednesday, 2 October 2019

 

Reeza
Super User

I'd recommend INTNX() instead.

 

wednesday = intnx('week', date, 0, 'b') + 3;

The third parameter, the interval, is set to 0 to stay on the same week. 

The 'b' aligns it to the beginning of the week, Sunday. 

+3 adds 3 days to get it to Wednesday. 

 

This will give the Wednesday of the week, regardless if it's before or after the date.

TheNovice
Quartz | Level 8

Thanks Reeza, I will try this today. Apologies for the delay. Got pulled onto another task.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 1723 views
  • 2 likes
  • 3 in conversation