Desktop productivity for business analysts and programmers

return yesterdays date except on a Monday return the Friday date

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 129
Accepted Solution

return yesterdays date except on a Monday return the Friday date

Hi All,

 

I use the following code to select the previous days date.

 

 

%let DateVAR =  %sysfunc(intnx(day,%sysfunc(today()),-1),yymmddn8.);

I need to now select the previous days date for a report except when it is run on a Monday I need it to return the Fridays date.

 

So if the report is run on Monday 9 July 2018 the date returned will be 6 July 2018.

 

How do I change this code to do this or is there different code I will need to use.

 

Cheers

 

Dean


Accepted Solutions
Solution
‎07-11-2018 07:07 PM
Super User
Posts: 6,899

Re: return yesterdays date except on a Monday return the Friday date

[ Edited ]

So when the program runs on Sunday, you still want to return Saturday's date (not Friday)?

 

It's much easier to follow if you use a DATA step and get rid of %SYSFUNC.  It could look like this:

 

data _null_;

datevar = today() - 1;

if weekday(datevar) = 1 then datevar = datevar - 2;

call symputx('datevar', put(datevar, yymmddn8.));

run;

View solution in original post


All Replies
Super User
Posts: 23,928

Re: return yesterdays date except on a Monday return the Friday date

weekday rather than day as the interval?

Or I guess if it's only Monday, use an IFN/IFC inside it?

Frequent Contributor
Posts: 129

Re: return yesterdays date except on a Monday return the Friday date

Hi @Reeza,

 

I have never used IFC/IFN - can you show me an example or direct me to some help please.

 

Ultimately I need to populate a Where statement like below.

 

 

	WHERE Date = "&DateVAR."d

Thanks for your help.

 

Cheers

 

Dean

 

 

Super User
Posts: 2,037

Re: return yesterdays date except on a Monday return the Friday date

You can play with something like this

 

 

%let DateVAR = %sysfunc(today(),downame.);
%put &datevar;
%if &DateVAR=Monday %then .............more statements...;

Solution
‎07-11-2018 07:07 PM
Super User
Posts: 6,899

Re: return yesterdays date except on a Monday return the Friday date

[ Edited ]

So when the program runs on Sunday, you still want to return Saturday's date (not Friday)?

 

It's much easier to follow if you use a DATA step and get rid of %SYSFUNC.  It could look like this:

 

data _null_;

datevar = today() - 1;

if weekday(datevar) = 1 then datevar = datevar - 2;

call symputx('datevar', put(datevar, yymmddn8.));

run;

Frequent Contributor
Posts: 129

Re: return yesterdays date except on a Monday return the Friday date

Posted in reply to Astounding

Hi @Astounding,

 

The report is for weekdays only so don't want to return Saturdays or Sundays - am I right if I change the code to the below to remove Saturdays as well?

 

 

data _null_;
datevar = today() - 1;
if weekday(datevar) = 1 then datevar = datevar - 2;
else if weekday(datevar) = 7 then datevar = datevar - 1;
call symputx('datevar', put(datevar, yymmddn8.));
run;

 

Cheers

 

Dean

Super User
Posts: 6,899

Re: return yesterdays date except on a Monday return the Friday date

You get 100%.

Occasional Contributor
Posts: 17

Re: return yesterdays date except on a Monday return the Friday date

You can try this:

 

%macro dtx;

%global rptdt;

%if %eval(%sysfunc(weekday(%sysfunc(today()))))=2 %then %let rptdt = %sysfunc(intnx(weekday, %sysfunc(today()), -1), mmddyy10.);

%else %let rptdt = %sysfunc(intnx(day, %sysfunc(today()), -1), mmddyy10.);

%mend dtx;

 

%dtx;

%put &rptdt;

 

Occasional Contributor
Posts: 17

Re: return yesterdays date except on a Monday return the Friday date

I just saw that you don't want Sat & Sun. Then this all you need:
%let rptdt = %sysfunc(intnx(weekday, %sysfunc(today()), -1), mmddyy10.);
Super User
Posts: 23,928

Re: return yesterdays date except on a Monday return the Friday date

@EEng Yes, if s/he wants only weekdays then the weekday is a better approach. It was the first suggestion s/he received as well, so I'm guessing that's not what they want. 

Frequent Contributor
Posts: 129

Re: return yesterdays date except on a Monday return the Friday date

Hi @Reeza,

 

Apologies as I didn't read your response properly  - I thought that was a question - now I know what you meant, replace day with weekday.

 

I should have known better as your responses to queries are always very helpful.

 

Regards

 

Dean 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 248 views
  • 3 likes
  • 5 in conversation