DATA Step, Macro, Functions and more

Issue in SAS while trying to create a macro.

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Issue in SAS while trying to create a macro.

*** Test macro ***;

%macro test;

%if (%sysevalf(&a*1) <= %sysevalf(&b*1)) %then %do;

%put %sysevalf(&a*1) IS LESS THAN OR EQUAL TO %sysevalf(&b*1);

%end;

%else %do;

%put %sysevalf(&a*1) GREATER THAN %sysevalf(&b*1);

%end;

%mend test;

%let a=10.6;

%let b=9.9;

%test;

%let a=9.9;

%let b=10.6;

%test;


These two macro calls give me a wrong answer. Can anyone help me out from this?


Accepted Solutions
Solution
‎09-23-2014 01:13 PM
Super User
Posts: 5,085

Re: Issue in SAS while trying to create a macro.

Also note, once you have the proper number of %sysevalf functions,  you do not need to multiply by 1.

%if 9.9 < 10.6 is false because decimal points force a character comparison, but

%if %syseval(9.9 < 10.6) returns 1, a true comparison.

View solution in original post


All Replies
Super User
Posts: 17,874

Re: Issue in SAS while trying to create a macro.

Your comparison is also a function, so add one more %sysevalf:

%macro test;

%if (%sysevalf(%sysevalf(&a*1) <= %sysevalf(&b*1))) %then %do;

%put %sysevalf(&a*1) IS LESS THAN OR EQUAL TO %sysevalf(&b*1);

%end;

%else %do;

%put %sysevalf(&a*1) GREATER THAN %sysevalf(&b*1);

%end;

%mend test;

%let a=10.6;

%let b=9.9;

%test;

%let a=9.9;

%let b=10.6;

%test;

Respected Advisor
Posts: 3,777

Re: Issue in SAS while trying to create a macro.

Actually you have too many %SYSEVALs

%macro test;
  
%let rc = %sysevalf(&a*1 <= &b*1,boolean);
   %put NOTE: &=rc;
   %let tf = %scan(False True,%eval(&rc+1));
   %put NOTE: Expression &a*1 <= &b*1 is &tf;
   %mend test;

%let a=10.6;
%let b=9.9;
%test;

%let a=9.9;
%let b=10.6;
%test;

40         %let a=10.6;
41         %let b=9.9;
42         %test;
NOTE: RC=
0
NOTE: Expression
10.6*1 <= 9.9*1 is False
43        
44         %let a=9.9;
45         %let b=10.6;
46         %test;
NOTE: RC=
1
NOTE: Expression
9.9*1 <= 10.6*1 is True
Solution
‎09-23-2014 01:13 PM
Super User
Posts: 5,085

Re: Issue in SAS while trying to create a macro.

Also note, once you have the proper number of %sysevalf functions,  you do not need to multiply by 1.

%if 9.9 < 10.6 is false because decimal points force a character comparison, but

%if %syseval(9.9 < 10.6) returns 1, a true comparison.

Respected Advisor
Posts: 3,777

Re: Issue in SAS while trying to create a macro.

I'n not the one who wrote the expression.  I don't care if the OP multiplies by 1 or not.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 216 views
  • 3 likes
  • 4 in conversation