Help using Base SAS procedures

Trouble with arithmetic operators in Macros

Reply
Occasional Contributor MBI
Occasional Contributor
Posts: 19

Trouble with arithmetic operators in Macros

Hi all,

I'm pretty new to macros and I ran into a roadblock on the code below. I don't understand why the first one works but the second one doesn't. Any help would be appreciated!

-MBI


Example 1 - the 'do' loop is executed
%let A = 1;
%let B = 2;
%let P = (&A + &B);
%if &P = 3 %then %do;
proc sql;
UPDATE nxn SET M1=333 WHERE obs=1;
run;
%end;


Example 2 - the 'do' loop is NOT executed
%let A = 1;
%let B = 2;
%let P = (&A / &B);
%if &P = 0.5 %then %do;
proc sql;
UPDATE nxn SET M1=333 WHERE obs=1;
run;
%end;

I'm also having the same problem with the do loop not executing when using the SQRT operator. Everything seems to work fine if I stay away from division and SQRT. Any ideas?
SAS Super FREQ
Posts: 8,743

Re: Trouble with arithmetic operators in Macros

Hi:
I suggest you read the Macro doc on how arithmetic needs to be specified in order to be evaluated correctly:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001072288.htm
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/z6express.htm

cynthia
Occasional Contributor MBI
Occasional Contributor
Posts: 19

Re: Trouble with arithmetic operators in Macros

Cynthia,

Wow, thanks for the information. That was perfect! Am I correct in assuming that there is no way to do a SQRT? I didn't see it on the list of operators from:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/z6express.htm#a001072320

Thanks,

MBI
Super Contributor
Super Contributor
Posts: 3,174

Re: Trouble with arithmetic operators in Macros

You have DATA step functions available in macro language when using the %SYSFUNC macro function. Here is a DOC link - also search the SAS support http://support.sas.com/ website for additional technical references on the topic.

Scott Barry
SBBWorks, Inc.


http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a001962663.htm
Valued Guide
Posts: 2,175

Re: Trouble with arithmetic operators in Macros

you may find %sysEvalF() helpful, for example [pre] %put sqrt = %sysevalF( 64 ** 0.5 ) ;
%put sqrt = %sysevalF( 60 ** 0.5 ) ;[/pre]

These print square roots in the log, like[pre]1 %put sqrt = %sysevalF( 64 ** 0.5 ) ;
sqrt = 8
2 %put sqrt = %sysevalF( 60 ** 0.5 ) ;
sqrt = 7.74596669241483[/pre]

PeterC
Occasional Contributor MBI
Occasional Contributor
Posts: 19

Re: Trouble with arithmetic operators in Macros

Cynthia/Scott/Peter,

Thanks so much for the help. My macro is now working properly.

-MBI
Ask a Question
Discussion stats
  • 5 replies
  • 135 views
  • 0 likes
  • 4 in conversation