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

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;

 

151 Data DS;
152 %ApplyTax(dsvar,.1,1000);
MLOGIC(APPLYTAX): Beginning execution.
MLOGIC(APPLYTAX): Parameter TAX has value dsvar
MLOGIC(APPLYTAX): Parameter TRATE has value .1
MLOGIC(APPLYTAX): Parameter ANNUALBENEFIT has value 1000
MLOGIC(APPLYTAX): %IF condition .1 > 0 is FALSE
MPRINT(APPLYTAX): put "is less than" ;
SYMBOLGEN: Macro variable TRATE resolves to .1
MLOGIC(APPLYTAX): %IF condition &TRate> 0 is FALSE
MPRINT(APPLYTAX): put "is less than" ;
MLOGIC(APPLYTAX): Ending execution.
153 run;
 
 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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) ...
RW9
Diamond | Level 26 RW9
Diamond | Level 26

"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.

BrentonCSmith
Fluorite | Level 6
thanks!
Kurt_Bremser
Super User

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;

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
  • 4 replies
  • 2355 views
  • 3 likes
  • 4 in conversation