Comparing decimal values in sas macros

Reply
Contributor
Posts: 56

Comparing decimal values in sas macros

[ Edited ]

Can anyone please tell why sas behaves differently in below code ?I compare macro variable and results are diffrent each time .

once I use &x and another time "&x." .

 

%macro test;
%let x=0.0259698745;

%if &x. < .05 %then %do;
%put Less than ;
%end;

%else %if &x. >= .05 %then %do;
%put greater than;

%end;

%if "&x." < .05 %then %do;
%put Less than ;
%end;

%else %if "&x." >= .05 %then %do;
%put greater than;

%end;

%mend test;
%test;

 

_______________Log __________

MLOGIC(TEST): Beginning execution.
MLOGIC(TEST): %LET (variable name is X)
SYMBOLGEN: Macro variable X resolves to 0.0259698745
MLOGIC(TEST): %IF condition &x. < .05 is FALSE
SYMBOLGEN: Macro variable X resolves to 0.0259698745
MLOGIC(TEST): %IF condition &x. >= .05 is TRUE
MLOGIC(TEST): %PUT greater than
greater than
SYMBOLGEN: Macro variable X resolves to 0.0259698745
MLOGIC(TEST): %IF condition "&x." < .05 is TRUE
MLOGIC(TEST): %PUT Less than
Less than
MLOGIC(TEST): Ending execution.

 

Super User
Posts: 19,167

Re: Comparing decimal values in sas macros

When doing comparisons with macro variables and decimals you need to use %sysevalf, otherwise you're getting comparisons done alphabetically.

 

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000206831.htm

Super User
Posts: 11,134

Re: Comparing decimal values in sas macros

The macro language is basically a TEXT processor. So a simple comparison uses text rules. Since your second value started with the decimal then you get unexpected result. See for non-macro similar result.

data _null_;
   x= '0.025';
   y= '.05';
   if x < y then put "x is < y";
   else put "x is not < y";
   z= '0.5';
   if x < z then put "x is < z";
   else put "x is not < z";
run;

Dealing with decimal values you'll want to use something like %if %sysevalf(&x < .5) %then %do...

 

Super User
Super User
Posts: 7,720

Re: Comparing decimal values in sas macros

This is down to those elements you have described being text rather than numbers and it doing text comparison and implicit conversions.  To clarify, macro language is a text generation tool.  It is there to avoid the need to type code over and over again.  It is Not a programming language.  Base SAS is a programming language.  Its function is to process and manipulate data.  It has structures setup to handle numbers, text and other forms of data, macro language is not.  

Why would you need to do this?  I cannot think of any situation where that code cannot be simplified, and processed correctly with:

%let x=0.0259698745;
data _null_;
  if &x. < 0.05 then put "Less Than";
  else put "Greater Than";
run;

 

Super User
Posts: 5,369

Re: Comparing decimal values in sas macros

Even a decimal point is considered a character in macro language:

 

%if 2. ne 2 %then %put NOT EQUAL !;

 

The decimal point causes a character comparison, so these strings are not equal.  Here's how you might use %sysevalf:

 

%if %sysevalf(&x < 0.05) %then %do;

 

That's the function that can evaluate decimal fractions in macro language.

Ask a Question
Discussion stats
  • 4 replies
  • 582 views
  • 4 likes
  • 5 in conversation