Help using Base SAS procedures

Comparison between two values are Giving wrong results in %If

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Comparison between two values are Giving wrong results in %If

Hi All,

 

I am writing the below %If else statement:

%if (%sysfunc(abs(%sysevalf((47496-49030.8)/49030.8)*100)) gt 12)then %do;

      %Put I am in the Loop;

%End;

In log it is showing that:

MLOGIC(S1): %IF condition
(%sysfunc(abs(%sysevalf((47496-49030.8)/49030.8)*100)) gt 12)
is TRUE

Whereas if I check the value of the calculation in the below chunk:

 

%let a= %sysfunc(abs(%sysevalf((47496-49030.8)/49030.8)*100));

%Put &a.;

 

Then in the Log it is showing:

 

SYMBOLGEN: Macro variable A resolves
to 3.130277295087
3.130277295087

So, now my question is how can 3.130277295087 be greater than 12?

I believe this has to do something with how SAS stores the data internally in hexadecimal form. But I am not very sure.

Can anybody shed some light on this one?

 

Note: If I put a round function in the %If statement, then the result comes out as expected (i.e.False).

Thanks all in advance for your help.

Regards,

Saugata Mukherjee.


Accepted Solutions
Solution
‎04-23-2014 06:24 AM
Super User
Posts: 6,928

Re: Comparison between two values are Giving wrong results in %If

Have a look at this:

SAS(R) 9.2 Macro Language: Reference

Right in the middle you have an example of how the %sysevalf function makes it possible.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,928

Re: Comparison between two values are Giving wrong results in %If

Without the round function, you have a period in the number, which causes the macro processor to treat the numbers as character strings and do a string comparison, which yields the undesired result.

See the documentation of the %sysevalf function, which will help here.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 35

Re: Comparison between two values are Giving wrong results in %If

Hi Kurt,

So, does that mean that it is not possible to do floating point comparison in %If statement?

Regards,

Saugata Mukherjee.

Solution
‎04-23-2014 06:24 AM
Super User
Posts: 6,928

Re: Comparison between two values are Giving wrong results in %If

Have a look at this:

SAS(R) 9.2 Macro Language: Reference

Right in the middle you have an example of how the %sysevalf function makes it possible.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 35

Re: Comparison between two values are Giving wrong results in %If

Thanks a lot Kurt. Now I have a better understanding of this :smileylaugh:

Contributor SKK
Contributor
Posts: 35

Re: Comparison between two values are Giving wrong results in %If

Try this:

%if %sysevalf((%sysfunc(abs(%sysevalf((47496-49030.8)/49030.8)*100))) gt 12) %then %do;

      %Put I am in the Loop;
%End;

Contributor
Posts: 35

Re: Comparison between two values are Giving wrong results in %If

Thank youSmiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 312 views
  • 6 likes
  • 3 in conversation