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!
%let salary1=25000;
%let salary2=27000;
%let salary_diff = %eval(&salary2 - &salary1);
%put The salary difference is &salary_diff;
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!
%let salary1=25000;
%let salary2=27050.45;
%let salary_diff = %eval(&salary2 - &salary1);
%put The salary difference is &salary_diff;
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!
%let salary1=25000;
%let salary2=27050.45;
%let salary_diff = %sysevalf(&salary2 - &salary1);
%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
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.