My %IF macro is not working. .1 should be > 0 but in this macro it executes the %ELSE conditional. If I Enter 1, the %IF condition is excuted. The problem is that .1 is GT 0. It if late, but the message "%IF condition .1 > 0 is FALSE" doesn't make sense to these tired eyes.
%macro ApplyTax(Tax,TRate,AnnualBenefit);
%if .1 > 0 %then put "Is Greater";
%else put "is less than";
;
%if &TRate> 0 %then put "Is Greater";
%else put "is less than";
;
%mend;
Data DS;
%ApplyTax(dsvar,.1,1000);
run;
Here is the MPRINT;
It is doing what you asked it to do. The ASCII code for a period is less than the code for the zero character.
2 data _null_; 3 a='.1'; 4 b='0'; 5 put (a b) (= $hex.); 6 run; a=2E31 b=30
If you want to try to compare floating point values in macro code you need to use %SYSEVALF()
%if %sysevalf( .1 > 0) ...
It is doing what you asked it to do. The ASCII code for a period is less than the code for the zero character.
2 data _null_; 3 a='.1'; 4 b='0'; 5 put (a b) (= $hex.); 6 run; a=2E31 b=30
If you want to try to compare floating point values in macro code you need to use %SYSEVALF()
%if %sysevalf( .1 > 0) ...
"If you want to try to compare floating point values" - Or of course, do data processing in datastep where it belongs, where the data types are present for such data and where functions are there to perform such processing.
Automatic arithmetic evaluation in a %if condition will only be done when integers are used. In all other cases, you need to assist the macro compiler as @Tom suggested.
(this works as intended)
%if 23 > 3 %then %do; %put yes; %end;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.