Using Functions and CALL Routines

Restrictions Affecting Function Arguments

If the value of an argument is invalid, SAS writes a note or error message to the log and sets the result to a missing value. Here are some common restrictions for function arguments:
  • Some functions require that their arguments be restricted within a certain range. For example, the argument of the LOG function must be greater than 0.
  • When a numeric argument has a missing value, many functions write a note to the SAS log and return a missing value. Exceptions include some of the descriptive statistics functions and financial functions.
  • For some functions, the allowed range of the arguments is platform-dependent, such as with the EXP function.

Using the OF Operator with Temporary Arrays

You can use the OF operator with temporary arrays. This capability enables the passing of temporary arrays to most functions whose arguments contain a varying number of parameters. You can use temporary arrays in OF lists in some functions, just as you can use temporary arrays in OF lists in regular variable arrays.
There are some limitations in using temporary arrays. These limitations are listed after the example.
The following example shows how you can use temporary arrays:
data _null_;
   array y[10] _temporary_ (1,2,3,4,5,6,7,8,9,10);
   x = sum(of y{*});
   put x=;
run;

data _null_;
   array y[10] $10 _temporary_ ('1','2','3','4','5',
                                '6','7','8','9','10');
   x = max(of y{*});
   put x=;
run;
Log Output for the Example of Using Temporary Arrays
x=55
x=10
The following limitations affect temporary array OF lists:
  • cannot be used as array indices
  • can be used in functions where the number of parameters matches the number of elements in the OF list, as with regular variable arrays
  • can be used in functions that take a varying number of parameters
  • cannot be used with the DIF, LAG, SUBSTR, LENGTH, TRIM, or MISSING functions, nor with any of the variable information functions such as VLENGTH

Characteristics of Target Variables

Some character functions produce resulting variables, or target variables, with a default length of 200 bytes. Numeric target variables have a default length of 8 bytes. Character functions to which the default target variable lengths do not apply are shown in the following table. These functions obtain the length of the return argument based on the length of the first argument.
Functions Whose Return Argument Is Based on the Length of the First Argument
Functions
COMPBL
RIGHT
COMPRESS
STRIP
DEQUOTE
SUBSTR
INPUTC
SUBSTRN
LEFT
TRANSLATE
LOWCASE
TRIM
PUTC
TRIMN
REVERSE
UPCASE
The following list of functions shows the length of the target variable if the target variable has not been assigned a length:
BYTE
target variable is assigned a default length of 1.
INPUT
length of the target variable is determined by the width of the informat.
PUT
length of the target variable is determined by the width of the format.
VTYPE
target variable is assigned a default length of 1.
VTYPEX
target variable is assigned a default length of 1.

Notes about Descriptive Statistic Functions

SAS provides functions that return descriptive statistics. Many of these functions correspond to the statistics produced by the MEANS and UNIVARIATE procedures. The computing method for each statistic is discussed in the elementary statistics procedures section of the Base SAS Procedures Guide. SAS calculates descriptive statistics for the nonmissing values of the arguments.

Notes about Financial Functions

Types of Financial Functions

SAS provides a group of functions that perform financial calculations. The functions are grouped into the following types:
Types of Financial Functions
Function Type
Function
Description
Cashflow
CONVX, CONVXP
calculates convexities for cashflows
DUR, DURP
calculates modifies duration for cashflows.
PVP, YIELDP
calculates present value and yield-to-maturity for a periodic cashflow
Parameter calculations
COMPOUND
calculates compound interest parameters
MORT
calculates amortization parameters
Internal rate of return
INTRR, IRR
calculates the internal rate of return
Net present and future value
NETPV, NPV
calculates net present and future values
SAVING
calculates the future value of periodic saving
Depreciation
DACCxx
calculates the accumulated depreciation up to the specified period
DEPxxx
calculates depreciation for a single period
Pricing
BLKSHCLPRC, BLKSHPTPRC
calculated call prices and put prices for European options on stocks, based on the Black-Scholes model
BLACKPLPRC, BLACKPTPRC
calculates call prices and put prices for European options on futures, based on the Black model
GARKHCLPRC, GARKHPTPRC
calculates call prices and put prices for European options on stocks, based on the Garman-Kohlhagen model
MARGRCLPRC, MARGRPTPRC
calculates call options and put prices for European options on stocks, based on the Margrabe model

Using Pricing Functions

