BookmarkSubscribeRSS Feed
FrankE
Fluorite | Level 6
Hi guys,

How would I do the date math to get last Wednesday based on today's date?

I know it's going to use INTNX somehow but I can't seem to think it through..

Thanks!
8 REPLIES 8
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
SAS numeric date variable is integer value, representing days since 1/1/1960. So if you use INTNX to get back to the start of the prior week (Sunday) and then count forward +3, you will have the Wednesday date.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

documentation date introduction site:sas.com

intnx site:sas.com
polingjw
Quartz | Level 8
Since you are looking for a Wednesday, you could also use 'middle' as the alignment parameter instead of incrementing by three.

data _null_;
LastWednesday = intnx('week', today(), 0, 'middle');
if LastWednesday >= today() then LastWednesday + -7;
put LastWednesday = weekdate.;
run;
data_null__
Jade | Level 19
What answer do you expect tomorrow Wednesday Sep 22,2010?
FrankE
Fluorite | Level 6
Hey guys,

thanks for all your help.. The actual requirement was to come up with a start date of 2 Saturdays ago and and end date of the previous Saturday. Both in DATE9. format. I needed to get them into macro vars so I used %let instead:

%let start_date = %sysfunc(putn(%eval(%sysfunc(intnx(week, "&SYSDATE"D, -1))-1),DATE9.));
%let end_date = %sysfunc(putn(%eval(%sysfunc(intnx(week, "&SYSDATE"D, 0))-1),DATE9.));
data_null__
Jade | Level 19
I think you can simplify this a bit with a shift index and SYSFUNC's little known format parameter.


INTNX(interval<.shift-index>, start-from, increment<, 'alignment'>)

[pre]
5006 %let start_date = %sysfunc(putn(%eval(%sysfunc(intnx(week, "&SYSDATE"D, -1))-1),DATE9.));
5007 %let end_date = %sysfunc(putn(%eval(%sysfunc(intnx(week, "&SYSDATE"D, 0))-1),DATE9.));
5008
5009
5010 %put &START_DATE &END_DATE;
11SEP2010 18SEP2010
5011
5012
5013 %let start_date = %sysfunc(intnx(week.7, "&SYSDATE"D, -1),date9.);
5014 %let end_date = %sysfunc(intnx(week.7, "&SYSDATE"D, 0),date9.);
5015
5016
5017 %put &START_DATE &END_DATE;
11SEP2010 18SEP2010
[/pre]
FrankE
Fluorite | Level 6
ohhh much better! thanks for the tip!!
Ksharp
Super User
Or another choice.

[pre]
options mprint mlogic symbolgen;
%let start_date = %sysfunc(putn(%eval(%sysfunc(intnx(week, %sysfunc(today()), -1))-1),DATE9.));
%let end_date = %sysfunc(putn(%eval(%sysfunc(intnx(week, %sysfunc(today()), 0))-1),DATE9.));
%put _user_;
[/pre]



Ksharp

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 4368 views
  • 0 likes
  • 6 in conversation