DATA Step, Macro, Functions and more

Date Variable - If statement for weekend adjustment

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

Date Variable - If statement for weekend adjustment

I have my date variable set as:

%let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(day,%sysfunc(today()) ,-1)),yymmdd10.))%str(%');

Which is serving it's purpose to create a variable "DATE" that I use in the query to always check for new activity from yesterday. However on Mondays, I need to check for new activity over the whole weekend. So it needs to be

%let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(day,%sysfunc(today()) ,-3)),yymmdd10.))%str(%');

(Which on Monday should return Friday's date.)

Currently it's just making that minor manual adjustment on Mondays, but it seems like I should be able to have the adjustment occur automatically with an IF statement.

But I'm not sure how to set the IF statement so it recognizes to only use the second one on Mondays and on Tuesday through Friday to use the first.

(That is my main concern to avoid needing to manually adjust it every Monday, but should someone have a trick to have it make a similar adjustment for holidays to eliminate that manual adjustment, that would be awesome.)


Accepted Solutions
Solution
‎12-29-2014 04:12 PM
Super User
Posts: 19,869

Re: Date Variable - If statement for weekend adjustment

Posted in reply to EarlyCode

I was thinking of replacing DAY with WEEKDAY

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition


%let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(DAY,%sysfunc(today()) ,-1)),yymmdd10.))%str(%');

%put &date.;


VS


%let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(WEEKDAY,%sysfunc(today()) ,-1)),yymmdd10.))%str(%');

%put &date.;


72   %let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(DAY,%sysfunc(today())

72 ! ,-1)),yymmdd10.))%str(%');

73   %put &date.;

'2014-12-28'

74

75   %let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(WEEKDAY,%sysfunc(today())

75 ! ,-1)),yymmdd10.))%str(%');

76   %put &date.;

'2014-12-26'

View solution in original post


All Replies
Super User
Posts: 19,869

Re: Date Variable - If statement for weekend adjustment

Posted in reply to EarlyCode

Look at the weekday interval for intnx function instead of day Smiley Happy

Contributor
Posts: 30

Re: Date Variable - If statement for weekend adjustment

%LET DAY=%SYSFUNC(putn( %sysfunc(today()) ,WEEKDAY.));

%PUT DAY=&DAY;

%MACRO Monday_Check;

%IF &day=2

       %THEN %LET DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(day,%sysfunc(today()) ,-1)),yymmdd10.))%str(%');

       %ELSE %LET DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(day,%sysfunc(today()) ,-3)),yymmdd10.))%str(%');

Run;

%Mend Monday_Check;

%Monday_Check;

%PUT Yesterday=  &DATE  Day= &Day;

I'm missing something, I'm not sure if it's in the macro statement or the IF statement. But it's not running correctly.

Solution
‎12-29-2014 04:12 PM
Super User
Posts: 19,869

Re: Date Variable - If statement for weekend adjustment

Posted in reply to EarlyCode

I was thinking of replacing DAY with WEEKDAY

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition


%let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(DAY,%sysfunc(today()) ,-1)),yymmdd10.))%str(%');

%put &date.;


VS


%let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(WEEKDAY,%sysfunc(today()) ,-1)),yymmdd10.))%str(%');

%put &date.;


72   %let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(DAY,%sysfunc(today())

72 ! ,-1)),yymmdd10.))%str(%');

73   %put &date.;

'2014-12-28'

74

75   %let DATE=%str(%')%sysfunc( putn( %sysfunc(intnx(WEEKDAY,%sysfunc(today())

75 ! ,-1)),yymmdd10.))%str(%');

76   %put &date.;

'2014-12-26'

Contributor
Posts: 30

Re: Date Variable - If statement for weekend adjustment

So by obsessing over the idea of a if statement I was making it more difficult on myself then it needed to be...

Sounds about right for me... :smileyblush:

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 801 views
  • 3 likes
  • 2 in conversation