Martin Reindl
Martin Reindl

Reputation: 1603

Store result of numeric expression in SAS macro variable

I am using SAS Enterprise guide and want to store the result of a small calculation in a SAS macro variable:

My code looks as follows:

%do quarter = 0 to 3; 
    %let macro_variable = &quarter. * 3; 
%end; 

However, this results in the following errors in my code:

Statement is not valid, or it is used out of the proper order. Affected code: 0*3.

What am I doing wrong here? This seems like such a simple operation.

Upvotes: 0

Views: 340

Answers (1)

M_CE_A
M_CE_A

Reputation: 66

The macro function %eval() lets you perform arithmetic with integer macro variables.

Try this:

%do quarter = 0 %to 3;
  %let macro_variable = %eval(&quarter. *3);
%end;

If you need a calculation for non integers then replace %eval with %sysevalf which uses floating point arithmetic

Upvotes: 1

Related Questions