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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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