Using SAS Functions with Macro Variables

The %SYSFUNC Function

A Brief Overview

The %SYSFUNC function executes SAS functions or user-written functions in the macro facility. All arguments in DATA step functions within %SYSFUNC must be separated by commas. You cannot use argument lists preceded by the word OF.
Note: The arguments in %SYSFUNC are evaluated according to the rules of the SAS macro language. This includes both the function name and the argument list in the function. In particular, an empty argument position will not generate a NULL argument, but a zero-length argument.
%SYSFUNC does not mask special characters or mnemonic operators in its result.
When a function called by %SYSFUNC requires a numeric argument, the macro facility converts the argument to a numeric value. %SYSFUNC can return a floating-point number when the function that is executed supports floating-point numbers.
Note: Instead of INPUT and PUT, use INPUTN, INPUTC, PUTN, and PUTC.
CAUTION:
Values returned by SAS functions might be truncated.
Although values returned by macro functions are not limited to the length imposed by the DATA step, values returned by SAS functions do have that limitation.

%SYSFUNC Syntax

Syntax, %SYSFUNC function:
%SYSFUNC(function(argument-1<...argument-n>)<,format>)
function
is the name of the function to execute. This function can be a SAS function, a function written with SAS/TOOLKIT software, or a function created using the FCMP procedure. The function cannot be a macro function.
argument
is one or more arguments used by function. An argument can be a macro variable reference or a text expression that produces arguments for a function.
format
is an optional format to apply to the result of function. There is no default value for format. If you do not specify a format, the SAS macro facility does not perform a format operation on the result and uses the default of the function.

Example: Using the %SYSFUNC Function

Use the %SYSFUNC function to execute the PROPCASE function to convert all uppercase letters to lowercase, and convert the first character of a word to uppercase.
%let string=william SMITH;
%put %sysfunc(propcase(&string));
The following is written to the SAS log.
Log 8.4 SAS Log
William Smith

The %EVAL Function

A Brief Overview

The %EVAL function evaluates arithmetic and logical expressions using integer arithmetic.
The %EVAL function evaluates integer arithmetic or logical expressions. %EVAL converts its argument from a character value to a numeric or logical expression and performs the evaluation. Finally, %EVAL converts the result back to a character value and returns that value.
If all operands can be interpreted as integers, the expression is treated as arithmetic. If at least one operand cannot be interpreted as numeric, the expression is treated as logical. If a division operation results in a fraction, the fraction is truncated to an integer.
Logical, or Boolean, expressions return a value that is evaluated as true or false. In the macro language, any numeric value other than 0 is true, and a value of 0 is false.
%EVAL accepts only operands in arithmetic expressions that represent integers (in standard or hexadecimal form). Operands that contain a period character cause an error when they are part of an integer arithmetic expression. The following examples show correct and incorrect usage, respectively:
%let d=%eval(10+20);       /* Correct usage   */ 
%let d=%eval(10.0+20.0);   /* Incorrect usage */
Because %EVAL does not convert a value containing a period to a number, the operands are evaluated as character operands. When %EVAL encounters a value containing a period, it displays an error message about finding a character operand where a numeric operand is required. An expression that compares character values in the %EVAL function uses the sort sequence of the operating environment for the comparison.
Note: %EVAL performs integer evaluations, but %SYSEVALF performs floating-point evaluations.

%EVAL Syntax

Syntax, %EVAL function:
%EVAL(arithmetic or logical expression)

Example: Using the %EVAL Function

The following example illustrates different types of integer arithmetic evaluation.
%let a=1+2;
%let b=10*3;
%let c=5/3;
%let eval_a=%eval(&a);
%let eval_b=%eval(&b);
%let eval_c=%eval(&c);

%put &a is &eval_a;
%put &b is &eval_b;
%put &c is &eval_c;
The following is written to the SAS log.
Log 8.5 SAS Log
1+2 is 3
10*3 is 30
5/3 is 1
The third %PUT statement shows that %EVAL discards the fractional part of the result when it performs division on integers that would produce a fraction:

The %SYSEVALF Function

A Brief Overview

The %SYSEVALF function evaluates arithmetic and logical expressions using floating-point arithmetic.
The %SYSEVALF function performs floating-point arithmetic and returns a value that is formatted using the BEST32. format. The result of the evaluation is always text. %SYSEVALF is the only macro function that can evaluate logical expressions that contain floating-point or missing values. Specify a conversion type to prevent problems when %SYSEVALF returns one of the following:
  • missing or floating-point values in macro expressions
  • macro variables that are used in other macro expressions that require an integer value
If the argument in the %SYSEVALF function contains no operator and no conversion type is specified, then the argument is returned unchanged.
Note: %SYSEVALF supports floating-point numbers. However, %EVAL performs only integer arithmetic. You must use the %SYSEVALF macro function in macros to evaluate floating-point expressions. However, %EVAL is used automatically by the macro processor to evaluate macro expressions.

%SYSEVALF Syntax

Syntax, %SYSEVALF function:
%SYSEVALF(expression<,conversion-type>)
expression
is an arithmetic or logical expression to evaluate.
conversion-type
converts the value returned by %SYSEVALF to the type of value specified. The value can then be used in other expressions that require a value of that type. Conversion-type can be one of the following:
BOOLEAN
returns 0 if the result of the expression is 0 or missing. It returns 1 if the result is any other value.
CEIL
returns a character value representing the smallest integer that is greater than or equal to the result of the expression. If the result is within 10–12 of an integer, the function returns a character value representing that integer.
FLOOR
returns a character value representing the largest integer that is less than or equal to the result of the expression. If the result is within 10–12 of an integer, the function returns that integer.
INTEGER
returns a character value representing the integer portion of the result (truncates the decimal portion). If the result of the expression is within 10–12 of an integer, the function produces a character value representing that integer. If the result of the expression is positive, INTEGER returns the same result as FLOOR. If the result of the expression is negative, INTEGER returns the same result as CEIL.

Example: Using the %SYSEVALF Function

The macro FIGUREIT performs all types of conversions for %SYSEVALF values.
%macro figureit(a,b);
   %let y=%sysevalf(&a+&b);
   %put The result with SYSEVALF is: &y;
   %put  The BOOLEAN value is: %sysevalf(&a +&b, boolean);
   %put  The CEIL value is: %sysevalf(&a+&b, ceil);
   %put  The FLOOR value is: %sysevalf(&a+&b, floor);
   %put  The INTEGER value is: %sysevalf(&a+&b, int);
%mend figureit;

%figureit(100,1.597)
The following is written to the SAS log.
Log 8.6 SAS Log
The result with SYSEVALF is: 101.597
The BOOLEAN value is: 1
The CEIL value is: 102
The FLOOR value is: 101
The INTEGER value is: 101
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.197.128