# Using Functions

• 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.

• Most functions do not permit missing values as arguments. Exceptions include some of the descriptive statistic functions and financial functions.

• In general, the allowed range of the arguments is platform-dependent, such as with the EXP function.

• For some probability functions, combinations of extreme values can cause convergence problems.

 Characteristics of Target Variables

Target Variables
Function Target Variable Type Target Variable Length (bytes)
BYTE character 1
COMPRESS character length of first argument
INPUT character width of informat

numeric 8
LEFT character length of argument
PUT character width of format
REVERSE character length of argument
RIGHT character length of argument
SUBSTR character length of first argument
TRANSLATE character length of first argument
TRIM character length of argument
UPCASE, LOWCASE character length of argument
VTYPE, VTYPEX character 1

 Notes on Descriptive Statistic Functions

 Notes on 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 Functions Description
Cashflow CONVX, CONVXP calculates convexity for cashflows

DUR, DURP calculates modified 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

### Special Considerations for Depreciation Functions

The period argument for depreciation functions can be fractional for all of the functions except DEPDBSL and DACCDBSL. For fractional arguments, the depreciation is prorated between the two consecutive time periods preceding and following the fractional period.

CAUTION:
Verify the depreciation method for fractional periods. You must verify whether this method is appropriate to use with fractional periods because many depreciation schedules, specified as tables, have special rules for fractional periods.

 Using DATA Step Functions within Macro Functions

%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 you use them within %SYSFUNC.

```dsid=open("sasuser.houses","i");
dsid=open("&mydata","&mode");
%let dsid=%sysfunc(open(sasuser.houses,i));
%let dsid=%sysfunc(open(&mydata,&mode));```

You can use these functions to call all of the DATA step SAS functions except those that pertain to DATA step variables or processing. These prohibited functions are: DIF, DIM, HBOUND, INPUT, IORCMSG, LAG, LBOUND, MISSING, PUT, RESOLVE, SYMGET, and all of the variable information functions (for example, VLABEL).

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 Categories and Descriptions of Functions. For complete descriptions and examples, see SAS Language Reference: Dictionary.