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

 

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

try :

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

View solution in original post

5 REPLIES 5
novinosrin
Tourmaline | Level 20

try :

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

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;

Reeza
Super User

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.

 


 

ballardw
Super User

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

Tom
Super User Tom
Super User

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;

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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