BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Zatere
Quartz | Level 8

Hello,

 

The below conditional logic fails and cannot figure out the reason:

 

%let _N_ = 6.3;

%macro test();
	%if &_N_ < 100 %then
		%do;
			%put &_N_ is less than 100;
		%end;
	%else
		%do;
			%put &_N_ is greater than 100;
		%end;
%mend;

%test();

 

Please see the logs:

 

28         %let _N_ = 6.3;
29         
30         %macro test();
31         	%if &_N_ < 100 %then
32         		%do;
33         			%put &_N_ is less than 100;
34         		%end;
35         	%else
36         		%do;
37         			%put &_N_ is greater than 100;
38         		%end;
39         %mend;
40         
41         %test();
6.3 is greater than 100

 

I have a workaround:

 

%let _N_ = 6.3;

%macro test2();
	%if %sysevalf(&_N_,integer) < 100 %then
		%do;
			%put &_N_ is less than 100;
		%end;
	%else
		%do;
			%put &_N_ is greater than 100;
		%end;
%mend;

%test2();

The logs:

 

28         %let _N_ = 6.3;
29         
30         %macro test2();
31         	%if %sysevalf(&_N_,integer) < 100 %then
32         		%do;
33         			%put &_N_ is less than 100;
34         		%end;
35         	%else
36         		%do;
37         			%put &_N_ is greater than 100;
38         		%end;
39         %mend;
40         
41         %test2();
6.3 is less than 100

But still I would like to understand the reason..

 

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @Zatere,

 

As is described in How the Macro Processor Evaluates Arithmetic Expressions, the period in "6.3" indicates to the macro processor that this string is to be treated as text. As such it is alphabetically greater than the string "100" (or, e.g., than the string "5999999999"). Without a non-digit character the comparison would be evaluated using integer arithmetic, i.e., 63 < 100 would correctly be true.

 

The %SYSEVALF function allows for an evaluation using floating-point arithmetic. If this is what you're after, you should put the entire logical expression &_N_ < 100 into the argument of %SYSEVALF. Thus it would be evaluated as desired even if 100 was replaced by a non-integer value. For example, %sysevalf(6.3 < 6.4) would be true and %sysevalf(6.3 < 6.1) false, whereas %sysevalf(6.3, integer)=6 and hence is less than 6.1 for the macro processor (string comparison!).

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

The documentation for %SYSEVALF explains:

 

Evaluates arithmetic and logical expressions using floating-point arithmetic.

 

So you can't use non-integer arithmetic on macro variables, unless you use %SYSEVALF

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @Zatere,

 

As is described in How the Macro Processor Evaluates Arithmetic Expressions, the period in "6.3" indicates to the macro processor that this string is to be treated as text. As such it is alphabetically greater than the string "100" (or, e.g., than the string "5999999999"). Without a non-digit character the comparison would be evaluated using integer arithmetic, i.e., 63 < 100 would correctly be true.

 

The %SYSEVALF function allows for an evaluation using floating-point arithmetic. If this is what you're after, you should put the entire logical expression &_N_ < 100 into the argument of %SYSEVALF. Thus it would be evaluated as desired even if 100 was replaced by a non-integer value. For example, %sysevalf(6.3 < 6.4) would be true and %sysevalf(6.3 < 6.1) false, whereas %sysevalf(6.3, integer)=6 and hence is less than 6.1 for the macro processor (string comparison!).

Zatere
Quartz | Level 8
Thanks, the :%sysevalf(&_N_ < 100) does the job

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 447 views
  • 1 like
  • 3 in conversation