DATA Step, Macro, Functions and more

macro loop %if %then not evaluating correctly

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

macro loop %if %then not evaluating correctly

 

This is within a macro function.

 

Example termdate = 01/01/2007

 

%do II = 0 %to 11;
			%let x = %sysfunc(intnx(month,&termdate.,&ii,B),Date9.);
			%let t = %str(%')&x.%str(%')d;
			%put &t.;
			%put ⅈ


					%if %sysevalf(&t.) ge '01FEB2018'd %then %do;

the %put &t. produces:

'01JAN2007'd

 

but then when I use it in the %if statement below

it evaluates to TRUE when using Mlogic option. 

It should be False.

 

This is how it should look when the macro variable has been resolved.

 

%if '01JAN2007'd ge '01FEB2018'd %then %do;

Should be false but it comes out true.

 


Accepted Solutions
Solution
‎02-07-2018 10:17 AM
PROC Star
Posts: 1,817

Re: macro loop %if %then not evaluating correctly

try :

%if %sysevalf(&t. ge '01FEB2018'd) %then %do;

View solution in original post


All Replies
Solution
‎02-07-2018 10:17 AM
PROC Star
Posts: 1,817

Re: macro loop %if %then not evaluating correctly

try :

%if %sysevalf(&t. ge '01FEB2018'd) %then %do;
Super User
Posts: 6,781

Re: macro loop %if %then not evaluating correctly

Macro language doesn't understand dates.  It understands character strings.  If you want to force it to make the "right" comparison, you can do it with:

 

%if %sysevalf(&t.) ge %sysevalf('01FEB2018'd) %then %do;

Super User
Posts: 23,752

Re: macro loop %if %then not evaluating correctly

Is that all your code? If so, you should use a data step and data step loop, not a macro loop. 

 


mhodge20 wrote:

 

This is within a macro function.

 

Example termdate = 01/01/2007

 

%do II = 0 %to 11;
			%let x = %sysfunc(intnx(month,&termdate.,&ii,B),Date9.);
			%let t = %str(%')&x.%str(%')d;
			%put &t.;
			%put ⅈ


					%if %sysevalf(&t.) ge '01FEB2018'd %then %do;

the %put &t. produces:

'01JAN2007'd

 

but then when I use it in the %if statement below

it evaluates to TRUE when using Mlogic option. 

It should be False.

 

This is how it should look when the macro variable has been resolved.

 

%if '01JAN2007'd ge '01FEB2018'd %then %do;

Should be false but it comes out true.

 


 

Super User
Posts: 13,563

Re: macro loop %if %then not evaluating correctly


mhodge20 wrote:

 

This is within a macro function.

 

Example termdate = 01/01/2007

 

the %put &t. produces:

'01JAN2007'd

 

but then when I use it in the %if statement below

it evaluates to TRUE when using Mlogic option. 

It should be False.

 

This is how it should look when the macro variable has been resolved.

 

%if '01JAN2007'd ge '01FEB2018'd %then %do;

Should be false but it comes out true.

 


No it shouldn't. Strings are compared one character at a time until a difference (if any) is found: 0=0, 1=1 but J>F.

for added fun check on this

%if '01JAN2007'd ge '01feb2018'd %then %do;

Super User
Super User
Posts: 8,115

Re: macro loop %if %then not evaluating correctly

[ Edited ]

The condition in a %IF is normally evaluated using the %EVAL() macro function.  That function does not handle floating point numbers or literals.  So you need to generate your own test using the %SYSEVALF() macro function instead if you are comparing date literals.

 

Also you need to convert your date like string into a date value, either an actual number of days or a date literal, before you can compare dates.

%let datenumber = %sysfunc(intnx(month,&termdate.,&ii,B));
%let dateliteral = "%sysfunc(intnx(month,&termdate.,&ii,B),Date9.)"d;
%if %sysevalf(&datenumber. ge '01FEB2018'd) %then %do;
%if %sysevalf(&dateliteral. ge '01FEB2018'd) %then %do;

 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 108 views
  • 0 likes
  • 6 in conversation