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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1909 views
  • 2 likes
  • 3 in conversation