Trying to get logic to execute conditionally

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Trying to get logic to execute conditionally

This code works to establish a text variable (‘outfile’) to append yesterday’s date to filenames, and another variable (‘curr_date’) to use in subsequent query where clauses.

  

%let Curr_Date =today()-1;

data _null_;

yesterday = 'Daily_'||put(date()-1,date9.);

call symput('outFile',yesterday);

run;

I tried modifying it as follows so that on Monday the two variables would look back 3 days (to Friday) instead of one day.  It doesn’t work – even on Monday, the first if clause is never satisfied, and it drops to the else and sets the variables back one day instead of three (the variables DO work, it is just that the else loop executes when I want the if loop to execute).  Anyone know how to get the first “if” clause to execute on Mondays?
Thanks!

  

data _null_;

if weekday(date())= 2 then do;

       yesterday = 'Daily_'||put(date()-3,date9.);

       call symput('outFile',yesterday);

       %let Curr_Date = today()-3;

        end;

  else do;

      yesterday = 'Daily_'||put(date()-1,date9.);

       call symput('outFile',yesterday);

       %let Curr_Date = today()-1;

       end;

run;


Accepted Solutions
Solution
‎08-25-2015 03:41 AM
Esteemed Advisor
Posts: 6,661

Re: Trying to get logic to execute conditionally

%let curr_date= is a macro statement that is executed  before the data step is even compiled; since two macro statements are encountered when SAS reads your code, both are executed, and the second assignment overrides the first.

Use a simple assignment operation + call symput instead:

curr_date = today() - 1;

call symput('curr_date',put(curr_date,best5.));

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Grand Advisor
Posts: 17,355

Re: Trying to get logic to execute conditionally

You don't need conditional logic, look into the INTNX function with the WEEKDAY interval.

%let Curr_Date =today()-1;

data _null_;

yesterday = 'Daily_'||put(intnx('weekday', date(), -1),date9.);

call symput('outFile',yesterday);

run;

%put &outfile;

New Contributor
Posts: 4

Re: Trying to get logic to execute conditionally

Thank you Reeza, this helped with the 'outfile' variable, which now steps back one weekday as intended - thank you!

However, the 'curr_date' is still inflexible and always looks to the previous day.  I tried using the intnx function as follows, but it didn't work:

%let Curr_Date =(intnx('weekday', date(), -1),date9.);

I'd like some way to get sas to execute this statement:

%let Curr_Date =(intnx('weekday', date(), -3),date9.);

On Mondays, and this one:

%let Curr_Date =(intnx('weekday', date(), -1),date9.);

Every other day, but am not sure how to do this in open code.  Is there any way to get SAS to execute a block of code based on a condition, and a different block otherwise, in open code, or do I need to use a macro to do this?


Thanks!

Grand Advisor
Posts: 17,355

Re: Trying to get logic to execute conditionally

You need to use the %sysfunc to use a function in macro code as Tom's code illustrates. I highly agree with his suggestion of using the date format YYMMDD.

%let CURR_DATE = %sysfunc(intnx(day,%sysfunc(today()),-1), date9.) ;



Super User
Super User
Posts: 6,347

Re: Trying to get logic to execute conditionally

Use the INTNX() function to calculate relative days.

It can be easier to use the raw number of days formats in the macro variables rather then the user friendly formatted values.

You can make other macro variables that have the formatted values for use in filenames, etc.  I prefer to use YYMMDD format when embedding into filenames since then the names will sort naturally. ('20150101' is before '20150201' while when using DATE9 you get that '01FEB2015' is before '01JAN2015').

You can do it all in macro code if you want, just make sure to use %SYSFUNC() where required.

%let CURR_DATE = %sysfunc(intnx(day,%sysfunc(today()),-1)) ;

%let CURR_DATE_FORMATTED = %sysfunc(putn(&curr_date,yymmddn8)) ;

%let PREV_DATE = %sysfunc(intnx(weekday,&curr_date,-1)) ;

%let PREV_DATE_FORMATTED =  %sysfunc(putn(&prev_date,yymmddn8)) ;

%put _user_;

GLOBAL CURR_DATE 20323

GLOBAL CURR_DATE_FORMATTED 20150823

GLOBAL PREV_DATE 20320

GLOBAL PREV_DATE_FORMATTED 20150820

Respected Advisor
Posts: 4,976

Re: Trying to get logic to execute conditionally

Regardless of the approach you end up taking, there is a key concept you will need to learn.  %LET is never part of a DATA step.  It doesn't matter where you put the %LET statement, it can never be executed by a DATA step DO group or IF/THEN statement.

You might as well have reordered your statements this way, because the result would be the same (and may explain in part why you are getting the results that you see):

%let Curr_Date = today()-3;

%let Curr_Date = today()-1;

data _null_;

if weekday(date())= 2 then do;

       yesterday = 'Daily_'||put(date()-3,date9.);

       call symput('outFile',yesterday);

       end;

else do;

      yesterday = 'Daily_'||put(date()-1,date9.);

       call symput('outFile',yesterday);

       end;

run;

There are other issues, and other pieces to fix, but this is one key concept that you need here.

Good luck.

Solution
‎08-25-2015 03:41 AM
Esteemed Advisor
Posts: 6,661

Re: Trying to get logic to execute conditionally

%let curr_date= is a macro statement that is executed  before the data step is even compiled; since two macro statements are encountered when SAS reads your code, both are executed, and the second assignment overrides the first.

Use a simple assignment operation + call symput instead:

curr_date = today() - 1;

call symput('curr_date',put(curr_date,best5.));

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 4

Re: Trying to get logic to execute conditionally

Thanks community for the inputs - this answer explains why the logical conditions had no impact on which % let statement was being executed, and between the answers from Tom and Reeza above I now have code that works perfectly for every weekday.  Thanks everyone!

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 336 views
  • 7 likes
  • 5 in conversation