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

Hello

For each date in the data set I want to :

1- Last Wednesday

2-Last Thursday (that is before Last Wednesday)

For example:

For 15JAN2023 (Sunday) last Wednesday was 11JAN2023 and last Thursday(before last Wednesday) was 05JAN2023

 

What is the way to calculate it and add 2 desired columns :

Last_WED

LAST_Thu_Before_Last_WED

 

 

 

data have;
format date ddmmyy10.;
input date : date9.;
cards;
15JAN2023
27MAR2023
01JAN2023
19NOV2023
;
Run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Last Thursday before last Wednesday is easy. Once you find last Wednesday, then you subtract 6.

 

To get last Wednesday, the INTNX function will do this, if you ask for the beginning of a week that contains a specific date using the WEEK.4 interval. (the 4 indicates Wednesday)

--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Last Thursday before last Wednesday is easy. Once you find last Wednesday, then you subtract 6.

 

To get last Wednesday, the INTNX function will do this, if you ask for the beginning of a week that contains a specific date using the WEEK.4 interval. (the 4 indicates Wednesday)

--
Paige Miller
Patrick
Opal | Level 21

One way to go:

data have;
  format date lastWedDt lastThuDt weekdatx.;
  input date : date9.;
  lastWedDt=intnx('week.4',date,0,'b');
  lastThuDt=intnx('week.5',lastWedDt,0,'b');
  cards;
15JAN2023
27MAR2023
01JAN2023
19NOV2023
;

proc print data=have;
run;

Patrick_0-1704200253385.png

 

 

Ronein
Meteorite | Level 14

Thanks,

However for 15JAN2023 I got last WED 10.01.2023  but it should be 11.01.2023

 

data want;
set have;
last_WED=Intnx('week.4',date,-1,'end');
Last_Thu_Before_Wed=last_WED-6;
last_WED_WeekDay=Weekday(last_WED);
Last_Thu_Before_Wed_WeekDay=Weekday(Last_Thu_Before_Wed);
format last_WED Last_Thu_Before_Wed  ddmmyy10.;
Run;
 

 

Patrick
Opal | Level 21

@Ronein If you look at the code I've posted and the result it returned then you'll see that I've used the week.4 interval same as @PaigeMiller proposed.

If you get another result with this interval then you need to share your code.

Ronein
Meteorite | Level 14

It is working 100%

data want;
set have;
last_WED=intnx('week.4',date,0,'b');
Last_Thu_Before_Wed=last_WED-6;
last_WED_WeekDay=Weekday(last_WED);
Last_Thu_Before_Wed_WeekDay=Weekday(Last_Thu_Before_Wed);
format last_WED Last_Thu_Before_Wed  ddmmyy10.;
Run;
PaigeMiller
Diamond | Level 26
last_WED=Intnx('week.4',date,-1,'end');

The -1 indicates you want the last Wednesday of the previous week, which is not what you asked for. How could you change this to get the last Wednesday of the current week?

--
Paige Miller
Ronein
Meteorite | Level 14
Great,
Use 0 and beginning
last_WED=intnx('week.4',date,0,'b');

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 1494 views
  • 5 likes
  • 3 in conversation