Exploring SAS Macro functions – Eval and Sysevalf

EVAL and SYSEVALF are the two macro functions that evaluate arithmetic expressions and logical expressions.

First, let’s understand why do we need EVAl or the SYSEVALF function. In SAS, Macro’s are text-based which means there are no numeric variables. Whereas in the Data Step, numbers and characters are two separate things.

Therefore, arithmetic and logical operations are not as straightforward. That is why we need evaluations function as the %Eval Macro Function.

See the example below.

%let x = 5;
%let y = 1+2;
%let z = &x+5;
%put &=x &=y &=z

In the log:

X=5 Y=1+2 Z=5+5

As you can see, the arithmetic expressions are not evaluated and macro variables resolve to plain text.

In summary, The evaluation macro functions are used to:

  • To evaluate arithmetic and logical expressions
  • Inside and outside of macros
  • During logical comparisons to specify TRUE or FALSE (Evaluation functions return a value of 1 for logical expressions if the condition is true, 0 if it is false)
  • To perform integer and floating-point arithmetic.

The EVAL Function

The %EVAL function is used to evaluate integer arithmetic or logical expressions. The argument passed in the %EVAL function is converted from character to numeric and the results are converted back to character.

Syntax of the Eval Function:

%EVAL(arithmetic expression|logical expression)

This is a simple example of the Eval function.

%let x = 5;
%let y = %eval(1+2);
%let z = %eval(&x+5);
%put &=x &=y &=z;

In the log:

X=5 Y=3 Z=10

The %Eval Function treats integers as whole numbers. As in the example above, 2 and 1 were treated as whole numbers and the evaluation was successful.

You cannot use floating-point numbers with the eval function. For example, you cannot add  1.5 to 2 as in the above example. SAS treats period in a numeric expression as a character value and this will cause an error because it failed to add a character value to a number.

All parts of the macro language that evaluate expressions (for example, %IF and %DO statements) call %EVAL to evaluate the expressions.

The %EVAL function can be called explicitly or implicitly. It appears elusive and problematic when its argument is a macro expression that contains special characters, mixed arithmetic and logical operators, or any other macro quoting functions.

How the Eval function interacts with special characters?

Once %EVAL is invoked, whether implicitly or explicitly, its argument is scanned, resolved, and passed to %EVAL to evaluate. Then %EVAL determines whether it performs an integer arithmetic evaluation or a logical comparison based on the actual resolved argument. Eval function treats the expression is as arithmetic when all operands in its arguments can be interpreted as an integer. If at least one operand cannot be interpreted as numeric, the expression is treated as logical. We must be careful when it comes to logical comparisons.

%let x = %eval(10.0 > 5.0);
%put &=x

And here is the output of the above code,

X=0

Here, we have used the %Eval Function to evaluate whether 10.0 is greater than 5.0.

Practically it is true and should return one but remember, that the %Eval Function treats both 10.0 and 5.0 as character values because of the period.

Therefore, a character comparison takes place from left to right. While comparing character values, SAS uses the same sort sequence as Proc Sort.

This means that ‘1’ comes before ‘2’. Therefore, the %Eval Function evaluates to 0.

If a division operation results in a fraction, the fraction is truncated to an integer. You can use the %SYSEVALF function to deal with non-integer floating-point arithmetic and comparisons.

%let x = %eval(19/5);;
%put &=x

In the log.

X=3

In this example, we have divided 19 by 5 using the eval function, which is 3.8.

As you can see, the %Eval Macro Function truncates the result to an integer. Therefore, the returned value is 3.

%EVAL only accepts integers, and if it has a period (*) character, it will generate an error when they are part of an expression.

However, if a period appears in a resolved logical expression, it will be treated as a regular character. For example, if the SAS version is 8.2, the expression.

%eval(&sysver > 10)

is evaluated as true.

The following examples show correct and incorrect usage of the period, respectively:

Example:

%put %eval(10.0+20.0)
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. 
The condition was: 10.0+20.0

Now see the different results of using %EVAL and %SYSEVALF.

%put %eval(10+.)
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. 
The condition was: 10+.
%put %sysevalf(10+.)
NOTE: Missing values were generated as a result of performing an operation on missing values during %SYSEVALF expression evaluation.

%EVAL treats the period as a character value, which causes an error in an arithmetic operation. However, %SYSEVAL treats the period as a missing value and returns a missing value without an error.

For more details, I would recommend reading, the article – Take an In-Depth Look at the %EVAL Function

The SYSEVALF Function

%SYSEVALF function is used to evaluate floating-point numbers. The result of the %SYSEVALF function are converted to text but you can also request %SYSEVALF to convert the result to a different format. Below are the formats that can be used with the %sysevalf function.

Conversion Type Returned Value
BOOLEAN 0   if the result of the expression is 0 or missing 1   if the result is any other value
CEIL Round to the next largest whole integer
FLOOR Round to the next smallest whole integer
INTEGER Truncate the decimal fraction

 

Syntax:

%SYSEVALF(expression)

When requesting one of these four conversion types, specify the conversion type as the second argument to %SYSEVALF.

Example:

%sysevalf(1/3,boolean) /* returns 1 */
%sysevalf(10+.,boolean) /* returns 0 */

The %SYSEVALF function evaluates arithmetic and logical expressions using floating-point arithmetic and returns a value that is formatted using the BEST32. format.

You can use the %SYSEVALF function to perform arithmetic calculations that have floating-point value.

Below are some examples of the %SYSEVALF function.

%put %sysevalf(10.0*30.0); * returns in the log is 300;
%put %sysevalf(10.5+20.8); * returns in the log is 31.3;
%put %sysevalf(50/30);   * returns in the log is 1.66666666666666;

%SYSEVALF function perform arithmetic calculations and the result of the evaluation can be a floating-point value like in the final and last macro variable case, but as in integer arithmetic calculations, the result is always a text.

The %SYSEVALF function is used in conjugation with other functions like INTEGER, CEIL, and FLOOR.

For example, the following %PUT statements return 3, 4 and 3 respectively:

%let pi=2.14;

%put %sysevalf(&pi,integer); *PI returns in the log is 2;
%put %sysevalf(&pi, ceil); *PI returns in the log is 3;
%put %sysevalf(&pi,floor); *PI returns in the log is 2;

Conclusion:

So, this was our side on the 2 useful macro functions. Use the %Eval function to evaluate an arithmetic expression in SAS macro and use the %SYSEVALF function to evaluate floating-point arithmetic expressions.

We really hope that you must have found this article useful.

Moreover, if you have any other suggestions regarding other plagiarism tools, suggest us below the comment section. We would really take those lists in our further blog post.

Thanks for reading!

If you liked this article, you might also want to read CALL SYMPUT in SAS and Creating macro variables from the SAS dataset as well.

Do you any tips to add Let us know in the comments.

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap