Published on :
Macro CREATION_INTERNE

Using %EVAL and %SYSEVALF Functions

This code is also available in: Deutsch Español Français
Awaiting validation
This SAS© script illustrates how to use the %EVAL and %SYSEVALF macro functions to perform arithmetic operations on macro variables. It starts with a simple salary difference calculation using %EVAL, highlighting its suitability for integers. Then, it emphasizes the limitation of %EVAL with decimal numbers by introducing a floating-point value that causes an error. Finally, it presents %SYSEVALF as the robust solution for handling calculations with floating-point numbers, ensuring precision and avoiding errors.
Data Analysis

Type : CREATION_INTERNE


Data consists of numeric macro variables (%salary1, %salary2) defined directly within the script, without any external data source or SASHELP data.

1 Code Block
MACRO Data
Explanation :
This block initializes two macro variables, `salary1` and `salary2`, with integer values. It then uses the `%EVAL` macro function to calculate the difference between these two salaries and stores the result in a new macro variable `salary_diff`. The value of `salary_diff` is displayed in the SAS log using `%PUT`. This case works because `%EVAL` is suitable for integer operations.
Copied!
1%let salary1=25000;
2%let salary2=27000;
3%let salary_diff = %eval(&salary2 - &salary1);
4%put The salary difference is &salary_diff;
2 Code Block
MACRO
Explanation :
In this block, `salary2` is modified to include a decimal value. The difference calculation is attempted again with `%EVAL`. However, `%EVAL` is designed for integer operations and cannot correctly handle floating-point numbers. This would result in an error or warning in the SAS log, as `%EVAL` attempts to convert the decimal value to an integer, which is a limitation of this function.
Copied!
1%let salary1=25000;
2%let salary2=27050.45;
3%let salary_diff = %eval(&salary2 - &salary1);
4%put The salary difference is &salary_diff;
3 Code Block
MACRO
Explanation :
This block corrects the previous problem by using the `%SYSEVALF` macro function. `%SYSEVALF` is specifically designed to evaluate arithmetic expressions that can include floating-point numbers. It allows for precise calculation of the salary difference, even when the operands are decimals, without generating data type-related errors or warnings in the SAS log.
Copied!
1%let salary1=25000;
2%let salary2=27050.45;
3%let salary_diff = %sysevalf(&salary2 - &salary1);
4%put The salary difference is &salary_diff;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Practice 3: Use the %EVAL and %SYSEVALF Functions Lesson 2 - Using Macro Functions SAS Macro Language 1: Essentials