Appendix B. SAS/IML Operators, Functions, and Statements

Contents

  • B.1 Overview of the SAS/IML Language 419

  • B.2 A Summary of Frequently Used SAS/IML Operators 420

  • B.3 A Summary of Functions and Subroutines 420

    • B.3.1 Mathematical Functions 420

    • B.3.2 Probability Functions 421

    • B.3.3 Descriptive Statistical Functions 421

    • B.3.4 Matrix Query Functions 422

    • B.3.5 Matrix Reshaping Functions 422

    • B.3.6 Linear Algebra Functions 423

    • B.3.7 Set Functions 423

    • B.3.8 Formatting Functions 424

    • B.3.9 Module Statements 424

    • B.3.10 Control Statements 425

    • B.3.11 Statements for Reading and Writing SAS Data Sets 425

    • B.3.12 Options for Printing Matrices 425

B.1 Overview of the SAS/IML Language

The SAS/IML language contains over 300 built-in functions and subroutines. There are also hundreds of functions in Base SAS software that you can call from SAS/IML programs.

The best overview of these many functions is Chapter 23, "Language Reference" (SAS/IML User's Guide). The section "Overview" lists the SAS/IML functions according to their functionality; the section "Base SAS Functions Accessible from SAS/IML Software" lists the Base SAS functions according to their functionality.

This appendix presents tables that list functions discussed in this book and also related functions that were not explicitly used in this book but that are used often in practice.

B.2 A Summary of Frequently Used SAS/IML Operators

The SAS/IML language provides standard elementwise operators such as addition, subtraction, multiplication, and division. See Section 2.12 for details. The following table lists frequently used SAS/IML operators:

Table B.1. Frequently Used SAS/IML Operators

Statement

Description

&

Logical AND

|

Logical OR

^

Logical negation

-

Elementwise unary negation

+

Elementwise addition

-

Elementwise subtraction

#

Elementwise multiplication

/

Elementwise division

##

Elementwise power

*

Matrix multiplication

**

Matrix power

`

Matrix transpose

||

Horizontal concatenation of matrices

//

Vertical concatenation of matrices

A complete list of operators is available in the Chapter 23, "Language Reference" (SAS/IML User's Guide).

The SAS/IML language does not have bitwise logical operators; use the BAND, BNOT, BOR, and BXOR functions for bitwise operations.

B.3 A Summary of Functions and Subroutines

This section lists many of the SAS/IML functions and subroutines that are used in statistical data analysis. For a complete list of functions, see the "Overview" section of the Chapter 23, "Language Reference" (SAS/IML User's Guide).

B.3.1 Mathematical Functions

The SAS/IML language contains all of the standard mathematical functions that are available in the SAS DATA step. If you call one of these functions with a matrix argument, the function returns a matrix of values. For example, the following statement applies the SQRT function to every element of a 1 × 4 matrix:

x = {1 4 9 16};
s = sqrt(x);

Table B.2 indicates a few of the many mathematical functions that are available in the SAS/IML language.

Table B.2. Mathematical Functions

Function

Description

ARCOS, ARSIN, ATAN,

Inverse trigonometric functions

COS, SIN, TAN,

Trigonometric functions

EXP, LOG, SQRT,

Basic mathematical functions

INT, FLOOR, CEIL,

Truncation functions

B.3.2 Probability Functions

Table B.3 indicates a few of the probability functions that are available in the SAS/IML language.

Table B.3. Probability Functions and Subroutines

Function

Description

CDF

Returns a value from a cumulative probability distribution

NORMAL

Generates pseudorandom numbers from a normal distribution

PDF

Returns a value from a probability density distribution

QUANTILE

Returns a quantile from a distribution

RANDGEN

Call Generates pseudorandom numbers from a distribution

RANDSEED

Call Specifies the sequence for the RANDGEN subroutine

UNIFORM

Generates pseudorandom numbers from a uniform distribution

You can use the NORMAL and UNIFORM functions for generating small samples. However, the RANDGEN subroutine implements a Mersenne-Twister random number generator that is preferred by experts in statistical computation. You should use the RANDGEN subroutine when you intend to generate millions of random numbers.

B.3.3 Descriptive Statistical Functions

Several functions for descriptive statistics were introduced in SAS/IML 9.22: COUNTMISS, COUNTN, MEAN, PROD, QNTL, and VAR. Table B.4 lists some of the SAS/IML functions and subroutines for computing descriptive statistics.

Table B.4. Descriptive Statistical Functions and Subroutines

Function

Description

COUNTMISS

Returns the number of missing elements

COUNTN

Returns the number of nonmissing elements

CUSUM

Returns the cumulative sum of the elements

MIN, MAX

Returns the minimum or maximum element

MEAN

Returns various means of the elements

PROD

Returns the product of the elements

QNTL Call

Returns quantiles of the elements

RANK

Returns the ranks of the elements

RANKTIE

Returns the tied ranks of the elements

SSQ

Returns the sum of the squares of elements

SUM

Returns the sum of the elements

VAR

Returns the variance of the elements

B.3.4 Matrix Query Functions

A matrix query function gives you information about data in a matrix. What are the dimensions of the matrix? Are the data numerical or character? Are any elements nonzero? What are the locations of the nonzero elements? Table B.5 lists some of the matrix query functions.

Table B.5. Matrix Query Functions

Function

Description

ALL

Returns 1 if all elements are nonzero

ANY

Returns 1 if any element is nonzero

CHOOSE

Returns a value based on a criterion evaluated for each observation

LENGTH

Returns a size for each element in a character matrix. For English characters, this size corresponds to the number of characters in each element.

LOC

Returns the indices that satisfy a criterion

NCOL, NROW

Returns the number of columns or rows

NLENG

Returns a scalar number that indicates the size (in bytes) of the elements of a matrix

TYPE

Returns the type of matrix: 'C', 'N', 'U'

B.3.5 Matrix Reshaping Functions

Table B.6 lists functions that create, resize, or reshape a matrix. See Chapter 2, "Getting Started with the SAS/IML Matrix Programming Language," for details.

Table B.6. Functions That Create and Reshape Matrices

Function

Description

DIAG

Creates a diagonal matrix

DO

Creates an arithmetic sequence

I

Creates an identity matrix

INSERT

Inserts rows or columns into a matrix

J

Creates a matrix with identical values

REMOVE

Removes rows or columns from a matrix

REPEAT

Creates a matrix with repeated values

SHAPE

Reshapes a matrix

T

Transposes a matrix

VECDIAG

Returns a vector that contains the diagonal elements of a matrix

B.3.6 Linear Algebra Functions

Table B.7 lists a few functions that are useful for performing linear algebraic operations such as solving a system of linear equations. The SAS/IML language contains many other advanced functions for matrix computations.

Table B.7. Linear Algebra Functions and Subroutines

Function

Description

DET

Returns the determinant

EIGEN Call

Computes eigenvectors and eigenvalues

GINV

Returns a generalized inverse for a matrix

INV

Returns the inverse for a nonsingular matrix

QR Call

Computes the QR decomposition of a matrix

ROOT

Computes the Cholesky decomposition of a symmetric positive definite matrix

SOLVE

Returns the solution to a linear system of equations

SVD Call

Computes the singular value decomposition

TRACE

Returns the sum of the diagonal elements

B.3.7 Set Functions

Table B.8 lists functions that are useful for finding the union and intersection of sets of values. See Section 2.11 for details.

Table B.8. Functions for Set Operations

Function

Description

SETDIF

Returns the elements of a set that are not in another set

UNION

Returns the elements in the union of sets

UNIQUE

Returns a sorted list of unique values

XSECT

Returns the elements in the intersection of sets

B.3.8 Formatting Functions

Table B.9 lists functions that are useful for formatting data and for manipulating character strings. Many of these functions are documented in the SAS Language Reference: Dictionary.

Table B.9. Formatting Functions

Function

Description

CHAR

Returns the result of applying a w.d format to a numeric matrix

NUM

Returns a numeric matrix from a character string that contain numbers

PUTC

Returns the result of applying a format to a character matrix

PUTN

Returns the result of applying a format to a numeric matrix

STRIP

Removes leading and trailing blanks from a character string

TRIM

Removes trailing blanks from a character string

LEFT

Left-aligns a character string

RIGHT

Right-aligns a character string

B.3.9 Module Statements

A module is a user-defined function or subroutine. Table B.10 lists statements that are useful for defining, storing, and loading modules. The mechanism for storing and loading IMLPlus modules is different than the PROC IML mechanism, as explained in Section 5.7.

Table B.10. Module Statements

Statement

Description

FINISH

Ends the definition of a module

LOAD

Loads the definition of a previously stored module

START

Begins the definition of a module

STORE

Stores a defined module

B.3.10 Control Statements

A control statement enables you to iterate and to conditionally execute certain statements. See Section 2.8 for more information. Table B.11 lists some of the important control statements.

Table B.11. Control Statements

Statement

Description

DO

Iterates over a series of statements by incrementing a counter

DO/UNTIL

Iterates over a series of statements until a condition is satisfied

DO/WHILE

Iterates over a series of statements while a condition is satisfied

IF-THEN/ELSE

Tests a condition and executes certain statements depending on whether the condition is true or false

SUBMIT-ENDSUBMIT

Send statements to the SAS System or to R

The SUBMIT and ENDSUBMIT statements were introduced in SAS/IML 9.22.

B.3.11 Statements for Reading and Writing SAS Data Sets

You can read data from a SAS data set into SAS/IML vectors. You can also read numerical (or character) data into a matrix. You can create SAS data sets from data that are in SAS/IML matrices. Table B.12 lists some of the statements that are useful for reading and writing data sets. See Chapter 3, "Programming Techniques for Data Analysis," for details.

Table B.12. Statements for Reading and Writing Data

Statement

Description

APPEND

Writes data from matrices

CLOSE

Closes a SAS data set after reading or writing

CREATE

Creates a SAS data set for writing

READ

Reads data into matrices

USE

Opens a SAS data set for reading

B.3.12 Options for Printing Matrices

You can print the values of one or more matrices by using the PRINT statement. You can list multiple matrices on a single print statement. If you separate the matrices with commas, each matrix is printed on a separate row. Table B.13 lists optional arguments that you can use to print matrices with headers or labels, or to format the data. See Section 2.2.1 for details.

Table B.13. Options to the PRINT Statement

Option

Description

COLNAME=

Specifies a character matrix that contains labels for each column

ROWNAME=

Specifies a character matrix that contains labels for each row

FORMAT=

Specifies a SAS format to use when displaying the data

LABEL=

Specifies a label for the matrix

..................Content has been hidden....................

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