DATA Step, Macro, Functions and more

%If statement using dates

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

%If statement using dates

[ Edited ]

Hi there!

 

I'm stuck with the following code (simplified version): 

 

%macro testing(data);

%IF &data. <= '31MAY2014'd %THEN %DO;
%put 1;
%IF '31MAY2014'd  < &data. <= '31DEC2014'd %THEN %DO;
%put 2;
%IF '31DEC2014'd  < &data. <= '30APR2016'd %THEN %DO;
%put 3;

%mend;

%testing('30JUN2015'd);

Ideally, the code should print a '3' in the log, but it prints a 1. 

 

I have tried with datepart() and different time formats. I need to run different macros depending on the date, but I'm not able to evaluate whether a date is within a range. 

 

Could someone help?

 

Thanks in advance. 

 


Accepted Solutions
Solution
‎03-27-2018 03:26 PM
Super User
Posts: 6,908

Re: %If statement using dates

You're three steps away from having working code.

 

First, macro language doesn't compare dates.  At least not by default.  It compares character strings.

 

Second, a series of comparisons doesn't work the same way in macro language as it does in a DATA step.

 

Third, you need an %END statement for each %DO.  (Those probably got dropped when simplifying the example.)

 

All of that can be fixed.  Here's the sort of statements that would work:

 

%if %sysevalf(&data. <= '31MAY2014'd) %then %put 1;

 

%else %if %sysevalf('31MAY2014'd < &data) and %sysevalf(&data <= '31DEC2014'd) %then %put 2;

View solution in original post


All Replies
Solution
‎03-27-2018 03:26 PM
Super User
Posts: 6,908

Re: %If statement using dates

You're three steps away from having working code.

 

First, macro language doesn't compare dates.  At least not by default.  It compares character strings.

 

Second, a series of comparisons doesn't work the same way in macro language as it does in a DATA step.

 

Third, you need an %END statement for each %DO.  (Those probably got dropped when simplifying the example.)

 

All of that can be fixed.  Here's the sort of statements that would work:

 

%if %sysevalf(&data. <= '31MAY2014'd) %then %put 1;

 

%else %if %sysevalf('31MAY2014'd < &data) and %sysevalf(&data <= '31DEC2014'd) %then %put 2;

New Contributor
Posts: 3

Re: %If statement using dates

Posted in reply to Astounding

It worked! Thank you very much!

 

 

Super User
Super User
Posts: 8,264

Re: %If statement using dates

By default SAS uses the %EVAL() macro function in %IF, %WHILE and %UNTIL clauses.  That function can only handle integer comparisons.  It does not handle date literals (or floating point arithmetic).

 

 

Use %SYSEVALF() to evaluate values that use date literals.

 

Also macro code will not convert A<B<C into (A<B) and (B<C) like SAS code will.  Instead if will evaluate A<B to either 0 or 1 and then test if that is <C. 

 

New Contributor
Posts: 3

Re: %If statement using dates

Thanks a lot!! It worked with the %SYSEVALF()
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 194 views
  • 0 likes
  • 3 in conversation