A pricing model is used to calculate a theoretical market value (price) for a financial instrument. This value is referred to as a mark-to-market (MTM) value. Typically, a pricing function has the following form:
p r i c e = f u n c t i o n ( r f 1 , r f 2 , r f 3 , . . . )
In the pricing function, rf1, rf2, and rf3 are risk factors such as interest rates or foreign exchange rates. The specific values of the risk factors that are used to calculate the MTM value are the base case values. The set of base case values is known as the base case market state.
After determining the MTM value, you can perform the following tasks with the base case values of the risk factors (rf1, rf2, and rf3):
  • Set the base case values to specific values to perform scenario analyses.
  • Set the base case values to a range of values to perform profit/loss curve analyses and profit/loss surface analyses.
  • Automatically set the base case values to different values to calculate sensitivities - that is, to calculate the delta and gamma values of the risk factors.
  • Perturb the base case values to create many possible market states so that many possible future prices can be calculated, and simulation analyses can be performed. For Monte Carlo simulation, the values of the risk factors are generated using mathematical models and the copula methodology.
A list of pricing functions and their descriptions are included in Types of Financial Functions.

Using DATA Step Functions within Macro Functions

The macro functions %SYSFUNC and %QSYSFUNC can call most DATA step functions to generate text in the macro facility. %SYSFUNC and %QSYSFUNC have one difference: %QSYSFUNC masks special characters and mnemonics and %SYSFUNC does not. For more information about these functions, see %QSYSFUNC and %SYSFUNC in SAS Macro Language: Reference.
%SYSFUNC arguments are a single DATA step function and an optional format, as shown in the following examples:
%sysfunc(date(),worddate.)
%sysfunc(attrn(&dsid,NOBS))
You cannot nest DATA step functions within %SYSFUNC. However, you can nest %SYSFUNC functions that call DATA step functions. For example:
%sysfunc(compress(%sysfunc(getoption(sasautos)),
   %str(%)%(%')));
All arguments in DATA step functions within %SYSFUNC must be separated by commas. You cannot use argument lists that are preceded by the word OF.
Because %SYSFUNC is a macro function, you do not need to enclose character values in quotation marks as you do in DATA step functions. For example, the arguments to the OPEN function are enclosed in quotation marks when you use the function alone, but the arguments do not require quotation marks when used within %SYSFUNC.
dsid=open("sasuser.houses","i");
dsid=open("&mydata","&mode");
%let dsid=%sysfunc(open(sasuser.houses,i));
%let dsid=%sysfunc(open(&mydata,&mode));

Using CALL Routines and the %SYSCALL Macro Statement

When the %SYSCALL macro statement invokes a CALL routine, the value of each macro variable argument is retrieved and passed unresolved to the CALL routine. Upon completion of the CALL routine, the value for each argument is written back to the respective macro variable. If %SYSCALL encounters an error condition, the execution of the CALL routine terminates without updating the macro variable values and an error message is written to the log.
When %SYSCALL invokes a CALL routine, the argument value is passed unresolved to the CALL routine. The unresolved argument value might have been quoted using macro quoting functions and might contain delta characters. The argument value in its quoted form can cause unpredictable results when character values are compared. Some CALL routines unquote their arguments when they are called by %SYSCALL and return the unquoted values. Other CALL routines do not need to unquote their arguments. The following is a list of CALL routines that unquote their arguments when called by %SYSCALL:
In comparison, %SYSCALL invokes a CALL routine and returns an unresolved value, which contains delta characters. %SYSFUNC invokes a function and returns a resolved value, which does not contain delta characters. For more information, see Macro Quoting in SAS Macro Language: Reference, %SYSCALL Statement in SAS Macro Language: Reference, and %SYSFUNC and %QSYSFUNC Functions in SAS Macro Language: Reference.

Using Functions to Manipulate Files

SAS manipulates files in different ways, depending on whether you use functions or statements. If you use functions such as FOPEN, FGET, and FCLOSE, you have more opportunity to examine and manipulate your data than when you use statements such as INFILE, INPUT, and PUT.
When you use external files, the FOPEN function allocates a buffer called the File Data Buffer (FDB) and opens the external file for reading or updating. The FREAD function reads a record from the external file and copies the data into the FDB. The FGET function then moves the data to the DATA step variables. The function returns a value that you can check with statements or other functions in the DATA step to determine how to further process your data. After the records are processed, the FWRITE function writes the contents of the FDB to the external file, and the FCLOSE function closes the file.
When you use SAS data sets, the OPEN function opens the data set. The FETCH and FETCHOBS functions read observations from an open SAS data set into the Data Set Data Vector (DDV). The GETVARC and GETVARN functions then move the data to DATA step variables. The functions return a value that you can check with statements or other functions in the DATA step to determine how you want to further process your data. After the data is processed, the CLOSE function closes the data set.
For a complete listing of functions and CALL routines, see SAS Functions and CALL Routines by Category. For complete descriptions and examples, see the dictionary section of this book.