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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

You need %SYSEVALF to evaluate floats

 


%if %sysevalf(&prow1. > &prow2.) %then %do;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

You need %SYSEVALF to evaluate floats

 


%if %sysevalf(&prow1. > &prow2.) %then %do;
Tom
Super User Tom
Super User

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;
tanmaykhole208
Calcite | Level 5
Thank you, @Tom!
ballardw
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 558 views
  • 2 likes
  • 4 in conversation