DATA Step, Macro, Functions and more

Get previous day of week?

Reply
Contributor
Posts: 53

Get previous day of week?

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!
Super Contributor
Super Contributor
Posts: 3,174

Re: Get previous day of week?

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
Regular Contributor
Posts: 171

Re: Get previous day of week?

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;
Respected Advisor
Posts: 3,799

Re: Get previous day of week?

What answer do you expect tomorrow Wednesday Sep 22,2010?
Contributor
Posts: 53

Re: Get previous day of week?

Posted in reply to data_null__
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.));
Respected Advisor
Posts: 3,799

Re: Get previous day of week?

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]
Contributor
Posts: 53

Re: Get previous day of week?

Posted in reply to data_null__
ohhh much better! thanks for the tip!!
Super User
Posts: 10,044

Re: Get previous day of week?

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 Employee
Posts: 58

Re: Get previous day of week?

Ask a Question
Discussion stats
  • 8 replies
  • 1176 views
  • 0 likes
  • 6 in conversation