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.
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!).
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
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!).
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!
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.