Chapter Contents

Previous

Next

Using Functions


Restrictions on Function Arguments
If the value of an argument is invalid, SAS prints an error message and sets the result to a missing value. Here are some common restrictions on function arguments:


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. Character functions to which the default target variable lengths do not apply are shown in the following table.

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

SAS provides functions that return descriptive statistics. Except for the MISSING function, the functions correspond to the statistics produced by the MEANS procedure. The computing method for each statistic is discussed in "SAS Elementary Statistics Procedures" in the appendix of the SAS Procedures Guide. SAS calculates descriptive statistics for the nonmissing values of the arguments.


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.  [cautionend]


Using DATA Step Functions within Macro Functions

The macro functions %SYSFUNC and %QSYSFUNC can call 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 on 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 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).


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


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.