BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DME790
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

10 REPLIES 10
Reeza
Super User

weekday rather than day as the interval?

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

DME790
Pyrite | Level 9

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

 

 

novinosrin
Tourmaline | Level 20

You can play with something like this

 

 

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

Astounding
PROC Star

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;

DME790
Pyrite | Level 9

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

EEng
Obsidian | Level 7

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;

 

EEng
Obsidian | Level 7
I just saw that you don't want Sat & Sun. Then this all you need:
%let rptdt = %sysfunc(intnx(weekday, %sysfunc(today()), -1), mmddyy10.);
Reeza
Super User

@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. 

DME790
Pyrite | Level 9

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 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 10 replies
  • 2107 views
  • 3 likes
  • 5 in conversation