Hi All,
Getting an issue when using macro variable in macro condition. This code works if the macro variable values are whole numbers but if the values contain decimal values then SAS does not process the conditions correctly.
In the below example, prow1 value is greater than prow2 value but somehow SAS is doing %IF condition &prow2. > &prow1. is TRUE
Can you please help to identify issue and provide a resolution?
options mlogic mprint; %macro chk; /*The prow values are obtained by using PROC SQL SELECT INTO but below are displayed for example purpose*/ %let prow1 = 13.23529; %let prow2 = 5.714286; %put prow1, prow2 ==> &prow1. , &prow2.; %if &prow1. > &prow2. %then %do; data chk; a = "&prow1. > &prow2."; run; proc print data=chk; run; %end; %if &prow2. > &prow1. %then %do; data chk2; a = "&prow2. > &prow1."; run; proc print data=chk2; run; %end; %mend chk; %chk;
And here is the LOG for above code:
MLOGIC(CHK): Beginning execution. MLOGIC(CHK): %LET (variable name is PROW1) MLOGIC(CHK): %LET (variable name is PROW2) MLOGIC(CHK): %PUT prow1, prow2 ==> &prow1. , &prow2. prow1, prow2 ==> 13.23529 , 5.714286 MLOGIC(CHK): %IF condition &prow1. > &prow2. is FALSE MLOGIC(CHK): %IF condition &prow2. > &prow1. is TRUE MPRINT(CHK): data chk2; MPRINT(CHK): a = "5.714286 > 13.23529"; MPRINT(CHK): run; NOTE: The data set WORK.CHK2 has 1 observations and 1 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds MPRINT(CHK): proc print data=chk2; MPRINT(CHK): run; NOTE: There were 1 observations read from the data set WORK.CHK2. NOTE: The PROCEDURE PRINT printed page 1. NOTE: PROCEDURE PRINT used (Total process time): real time 0.00 seconds cpu time 0.01 seconds
Thank you!
You need %SYSEVALF to evaluate floats
%if %sysevalf(&prow1. > &prow2.) %then %do;
You need %SYSEVALF to evaluate floats
%if %sysevalf(&prow1. > &prow2.) %then %do;
Thank you, @novinosrin!
The implicit %EVAL() function call used by %IF does not do floating point. So it will treat your example values as strings to be compared. Use %SYSEVALF() instead.
%if %sysevalf(&prow1. > &prow2.) %then %do;
One thing to keep in mind: MACRO VARIABLES ARE ALWAYS TEXT.
So comparisons will be done like text comparisons: first character to first character, second to second and so, and the first difference setting the inequality.
If you want to compare character strings that contain decimal values then you need to wrap the comparison in %sysevalf, to evaluate the floating point (decimal) values.
%macro chk(); /*The prow values are obtained by using PROC SQL SELECT INTO but below are displayed for example purpose*/ %let prow1 = 13.23529; %let prow2 = 5.714286; %put prow1, prow2 ==> &prow1. , &prow2.; %if %sysevalf(&prow1. > &prow2.) %then %do; data chk; a = "&prow1. > &prow2."; run; proc print data=chk; run; %end; %if %sysevalf(&prow2. > &prow1.) %then %do; data chk2; a = "&prow2. > &prow1."; run; proc print data=chk2; run; %end; %mend chk;
Thank you, @ballardw!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.