171
SAS Functions Pocket Reference Chris Fehily Questing Vole Press San Francisco

32883311 Sas Functions Pocketref

  • Upload
    -

  • View
    54

  • Download
    18

Embed Size (px)

DESCRIPTION

d

Citation preview

Page 1: 32883311 Sas Functions Pocketref

SAS Functions Pocket Reference

Chris Fehily

Questing Vole PressSan Francisco

Page 2: 32883311 Sas Functions Pocketref

SAS Functions Pocket Referenceby Chris Fehily

Copyright © 2006 by Chris Fehily. All rights reserved.

Published by Questing Vole PressSan Francisco, California

Editor: Diane YeeInterior Design: Rad Proctor and Chris FehilyCover Design: k.a. salvoCompositor: Chris Fehily

For corrections go to www.fehily.com

Printed in the United States of America

ISBN 0-9785907-0-8

Page 3: 32883311 Sas Functions Pocketref

| iii

iContents

Chapter 1 Using Functions 1Introduction 1Typographic Conventions 1About Functions 2Functions by Category 5

Chapter 2 List of Functions 9

Page 4: 32883311 Sas Functions Pocketref
Page 5: 32883311 Sas Functions Pocketref

1

Chapter 1 1Using Functions

IntroductionLike William Goldman’s The Princess Bride, this book is the “goodparts” version of the official SAS 9.1 function reference. I’vestripped that creature to its essentials: Every function (even theobscure ones) is covered and each entry gives the function’s syn-tax, purpose, and required and optional arguments. In most casesyou’ll also find examples, cross-references to related functions,and other odds and ends.

I expect that you’ll use this book like I do: Pick it up while you’reprogramming, flip to whatever you need reminding of, and thentoss it aside without breaking stride. To this end, I give parameternames that are self-descriptive (in binomial functions the proba-bility of success is prob_success, not p) and consistent (str alwaysis a string, char a character, x a floating-point number, n an inte-ger, and so on).

Typographic ConventionsItalic type introduces new terms or represents replaceable vari-ables in regular text.

Monospace type denotes SAS code in examples and regular text.It also shows filenames, directory (folder) names, and the outputfrom programs or commands.

Italic monospace type denotes a variable in SAS code that youmust replace with a value. You’d replace filename with the nameof a real file, for example.

Bold monspace type highlights SAS code fragments and resultsthat are explained in the accompanying text.

Page 6: 32883311 Sas Functions Pocketref

2 | SAS Functions Pocket Reference

About FunctionsA function is a named routine that performs a computation on itsarguments and returns a value. Functions typically are used inDATA-step statements but they also can appear in SQL, WHEREexpressions, PROC REPORT, and some statistical procedures.You can use %sysfunc or %qsysfunc to invoke a function in amacro statement.

The syntax of a function is

function(arg1,arg2,...)

where function is the function’s name and each argument is a vari-able name, constant, or any expression, including another func-tion. Separate multiple arguments by commas. Most functionstake user-supplied arguments but a few take none because theyget their information from SAS or the operating system; for no-argument functions specify parentheses with nothing between.The following table lists the special symbols that I use for argu-ments in function syntax.

Characters Meaning| A vertical bar or pipe separates alternative arguments.

You can choose exactly one of the given items. (Don’t type the vertical bar.) A|B|C is read “A or B or C.” Don’t confuse the pipe symbol with the double-pipe symbol, ||, which is SAS’s string-concatenation operator.

[] Brackets enclose one or more optional arguments. (Don’t type the brackets.) [A|B|C] means “type A or B or C or type nothing.” [D] means “type D or type nothing.”

[ [ []]] Nested brackets show the hierarchy of two or more optional arguments. To specify any item, you also must specify all the bracketed items to its left. [A[B[C]]] means you can type A without typing B and C, but you can’t type B without typing A too. Likewise, you can’t type C without typing A and B also.

... Ellipses mean that the preceding item(s) can be repeat-ed any number of times.

Page 7: 32883311 Sas Functions Pocketref

Using Functions | 3

Return ValuesFunctions return either a numeric or character value. If youassign a returned character value to a variable, that variable (typi-cally created with a LENGTH statement) should be long enoughto hold the result without truncation. The returned value’s defaultlength either is derived from the arguments or is set to an arbi-trary fixed length that’s usually 200 or fewer characters.

ErrorsIf any argument’s value is invalid, SAS logs an error message andthe function returns a missing value (numeric or character). Theusual reasons for a bad argument are a value that’s out of range (aprobability that’s less than zero or greater than one, for example),a wrong type (a number instead of a string, or vice versa), or amissing value where a nonmissing one is needed.

Call RoutinesI use the term function to refer to both functions and call routinesunless the distinction is important. A call routine is almost thesame as a function except that, instead of returning a result, itchanges the values of some or all of the variables used as its argu-ments. (If you’ve programmed in other languages, you know thisbehavior as call-by-reference. Functions, which never alter theirarguments, are call-by-value.) The syntax of a call routine is

call routine(arg1,arg2,...);

where routine is the call routine’s name. The argument(s) that theroutine puts its results in must be variables of appropriate typeand length, not constants or complex expressions. Note the ter-minating semicolon—a call routine is a standalone statement thatcan’t be nested or used in an assignment statement. You can use%syscall to invoke a call routine in a macro statement.

Variable ListsIn functions or call routines that take variables as arguments (mostdescriptive-statistics functions, for example) you can use OF, -,

Page 8: 32883311 Sas Functions Pocketref

4 | SAS Functions Pocket Reference

and other keywords and operators to specify a shorthand list ofvariables rather than typing each variable explicitly. The follow-ing table shows some examples of function calls.

In this function call The arguments aresum(3,-4.5,6e-2,3Fx) Numeric constants

cat('a',"12") Character constants

sum(4,9,3,8,.,x) Numeric constants, a numeric missing value, and the variable x

cat('a',"12",' ',s) Character constants, a character missing value, and the variable s

sum(x1,x2,x3) The variables x1, x2, and x3

sum(OF x y z) The variables x, y, and z

sum(OF x1-x3) The variables x1, x2, and x3

sum(OF x:) All variables whose names begin with ‘x’

sum(OF item:) All variables whose names begin with ‘item’

sum(OF x1-x3,y1) The variables x1, x2, x3, and y1

sum(OF x1-x3 y1-y3) The variables x1, x2, x3, y1, y2, and y3

sum(OF x1-x3,OF y1-y3)

The same as sum(OF x1-x3 y1-y3)

sum(OF x1-x3,y1-y3) The variables x1, x2, x3, and y1 minus y3

sum(OF x1--y3) All variables between x1 and y3 inclusive, determined by their order in the dataset (available from PROC CONTENTS by using the VARNUM option)

sum(x1--y3) x1 minus negative y3

sum(OF a{*}) All elements of array a

sum(OF a{1}-a{3}) Illegal (not the array elements 1, 2, and 3)

sum(OF _NUMERIC_) All numeric variables in the DATA step

cat(OF _CHARACTER_) All character variables in the DATA step

sum(OF _ALL_) All variables in the DATA step

today() No arguments

Page 9: 32883311 Sas Functions Pocketref

Using Functions | 5

Functions by Category

Category FunctionsArray dim, hbound, lbound

Bitwise logicaloperations

band, blshift, bnot, bor, brshift, bxor

Characterstringmatching

call prxchange, call prxdebug, call prxfree, call prxnext, call prxposn, call prxsubstr, call rxchange, call rxfree, call rxsubstr, prxchange, prxmatch, prxparen, prxparse, prxposn, rxmatch, rxparse

Character any<chartype>, byte, call cats, call catt, call catx,call compcost, call missing, call scan, call scanq, cat, cats, catt, catx, choosec, choosen, coalescec, collate, compare, compbl, compged, complev, compress, count, countc, dequote, find, findc, ifc, ifn, index, indexc, indexw, kcvt, left, length, lengthc, lengthm, lengthn, lowcase, missing, nltime, not<chartype>, nvalid, propcase, quote, rank, repeat, reverse, right, scan, scanq, soundex, spedis, strip, subpad, substr, substrn, translate, trantab, tranwrd, trim, trimn, upcase, verify

Currency conversion

eurocurr

DBCS kcompare, kcompress, kcount, kcvt, kindex, kindexc, kleft, klength, klowcase, kreverse, kright, kscan, kstrcat, ksubstr, ksubstrb, ktranslate, ktrim, ktruncate, kupcase, kupdate, kupdateb, kverify

Date and time datdif, date, datejul, datepart, datetime, day, dhms, hms, hour, intck, intnx, juldate, juldate7, mdy, minute, month, nldate, nldatm, nltime, qtr, second, time,timepart, today, week, weekday, year, yrdif, yyq

Descriptivestatistics

css, cv, geomean, geomeanz, harmean, harmeanz, iqr, kurtosis, largest, mad, max, mean, median, min,missing, n, nmiss, ordinal, pctl, range, rms, skewness, smallest, std, stderr, sum, uss, var

Page 10: 32883311 Sas Functions Pocketref

6 | SAS Functions Pocket Reference

External files dclose, dcreate, dinfo, dnum, dopen, doptname,doptnum, dread, dropnote, fappend, fclose, fcol,fdelete, fexist, fget, fileexist, filename, fileref, finfo, fnote, fopen, foptname, foptnum, fpoint, fpos, fput, fread, frewind, frlen, fsep, fwrite, mopen, pathname, sysmsg, sysrc

Externalroutines

call module, call modulei, modulec, moduleic, modulein, modulen

Financial compound, convx, convxp, daccdb, daccdbsl, daccsl, daccsyd, dacctab, depdb, depdbsl, depsl, depsyd, deptab, dur, durp, intrr, irr, mort, netpv, npv, pvp, saving, yieldp

Hyperbolic cosh, sinh, tanh

Macro call execute, call symput, call symputx, resolve, symexist, symget, symglobl, symlocal

Mathematical abs, airy, beta, call allperm, call logistic, call softmax, call stdize, call tanh, cnonct, coalesce, comb, constant, dairy, deviance, digamma, erf, erfc, exp, fact, fnonct, gamma, ibessel, jbessel, lgamma, log, log10, log2,logbeta, mod, modz, perm, sign, sqrt, tnonct,trigamma

Probability cdf, logcdf, logpdf, logsdf, pdf, poisson, probbeta, probbnml, probbnrm, probchi, probf, probgam,probhypr, probmc, probnegb, probnorm, probt, sdf

Quantile betainv, cinv, finv, gaminv, probit, quantile, tinv

Randomnumber

call ranbin, call rancau, call ranexp, call rangam,call rannor, call ranperk, call ranperm, call ranpoi,call rantbl, call rantri, call ranuni, call streaminit, normal, ranbin, rancau, rand, ranexp, rangam, rannor, ranpoi, rantbl, rantri, ranuni, uniform

SAS file I/O attrc, attrn, cexist, close, curobs, dropnote, dsname, exist, fetch, fetchobs, getvarc, getvarn, iorcmsg,libname, libref, note, open, pathname, point, rewind, sysmsg, sysrc, var<attr>, varnum

Category Functions

Page 11: 32883311 Sas Functions Pocketref

Using Functions | 7

Special addr, addrlong, call poke, call pokelong, call sleep, call system, dif, getoption, input, inputc, inputn, lag, peek, peekc, peekclong, peeklong, ptrlongadd, put, putc, putn, sleep, sysget, sysparm, sysprocessid, sysprocessname, sysprod, system, uuidgen

State andzip code

fipname, fipnamel, fipstate, stfips, stname, stnamel, zipcity, zipfips, zipname, zipnamel, zipstate

Trigonometric arcos, arsin, atan, atan2, cos, sin, tan

Truncation ceil, ceilz, floor, floorz, fuzz, int, intz, round, rounde, roundz, trunc

Variablecontrol

call label, call set, call vname

Variableinformation

call vnext, var<attr>, v<attr>

Web tools htmldecode, htmlencode, urldecode, urlencode

Category Functions

Page 12: 32883311 Sas Functions Pocketref
Page 13: 32883311 Sas Functions Pocketref

List of Functions | 9

Chapter 2 2List of Functions

This chapter lists all SAS functions and call routines alphabetically.Those introduced in SAS 9.0 or later are marked 9+.

Page 14: 32883311 Sas Functions Pocketref

abs

10 | SAS Functions Pocket Reference

abs(x)

Returns the absolute value of x; that is, the nonnegative num-ber that has the same magnitude as x. Examples: abs(2.2) →2.2. abs(0/6) → 0. abs(-6) → 6. See also: sign (p. 146).

addr(var)

Returns the integer memory address of a variable on 32-bitsystems. Favor addrlong (p. 10) over addr because addrlongworks on both 32-bit and 64-bit systems. See also: call poke(p. 22), peek (p. 114), peekc (p. 114).

addrlong(var)

Returns the memory address of a variable as a binary value on32-bit or 64-bit systems.

Example:

Prints the hex-formatted memory address of the variable xdata _null_;x=1234;addr=addrlong(x);put addr= $hex16.;

run;

See also: addr (p. 10), call pokelong (p. 22), peekclong (p. 114), peeklong (p. 115), ptrlongadd (p. 125).

airy(x)

Returns the value of the Airy function Ai(x), which is a solu-tion to the differential equation and defined forreal values of x by the integral

.

Examples: airy(-2.338107410459) → 5.378084E-13 (approx-imate root). airy(2) → 0.0349241304. See also: dairy (p. 54).

any<chartype>(str[,start_pos]) 9+Group of functions that searches a string for a type of charac-ter and returns the first position at which it’s found, or zero(0) if it’s not found. start_pos is the search’s start position anddirection. start_pos < 0 searches backward (right-to-left). If

y″ xy– 0=

Ai x( ) 1π---

t3

3---- xt+⎝ ⎠⎛ ⎞ tdcos

0

∫=

Page 15: 32883311 Sas Functions Pocketref

any<chartype>

List of Functions | 11

start_pos is less than the negative length of str, the searchbegins at the end of str. start_pos defaults to 1, the first charac-ter in str. If start_pos = 0 or start_pos > the length of str, theresult is zero. Replace <chartype> with one of the values listedin the following table.

The functions anyalnum, anyalpha, anygraph, anylower,anyprint, anypunct, anyspace, and anyupper depend on theTRANTAB=, ENCODING=, and LOCALE= dataset and sys-tem options.

Examples:

s='ABC.123 _xyZ_';

anydigit(s) → 5.

anydigit(s,0) → 0.

chartype Searches for aalnum Alphanumeric character

alpha Alphabetic character

cntrl Control character

digit Digit

first Character that is valid as the first character in a SAS variable name under VALIDVARNAME=V7

graph Graphical character

lower Lowercase letter

name Character that is valid in a SAS variable name under VALIDVARNAME=V7

print Printable character

punct Punctuation character

space Whitespace character (space, horizontal and verti-cal tab, carriage return, line feed, form feed)

upper Uppercase letter

xdigit Hexadecimal character that represents a digit

Page 16: 32883311 Sas Functions Pocketref

arcos

12 | SAS Functions Pocket Reference

anyupper(s,4) → 12.

anyupper(s,-4) → 3.

anyalpha(s,999) → 0.

anyalpha(s,-999) → 12.

anycntrl(s) → 0.

See also: not<chartype> (p. 110).

arcos(x)

Returns the arccosine (inverse cosine) of x (-1 ≤ x ≤ 1); that is,the angle in radians whose cosine is x. Example: arcos(-1) →3.14159 (π radians). See also: cos (p. 51).

arsin(x)

Returns the arcsine (inverse sine) of x (-1 ≤ x ≤ 1); that is, theangle in radians whose sine is x.

Examples:

arsin(1) → 1.5708 (π/2 radians).

arsin(2) → . (missing—invalid x).

Create an inverse trig tabledata inv_trig_table;if _N_=1 then pi=constant('pi');retain pi;do x=-1 to 1 by 0.1;arcsin_radians=arsin(x);arcsin_degrees=arcsin_radians * 180/pi;arccos_radians=arcos(x);arccos_degrees=arccos_radians * 180/pi;output;

end;drop pi;

run;

See also: sin (p. 146).

atan(x)

Returns the two-quadrant arctangent (inverse tangent) of x;that is, the angle in radians whose tangent is x and whose val-

Page 17: 32883311 Sas Functions Pocketref

attrc

List of Functions | 13

ue is between -π/2 and π/2. Examples: atan(0) → 0. atan(-1)→ -0.7854 (-π/4 radians). See also: atan2 (p. 13), tan (p. 155).

atan2(x,y) 9+Returns the arctangent (inverse tangent) of the coordinates xand y. The returned angle is expressed in radians and isbetween -π and π. The result matches atan(x/y) but thecoordinates’ signs give the quadrant. Examples: atan2(1,1)→ 0.7854 (π/4 radians). atan2(-1,-1) → -2.3562 (-3π/4radians). See also: atan (p. 12), tan (p. 155).

attrc(dataset_id,attr_name)

Returns the value of a character attribute of a SAS dataset.dataset_id is the dataset identifier that open (p. 111) returns.attr_name is one of the values listed in the following table.attrc returns a missing value if attr_name is invalid. Example:attrc(open('mydata.dsn','i'),'mode') → ‘I’. See also:attrn (p. 15).

attr_name Returnscharset Character-set sort order:

Empty string—Dataset isn’t sortedascii—ASCII character setebcdic—EBCDIC character setansi—OS/2 ANSI standard ASCII character setoem—OS/2 OEM code format

encrypt Dataset encryption: yes or no

engine Name of the engine used to access the dataset

label Label assigned to the dataset

lib Libref of SAS data library in which the dataset resides

mem Dataset (member) name

mode The mode in which the dataset was opened; see the following table

mtype SAS data library member type

sortedby Names of BY variables in standard BY-clause format, or an empty string if the dataset isn’t sorted

Page 18: 32883311 Sas Functions Pocketref

attrc

14 | SAS Functions Pocket Reference

If attr_name is 'mode', attrc returns one of the values listedin the following table.

sortlvl Dataset sort type:Empty string—Dataset isn’t sortedweak—Dataset is user-sorted (usually by the SORTEDBY dataset option); SAS can’t validate cor-rectness and so depends on the order of observationsstrong—Dataset is sorted by SAS software (usually by PROC SORT or the OUT= option of PROC CONTENTS)

sortseq An empty string if the dataset is sorted on the native machine or if the sort collating sequence is the default for the OS; otherwise the result is the name of the alternate collating sequence used to sort the file

type SAS dataset type

mode Mode in which the dataset was openedi INPUT mode allows random access if the engine

supports it; otherwise the mode defaults to in

in INPUT mode reads sequentially and allows revisiting observations

is INPUT mode reads sequentially but doesn’t allow revisiting observations

n NEW mode creates a new dataset

u UPDATE mode allows random access if the engine supports it; otherwise the mode defaults to un

un UPDATE mode reads sequentially and allows revisit-ing observations

us UPDATE mode reads sequentially but doesn’t allow revisiting observations

v UTILITY mode allows modification of variable attributes and indexes associated with the dataset

attr_name Returns

Page 19: 32883311 Sas Functions Pocketref

attrn

List of Functions | 15

attrn(dataset_id,attr_name)

Returns the value of a numeric attribute of a SAS dataset.dataset_id is the dataset identifier that open (p. 111) returns.attr_name is one of the values listed in the following table.attrn returns a missing value if attr_name is invalid. Example:put(attrn(open('data.dsn','i'),'modte'),datetime.)→ 29MAR06:08:00:2. See also: attrc (p. 13).

attr_name Returnsalterpw 1 if a password is needed to alter the dataset; 0

otherwiseanobs 1 if the engine knows the number of observa-

tions; 0 otherwiseany|varobs -1 if the dataset has no observations or vari-

ables; 0 if observations don’t exist but variables do; or 1 if both observations and variables exist

arand|random 1 if the engine supports random access; 0 other-wise

arwu 1 if the engine can write (create and update) SAS files; 0 if the engine is read-only

audit 1 if logging to an audit file is turned on; 0 other-wise

audit_data 1 if after-update record images are stored; 0 oth-erwise

audit_before 1 if before-update record images are stored; 0 otherwise

audit_error 1 if unsuccessful after-update record images are stored; 0 otherwise

crdte Dataset creation date, as a SAS datetime (use the DATETIME. format to display this value)

iconst 0 if no dataset integrity constraints exist; 1 if one or more general integrity constraints exist; 2 if one or more referential integrity constraints exist; or 3 if both one or more general integrity constraints and one or more referential integrity constraints exist

Page 20: 32883311 Sas Functions Pocketref

attrn

16 | SAS Functions Pocket Reference

index 1 if the dataset supports indexing; 0 otherwiseisindex 1 if at least one dataset index exists; 0 otherwiseissubset 1 if the dataset is a subset (that is, at least one

WHERE clause is active); 0 otherwiselrecl Logical record lengthlrid Length of the record IDmaxgen Maximum number of generationsmaxrc 1 if an application checks return codes; 0 other-

wisemodte Dataset modification date, as a SAS datetime

(use the DATETIME. format to display this val-ue)

ndel Number of dataset observations marked for deletion

nextgen Next generation number to generatenlobs Number of logical observations (those not

marked for deletion); -1 if unknown. An active WHERE clause doesn’t affect this number.

nlobsf Number of logical observations (those not marked for deletion) after applying the OBS= and FIRSTOBS= system options and WHERE clauses. This attribute forces SAS to scan the entire dataset.

nobs Number of physical observations (including those marked for deletion); -1 if unknown. An active WHERE clause doesn’t affect this num-ber.

nvars Number of variables in the datasetpw 1 if a password is needed to access the dataset; 0

otherwiseradix 1 if access by observation number (radix

addressability) is allowed; 0 otherwisereadpw 1 if a password is needed to read the dataset; 0

otherwise

attr_name Returns

Page 21: 32883311 Sas Functions Pocketref

betainv

List of Functions | 17

band(n,m)

Returns the bitwise AND of two integers (0 ≤ n ≤ 232, 0 ≤ m ≤232). Example: band(10,7) → 2 (1010 & 0111 → 0010). Seealso: bnot (p. 18), bor (p. 18), bxor (p. 18).

beta(a,b) 9+Returns the value of the beta function with shape parametersa (> 0) and b (> 0). The beta function is defined for positivevalues of a and b by the integral

.

Note that B(a,b) = Γ(a)Γ(b)/Γ(a+b) where Γ() is the functiongamma (p. 80).

Examples:

beta(4,2) → 0.05.

beta(2,1) → 0.5.

beta(1,2) → 0.5.

beta(5,2) → 0.0333333333.

beta(5,-2) → . (missing—bad b).

See also: betainv (p. 17), logbeta (p. 100), probbeta (p. 116).

betainv(prob,a,b)

Returns the prob-th quantile from the beta distribution withshape parameters a (> 0) and b (> 0). The probability that an

tape 1 if the dataset is a sequential file; 0 otherwisewhstmt 0 if no WHERE clause is active; 1 if a perma-

nent WHERE clause is active; 2 if a temporary WHERE clause is active; or 3 if both permanent and temporary WHERE clauses are active

writepw 1 if a password is needed to write to the dataset; 0 otherwise

attr_name Returns

B a b,( ) xa 1– 1 x–( )b 1– xd0

1

∫=

Page 22: 32883311 Sas Functions Pocketref

blshift

18 | SAS Functions Pocket Reference

observation from a beta distribution is less than or equal tothe returned quantile is prob (0 ≤ x ≤ 1). betainv is theinverse of probbeta (p. 116). Example: betainv(0.04,2,1) →0.2. See also: beta (p. 17).

blshift(n,shift)

Left-shifts the integer n by shift bits (0 ≤ n ≤ 232; 0 ≤ shift ≤ 31).Example: blshift(7,3) → 56 (00000111 << 3 → 00111000).See also: brshift (p. 18).

bnot(n)

Returns the bitwise NOT of an integer (0 ≤ n ≤ 232). Example:bnot(0F000000Fx) → 0FFFFFF0 (hex). See also: band (p. 17),bor (p. 18), bxor (p. 18).

bor(n,m)

Returns the bitwise OR of two integers (0 ≤ n ≤ 232, 0 ≤ m ≤232). Example: bor(10,7) → 15 (1010 | 0111 → 1111). See also:band (p. 17), bnot (p. 18), bxor (p. 18).

brshift(n,shift)

Right-shifts the integer n by shift bits (0 ≤ n ≤ 232; 0 ≤ shift ≤31). Example: brshift(7,3) → 0 (0111 >> 3 → 0000). See also:blshift (p. 18).

bxor(n,m)

Returns the bitwise EXCLUSIVE OR of two integers (0 ≤ n ≤232, 0 ≤ m ≤ 232). Example: bxor(10,7) → 13 (1010 ^ 0111 →1101). See also: band (p. 17), bnot (p. 18), bor (p. 18).

byte(n)

Returns the n-th character (0 ≤ n ≤ 255) in the ASCII orEBCDIC collating sequence. The result’s default length is one.ASCII characters 0–127 are the standard set; characters 128–255 vary depending on your OS. Examples: byte(80) → ‘P’(ASCII). byte(80) → ‘&’ (EBCDIC). See also: collate (p. 45),rank (p. 131).

call allperm(index,var1[,var2,...]); 9+Generates a permutation of the variables var1, var2,..., varn(n ≤ 18), which must be all the same type and, if character,same length.

Page 23: 32883311 Sas Functions Pocketref

call compcost

List of Functions | 19

Example:

Generate and print all permutations of the elements of ararray ar [4] $1 ('a' 'b' 'c' 'd');n=dim(ar);nfact=fact(n);do i=1 to nfact;call allperm(i,of ar[*]);put i 5. +2 ar[*];

end;

See also: call ranperk (p. 28), call ranperm (p. 28), fact (p. 66), perm (p. 115).

call cats(result,str1[,str2,...]); 9+Concatenates strings, removing leading and trailing spaces,returning the result in the variable result. If result is too smallto contain the concatenated string, SAS

• logs a warning that the result was truncated• logs a note showing the function call’s location and the

argument that caused the truncation, except when called in SQL or in a WHERE clause

• sets _ERROR_ to 1 in the DATA step, except in a WHERE clause.

See also: cat (p. 38), cats (p. 40).

call catt(result,str1[,str2,...]); 9+Concatenates strings, removing trailing spaces, returning theresult in the variable result. See call cats (p. 19) for errorconditions. See also: cat (p. 38), catt (p. 40).

call catx(sep,result,str1[,str2,...]); 9+Concatenates strings, removing leading and trailing spacesand inserting separators between strings, returning the resultin the variable result. sep usually is a space or a comma but canbe any string. See call cats (p. 19) for error conditions. Seealso: cat (p. 38), catx (p. 40).

call compcost(op1=,cost1[,op2=,cost2,...]); 9+Sets the costs of operations for later use by compged (p. 47).Each op is followed by its integer cost (-32767 to 32767). If an

Page 24: 32883311 Sas Functions Pocketref

call execute

20 | SAS Functions Pocket Reference

operation doesn’t appear in a compcost call or its cost is amissing value, that operation’s default cost is used. The speci-fied costs remain in effect until the next compcost call or untilthe calling step ends. The following table lists each operationand its default cost. You can abbreviate each op to the numberof letters needed to identify it uniquely. Spaces are ignored.Example: call compcost('insert=',10,'de=',25);

call execute(str);

Resolves a string and executes it. str is a quoted string, anunquoted DATA-step character variable, or a characterexpression that resolves to a macro text expression or SASstatement. If str is a macro invocation, DATA-step executionpauses and the macro executes immediately. If str is a SASstatement or if macro-execution generates SAS statements,the statement(s) execute after the calling DATA-step ends. Fordetails see Execute in SAS Macro Language: Reference.

call label(var,char_var);

Assigns a variable’s label to a character variable. var is any SASvariable. If var has no label, var’s name is assigned to char_var.char_var is any SAS character variable. Labels can be up to256 characters long; make the length of char_var long enoughto avoid truncation. See also: v<attr> (p. 161).

call logistic(var1[,var2,...]); 9+Replaces each numeric var by its logistic value, defined by theequation ex/(1+ex). Use only variables as arguments, not con-

op Default cost op Default costappend= 50 insert= 100

blank= 10 match= 0

delete= 100 punctuation= 30

double= 20 replace= 100

fdelete= 200 single= 20

finsert= 200 swap= 20

freplace= 200 truncate= 10

Page 25: 32883311 Sas Functions Pocketref

call module

List of Functions | 21

stants or expressions. If any argument is a missing value, miss-ing values are returned for all arguments. Example: x=.5; y=0;call logistic(x,y); → Replaces x with 0.6224593312 and ywith 0.5.

call missing(var1[,var2,...]); 9+Assigns a missing value to the specified character or numericvariables. call missing allows mixed character and numericvars. It assigns a numeric missing value (.) to each numericvar and a character missing value (a space) to each charactervar.

Examples:

n1=3; n2=4; c1='abc';

call missing(n1,c1) → n1=., n2=4, c1=‘ ’.

call missing(of _all_) → n1=., n2=., c1=‘ ’.

See also: missing (p. 102).

call module([ctrl_str,]module_name[,arg1,arg2,...]);

Calls an external routine without any return code.

ctrl_str is an optional control string: an asterisk (*) followedby any combination of the characters in the following table.

module_name is the name of the external module to call.

arg is one or more arguments to pass to module_name. Usingthe wrong arguments can cause SAS or your OS to crash.

ctrl_str Meansi Print hex representations of all call module arguments.

Use i for debugging. Using i implies e.

e Print detailed error messages. Without e (or i) call module generates only “Invalid argument to function”. Use e for production environments.

h Provide help about call module syntax, the attribute file format, and suggested formats and informats

Page 26: 32883311 Sas Functions Pocketref

call modulei

22 | SAS Functions Pocket Reference

Example:

Call the xyz routineroutine xyz minarg=2 maxarg=2;arg 1 input num byvalue format=ib4.;arg 2 output char format=$char10.;data _null_;call module('xyz',1,x);put x=;

run;

See also: call modulei (p. 22), modulec (p. 104), moduleic (p. 104), modulein (p. 104), modulen (p. 104).

call modulei([ctrl_str,]module_name[,arg1,arg2,...]);

Calls an external routine without any return code (in IML environment only). For details see call module (p. 21). See also: moduleic (p. 104), modulein (p. 104).

call poke(source,pointer[,len]);

Same as call pokelong (p. 22) but for 32-bit systems only.See also: addr (p. 10), peek (p. 114), peekc (p. 114).

call pokelong(source,pointer[,len]);

Writes a value directly into memory on 32-bit or 64-bit sys-tems. source is a string that contains a value to write. pointer isa string that contains the virtual address of the data to over-write. len is the number of bytes to write from source to thepointer address. Omit len to copy the entire value of source. Seealso: addrlong (p. 10), call poke (p. 22), peekclong (p. 114),peeklong (p. 115).

call prxchange(prx_id,n,old_str[,new_str[,result_len[,trunc_flag[,num_changes]]]]); 9+

Matches and replaces a pattern.

prx_id is the pattern identifier that prxparse (p. 121) returns.

n is the number of times to search for a match and replace amatching pattern. If n = -1, all matching patterns are replaced.

old_str is the character expression on which to perform asearch and replace. Omit new_str to make changes directly toold_str.

Page 27: 32883311 Sas Functions Pocketref

call prxfree

List of Functions | 23

new_str is a character variable where the result of the changesto old_str are placed. old_str isn’t changed if new_str is speci-fied.

result_len returns the number of characters that are copiedinto the result. Trailing spaces in old_str aren’t copied tonew_str, and so aren’t included in result_len.

trunc_flag returns 0 if the entire replacement result is shorterthan new_str, or 1 if it’s longer than new_str.

num_changes returns the total number of replacements made.If the result is truncated when placed into new_str, the valueof num_changes doesn’t changed.

Example:

Prints new_str=The woods have a tree, tree, tree, and a tree.len=50. trunc_flag=0. num_changes=4.data _null_;length old_str $ 46;length new_str $ 100;length len trunc_flag num_changes 8;prx_id=prxparse('s/[crb]at/tree/');old_str='The woods have a bat, cat, bat, and a rat.';

call prxchange(prx_id,-1,old_str,new_str,len,trunc_flag,num_changes);

put new_str= len= trunc_flag= num_changes=;run;

See also: prxchange (p. 120).

call prxdebug(on_off); 9+Lets DATA-step Perl regular expressions log debug output. Seton_off to nonzero to turn debugging on or to zero to turn itoff. call prxdebug prints messages in the SAS log about howPerl regular expressions are compiled and pattern-matchedwhen calling PRX functions. See also: prxparse (p. 121).

call prxfree(prx_id); 9+Frees memory allocated to a Perl regular expression. prx_id isthe pattern identifier that prxparse (p. 121) returns. Example:See prxparen (p. 121).

Page 28: 32883311 Sas Functions Pocketref

call prxnext

24 | SAS Functions Pocket Reference

call prxnext(prx_id,start_pos,stop_pos,str,pos,len); 9+

Returns the position and length of a substring that matches apattern and iterates over multiple matches within a string.

prx_id is the pattern identifier that prxparse (p. 121) returns.

start_pos is the position at which to start the pattern matchingin str. If the match succeeds, call prxnext changes start_posto pos+max(1,len); otherwise start_pos isn’t changed.

stop_pos is the last character to use in str. If stop_pos is -1, thenthe last character is the last nonblank character in str.

str is the character expression to search.

pos returns the position in str at which the pattern begins. Ifno match is found, pos is zero (0).

len returns the length of the string that is matched by the pat-tern. If no match is found, len is zero (0).

Example:

Find three-letter words starting with ‘c’, ‘r’, or ‘b’ andending with ‘at’ data _null_;prx_id=prxparse('/[crb]at/');str='The woods have a bat, cat, and a rat.';start_pos=1;stop_pos=length(str);call prxnext(prx_id,start_pos,stop_pos,str,pos,len);

do while (pos>0);found=substr(str,pos,len);put found= pos= len= start_pos=;call prxnext(prx_id,start_pos,stop_pos,str,pos,len);

end;run;

The result in the SAS log is

found=bat pos=18 len=3 start_pos=21found=cat pos=23 len=3 start_pos=26found=rat pos=34 len=3 start_pos=37

Page 29: 32883311 Sas Functions Pocketref

call prxposn

List of Functions | 25

call prxposn(prx_id,buffer,start_pos[,len]); 9+Returns the start position and length of a capture buffer. Acapture buffer is part of a match, enclosed in parentheses, in aregular expression. call prxposn doesn’t return the capture-buffer text directly; use substr (p. 151) to return the text. Youcan return a capture buffer by using call prxchange (p. 22),call prxnext (p. 24), call prxsubstr (p. 26), or prxmatch(p. 120).

prx_id is the pattern identifier that prxparse (p. 121) returns.

buffer is a number identifying the capture buffer from whichto retrieve the start position and length. If buffer is zero,call prxposn returns the start position and length of theentire match. If buffer is between 1 and the number of openparentheses, call prxposn returns the start position andlength for that capture buffer. If buffer is greater than thenumber of open parentheses, call prxposn returns missingvalues for start_pos and len.

start_pos returns the position at which the capture buffer isfound. If buffer isn’t found, start_pos is zero. If buffer is greaterthan the number of open parentheses in the pattern, start_posis a missing value.

len returns the pattern length of the previous pattern match. Ifthe pattern match isn’t found, len is zero. If buffer is greaterthan the number of open parentheses in the pattern, len is amissing value.

Example:

Prints hour=09 minute=56 ampm=amdata _null_;prx_id=prxparse('/(\d\d):(\d\d)(am|pm)/'); str='The time is 09:56am.';if prxmatch(prx_id,str) then do;call prxposn(prx_id,1,start_pos,len);hour=substr(str,start_pos,len);call prxposn(prx_id,2,start_pos,len);minute=substr(str, start_pos,len);call prxposn(prx_id,3,start_pos,len);ampm=substr(str,start_pos,len);

Page 30: 32883311 Sas Functions Pocketref

call prxsubstr

26 | SAS Functions Pocket Reference

put hour= minute= ampm=;end;

run;

See also: prxposn (p. 124).

call prxsubstr(prx_id,str,start_pos[,len]); 9+Returns the position and length of a substring that matches apattern, or zero (0) in start_pos and len if no match is found.prx_id is the pattern identifier that prxparse (p. 121) returns.

Example:

Prints start_pos=7 len=5data _null_;prx_id=prxparse('/world/');str='Hello world!';call prxsubstr(prx_id,str,start_pos,len);put start_pos= len=;

run;

See also: prxmatch (p. 120).

call ranbin(seed,num_trials,prob_success,x);

Returns a random variate from a binomial distribution withmean np and variance np(1-p), where n is the integer numberof trials (num_trials ≥ 1) and p is the probability of success(0 < prob_success < 1).

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call ranbin executes.

x is a numeric variable. A new value for the random variate xis returned each time call ranbin executes.

See also: ranbin (p. 128).

call rancau(seed,x);

Returns a random variate from a Cauchy distribution withlocation parameter 0 and scale parameter 1.

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call rancau executes.

Page 31: 32883311 Sas Functions Pocketref

call rannor

List of Functions | 27

x is a numeric variable. A new value for the random variate xis returned each time call rancau executes.

See also: rancau (p. 128).

call ranexp(seed,x);

Returns a random variate from an exponential distributionwith parameter 1.

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call ranexp executes.

x is a numeric variable. A new value for the random variate xis returned each time call ranexp executes.

See also: ranexp (p. 129).

call rangam(seed,a,x);

Returns a random variate from a gamma distribution withshape parameter a (> 0).

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call rangam executes.

x is a numeric variable. A new value for the random variate xis returned each time call rangam executes.

See also: rangam (p. 130).

call rannor(seed,x);

Returns a random variate from a normal distribution withmean 0 and variance 1.

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call rannor executes.

x is a numeric variable. A new value for the random variate xis returned each time call rannor executes.

See also: rannor (p. 131).

Page 32: 32883311 Sas Functions Pocketref

call ranperk

28 | SAS Functions Pocket Reference

call ranperk(seed,k,var1[,var2,...]); 9+Generates a random permutation of k values from the vars,which must be all the same type and, if character, same length.seed is an integer random-number seed (< 231-1). If seed ≤ 0,the time of day is used to initialize the seed stream.

Example:

Generate random permutations of 3 of the values x1-x5data _null_;array ar x1-x5 (1 2 3 4 5);seed=98765;do n=1 to 10;call ranperk(seed,3,of x1-x5);put seed= @20 x1-x3;

end;run;

See also: call allperm (p. 18), call ranperm (p. 28), fact (p. 66), perm (p. 115).

call ranperm(seed,var1[,var2,...]); 9+Generates a random permutation of all values from the vars,which must be all the same type and, if character, same length.seed is an integer random-number seed (< 231-1). If seed ≤ 0,the time of day is used to initialize the seed stream.

Example:

Generate random permutations of the values x1-x4data _null_;array ar x1-x4 (1 2 3 4);seed=98765;do n=1 to 10;call ranperm(seed,of x1-x4);put seed= @20 x1-x4;

end;run;

See also: call allperm (p. 18), call ranperk (p. 28), fact (p. 66), perm (p. 115).

call ranpoi(seed,lambda,x);

Returns a random variate from a Poisson distribution withmean lambda (≥ 0).

Page 33: 32883311 Sas Functions Pocketref

call ranuni

List of Functions | 29

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call ranpoi executes.

x is a numeric variable. A new value for the random variate xis returned each time call ranpoi executes.

See also: ranpoi (p. 131).

call rantbl(seed,prob1,...,probn,x);

Returns a random variate (positive integer) from the proba-bility mass function defined by prob1 through probn. callrantbl returns 1 with probability prob1, 2 with prob2,..., nwith probn, where 0 ≤ probi ≤ 1 for i = 1, 2,..., n. If the probssum to less than one then call rantbl can return n+1; if theysum to greater than one then call rantbl ignores the extraprobs.

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call rantbl executes.

x is a numeric variable. A new value for the random variate xis returned each time call rantbl executes.

See also: rantbl (p. 131).

call rantri(seed,height,x);

Returns a random variate from a triangular distribution onthe interval (0,1) with mode height (0 < height < 1).

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call rantri executes.

x is a numeric variable. A new value for the random variate xis returned each time call rantri executes.

See also: rantri (p. 132).

call ranuni(seed,x);

Returns a random variate from a uniform distribution on theinterval (0,1).

Page 34: 32883311 Sas Functions Pocketref

call rxchange

30 | SAS Functions Pocket Reference

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. A new value of seed is returnedeach time call ranuni executes.

x is a numeric variable. A new value for the random variate xis returned each time call ranuni executes.

See also: ranuni (p. 132).

call rxchange(rx_id,n,old_str[,new_str]);

Replaces a pattern-match n times in old_str, changing old_strin place if new_str is omitted. rx_id is the pattern identifierthat rxparse (p. 135) returns. Example: See rxparse. See also:call rxfree (p. 30),call rxsubstr (p. 30),rxmatch (p. 134).

call rxfree(rx_id);

Frees memory allocated to a SAS regular expression. rx_id isthe pattern identifier that rxparse (p. 135) returns. Example:See rxparse.

call rxsubstr(rx_id,str,start_pos[,len[,score]]);

Returns the position, length, and score of a substring thatmatches a pattern, or zero (0) if no match is found. rx_id isthe pattern identifier that rxparse (p. 135) returns. str is thestring to search. start_pos is the position in str where thematched substring begins. len is the length of the matchedsubstring. score is an integer based on the number of matchesfor a particular pattern in a substring. When a pattern match-es more than one substring beginning at start_pos, the longestsubstring is selected. Example: See rxparse. See also: callrxchange (p. 30), call rxfree (p. 30), rxmatch (p. 134).

call scan(str,n,pos,len[,delims]); 9+Returns the position and length of the n-th word in a string. Ifn < 0, call scan counts words right to left. If n = 0 or |n| > thenumber of words in str, call scan returns zero in pos and len.delims specifies character(s) that separate words. The defaultASCII delimiters are: space . < ( + & ! $ * ) ; ^ - / , % | . Thedefault EBCDIC delimiters are: space . < ( + | & ! $ * ) ; ¬ - / , %| ¢. Contiguous delimiters are treated as one. Leading and

Page 35: 32883311 Sas Functions Pocketref

call set

List of Functions | 31

trailing delimiters are ignored. To extract the desired wordafter calling call scan, use substrn (p. 152).

Examples:

call scan('1 ab xyz',2,pos,len) → pos=3, len=2.

call scan('1 ab xyz',-3,pos,len) → pos=1, len=1.

call scan('1 ab xyz',5,pos,len) → pos=0, len=0.

call scan('1,ab xyz',2,pos,len,',') → pos=3, len=6.

call scan('1,ab xyz',2,pos,len,' ,') → pos=3, len=2.

call scan('123.abc(xyz)',3,pos,len) → pos=9, len=3.

See also: call scanq (p. 31), scan (p. 145), scanq (p. 145).

call scanq(str,n,pos,len[,delims]); 9+Returns the position and length of the n-th word in a string,ignoring quote-enclosed delimiters. If n < 0, call scanq countswords right to left. If n = 0 or |n| > the number of words in str,call scanq returns zero in pos and len. Unmatched quotes instr make left-to-right and right-to-left scans return differentwords. delims specifies character(s) that separate words. Thedefault delimiters are whitespace characters: blank, horizontaland vertical tab, carriage return, line feed, and form feed. Youcan’t use single or double quotes as delimiters. Contiguousdelimiters are treated as one. Leading and trailing delimitersare ignored. To extract the desired word after calling callscanq, use substrn (p. 152).

Examples:

call scanq('12 "a c" xyz',2,pos,len) → pos=4, len=5.

call scanq('12 a c xyz',2,pos,len) → pos=4, len=1.

See also: call scan (p. 30), scan (p. 145), scanq (p. 145).

call set(dataset_id);

Links SAS dataset variables to the DATA-step or macro vari-ables in your program that have the same name and data type.dataset_id is the dataset identifier that open (p. 111) returns.

Page 36: 32883311 Sas Functions Pocketref

call sleep

32 | SAS Functions Pocket Reference

After a call set, whenever a read is performed from the SASdataset, the values of the corresponding macro or DATA-stepvariables are set to the values of the matching SAS datasetvariables. If the variable lengths don’t match, the values aretruncated or padded accordingly. In general, use call setimmediately following open. If you don’t use call set, usegetvarc (p. 81) and getvarn (p. 81).

Example:

Get values for the first 10 observations in sasuser.houses and store them in mydatadata mydata;length style $8 sqfeet bedrooms baths 8 street $16 price 8;

drop rc dataset_id;dataset_id=open("sasuser.houses","i");call set(dataset_id);do i=1 to 10;rc=fetchobs(dataset_id,i);output;

end;run;

See also: fetch (p. 67), fetchobs (p. 68).

call sleep(n[,unit]);

Suspends program execution for n units of time (< 46 days).n ≥ 0 and unit defaults to 0.001 (a millisecond). See sleep(p. 147) for details.

Example: call sleep(6000,0.01) → Sleep for one minute.

call softmax(var1[,var2,...]); 9+Replaces each numeric var by its softmax value (scaled mag-nitude). Each varj is replaced by

.

The softmax values sum to one. Use only variables as argu-ments, not constants or expressions. If any argument is amissing value, missing values are returned for all arguments.

exj e

xi

i 1=

n

∑⁄

Page 37: 32883311 Sas Functions Pocketref

call stdize

List of Functions | 33

Example: x=0; y=1.0; z=-0.5; call softmax(x,y,z); →Replaces x with 0.2312, y with 0.6285, and z with 0.1402.

call stdize([opt1,opt2,...,]x1[,x2,...]); 9+Standardizes the set of numeric x’s according to the specifiedoptions, replacing each x with its standardized value. Stan-dardization subtracts a location measure and divides by ascale measure. Specify options to change the default result:

where result is the final output value returned for each vari-able, add is the constant to add (add=), mult is the constant tomultiply by (mult=), original is the original input value,location is the location measure, and scale is the scale mea-sure. You can replace missing values by any constant; if youdon’t specify missing= or replace, variables that have miss-ing values aren’t changed. The initial estimation method forabw=, ahuber=, and awave= is mad. Percentiles are computedby using definition 5; see pctl (p. 113).

Each opt is a case-insensitive string; leading and trailing spac-es are ignored. Use a separate argument for each opt. An optthat ends with an equal sign is followed by its correspondingvalue: another argument that is a numeric constant, variable,or expression. For details about the options see PROCSTDIZE in SAS/STAT User’s Guide. The default opts are stdand df. The following table lists location and scale options.

opt Optionabw= Tuning constant

agk= Proportion of pairs to be included in the estimation of the within-cluster variances

ahuber= Tuning constant

awave= Tuning constant

euclen Euclidean length

iqr Interquartile range

result add mult original location–( )scale

------------------------------------------------------⎝ ⎠⎛ ⎞×+=

Page 38: 32883311 Sas Functions Pocketref

call stdize

34 | SAS Functions Pocket Reference

The following table lists options that specify the divisor to beused in variance calculations.

The following table lists miscellaneous options.

l= Power (≥ 1) to which differences are to be raised in computing an L(p) or Minkowski metric

mad Median absolute deviation from the median

maxabs Maximum absolute values

mean Arithmetic mean (average)

median Middle number in a set of data that is ordered according to rank

midrange Midpoint of the range

range Range of values

spacing= Proportion of data to be contained in the spacing

std Standard deviation (the default)

sum Result obtained when numbers are added

ustd Unstandardizes variables

opt Optiondf Degrees of freedom (the default)

n Number of observations

opt Optionadd= Number to add to each value after standardizing and

multiplying by the mult= value (default 0)

fuzz= Relative fuzz factor

missing= Value to be assigned to variables that have a missing value

mult= Number to multiply each value by after standardizing (default 1)

opt Option

Page 39: 32883311 Sas Functions Pocketref

call streaminit

List of Functions | 35

Examples:

w=.; x=1; y=2; z=3;

call stdize(x,y,z); → x = -1, y = 0, z = 1.

call stdize('range',w,x,y,z); → w = . (missing), x = 0, y = 0.5, z = 1.

call stdize('mult=',10,'missing=',-1,'range',w,x,y,z); → w = -1, x = 0, y = 5, z = 10.

call streaminit(seed); 9+Sets a seed value that rand (p. 128) uses to generate randomnumbers. seed is an integer (< 231-1). To create reproduciblerandom-number streams, use call streaminit before callingrand. Calling rand before call streaminit (or using callstreaminit with seed ≤ 0) makes rand use the system clockto seed itself.

Example:

Create a reproducible stream of random numbersdata random;call streaminit(12345);do i=1 to 10;x=rand('normal');output;

norm Normalize the scale estimator to be consistent for the standard deviation of a normal distribution (affects only agk=, iqr, mad, and spacing=)

pstat Log the values of the location and scale measures

replace Replace each missing value with 0 in the standard-ized data—this value corresponds to the location measure before standardizing (to replace missing val-ues by other values, use missing=)

snorm Normalize the scale estimator to have an expectation of approximately 1 for a standard normal distribu-tion (affects only spacing=)

opt Option

Page 40: 32883311 Sas Functions Pocketref

call symput

36 | SAS Functions Pocket Reference

end;run;

call symput(macro_var_name,char_value);

Assigns a value to a macro variable. macro_var_name is acharacter expression that identifies the macro variable that’sassigned a value. If the macro variable doesn’t exist, it’s creat-ed. char_value is a character expression that contains theDATA-step information to assign. For more information seeSymput in SAS Macro Language: Reference. See also: callsymputx (p. 36), symget (p. 153).

call symputx(macro_var_name,value[,symbol_table]); 9+Assigns a value to a macro variable, converting a numericvalue to character by using the BEST. format and removingleading and trailing spaces. The macro variable is stored inthe specified symbol_table: g (global table), l (the most localtable that exists, which might be the global table), or f (themost local table in which the macro variable exists, or themost local symbol table if it doesn’t exist). If symbol_table isomitted, call symputx stores the macro variable in the sametable that call symput (p. 36) does.

Example:

Prints items=!remove spaces! and x=!123.456!data _null_;call symputx(' items ',' remove spaces ','l');call symputx(' x ',123.456);

run;%put items=!&items!;%put x=!&x!;

See also: symget (p. 153).

call system(os_command);

Submits an OS command for execution. os_command (≤ 1024characters) is a quote-enclosed system command, an expres-sion whose value is a system command, or the name of a char-acter variable whose value is a system command. See also: Xstatement, X command, system (p. 155).

Page 41: 32883311 Sas Functions Pocketref

call vnext

List of Functions | 37

call tanh(var1[,var2,...]); 9+Replaces each numeric var by its hyperbolic tangent, definedby (ex–e–x)/(ex+e–x). Use only variables as arguments, not con-stants or expressions. If any argument is a missing value, miss-ing values are returned for all arguments. Example: x=.5; y=0;call tanh(x,y); → Replaces x with 0.4621171573 and ywith 0. See also: tanh (p. 155).

call vname(var,char_var);

Assigns a variable’s name to a character variable. var is anySAS variable. char_var is any SAS character variable. Namescan be up to 32 characters long; make the length of char_varlong enough to avoid truncation. See also: v<attr> (p. 161).

call vnext(varname[,vartype[,varlen]]); 9+Returns the name, type, and length of a DATA-step variable.

varname is a character variable that’s updated by call vnext.If the input value of varname is blank, the value returned invarname is the name of the first variable in the DATA step’svariable list. If call vnext is executed for the first time in theDATA step, the value returned in varname is the name of thefirst variable in the DATA step’s variable list. If neither of thepreceding conditions is true, varname’s input value is ignored.Each time call vnext executes, it returns the name of thenext variable in the list in varname. After all the variablenames in the DATA step are returned, call vnext returns ablank in varname.

vartype is a character variable whose input value is ignored.The returned value is ‘N’ (if varname is numeric) or ‘C’ (if it’scharacter). If varname is blank, so is vartype.

varlen is a numeric variable whose input value is ignored. Thereturned value is the length of varname. If varname is blank,varlen is zero (0).

Example:

data test;x=1; y='abc'; z=.;length z 5;

Page 42: 32883311 Sas Functions Pocketref

cat

38 | SAS Functions Pocket Reference

run;

data attributes;set test;by x;input a b $ c;length varname $32 vartype $3;varname=' ';varlen=1;do i=1 to 99 until(varname=' ');call vnext(varname,vartype,varlen);put i= varname @20 vartype= varlen=;

end;myvar=0;datalines;

1 a 2;

The result in the SAS log is

i=1 x vartype=N varlen=8i=2 y vartype=C varlen=3i=3 z vartype=N varlen=5i=4 FIRST.x vartype=N varlen=8i=5 LAST.x vartype=N varlen=8i=6 a vartype=N varlen=8i=7 b vartype=C varlen=8i=8 c vartype=N varlen=8i=9 varname vartype=C varlen=32i=10 vartype vartype=C varlen=3i=11 varlen vartype=N varlen=8i=12 i vartype=N varlen=8i=13 myvar vartype=N varlen=8i=14 _ERROR_ vartype=N varlen=8i=15 _N_ vartype=N varlen=8i=16 vartype= varlen=0

See also: var<attr> (p. 160), v<attr> (p. 161).

cat(str1[,str2,...]) 9+Concatenates strings without removing leading or trailingspaces, returning the concatenated value with a length up to

• 200 characters in WHERE clauses and in PROC SQL• 32767 characters in a DATA step, except in WHERE clauses

Page 43: 32883311 Sas Functions Pocketref

cat

List of Functions | 39

• 65534 characters when a str is called from the macro pro-cessor.

If cat returns a value in a temporary buffer, the buffer lengthdepends on the caller and the buffer value can be truncatedafter cat finishes. If the target variable or buffer isn’t longenough to hold the concatenated string, SAS

• changes the result to a blank value in the DATA step or in PROC SQL

• logs a warning that the result was either truncated or set to a blank value, depending on the calling OS

• logs a note showing the function call’s location and the argument that caused the truncation

• sets _ERROR_ to 1 in the DATA step.The following table uses the concatenation operator (||), trim(p. 158), and left (p. 97) to show equivalents of cat, cats(p. 40), catt (p. 40), and catx (p. 40). s1-s4 are character vari-ables and d is a separator.

Examples:

Compare cat, cats, catt, and catxdata _null_;str1=' abc';str2='123';str3=' xy ';str4='Z';cat=cat(of str1-str4);

Call Equivalent codecat(of s1-s4) s1||s2||s3||s4

cats(of s1-s4) trim(left(s1))||trim(left(s2))||trim(left(s3))||trim(left(s4))

catt(of s1-s4) trim(s1)||trim(s2)||trim(s3)||trim(s4)

catx(d,of s1-s4) trim(left(s1))||d||trim(left(s2))||d||trim(left(s3))||d||trim(left(s4))

Page 44: 32883311 Sas Functions Pocketref

cats

40 | SAS Functions Pocket Reference

cats=cats(of str1-str4);catt=catt(of str1-str4);catx=catx('**',of str1-str4);put cat= cats= catt= catx= $char.;

run;

The results are listed in the following table.

See also: call cats (p. 19), call catt (p. 19), call catx (p. 19).

cats(str1[,str2,...]) 9+Concatenates strings, removing leading and trailing spaces.See cat (p. 38) for usage. Examples: See cat. See also: callcats (p. 19).

catt(str1[,str2,...]) 9+Concatenates strings, removing trailing spaces. See cat (p. 38)for usage. Examples: See cat. See also: call catt (p. 19).

catx(sep,str1[,str2,...]) 9+Concatenates strings, removing leading and trailing spacesand inserting separators between strings. sep usually is a spaceor a comma but can be any string. See cat (p. 38) for usage.Examples: See cat. See also: call catx (p. 19).

cdf(dist,quantile[,param1, param2,...])

Returns the left cumulative distribution function for the con-tinuous and discrete distributions listed in the following table.The CDF is the function F, for a random variable X, definedfor all real values of x by

.

Note that F(–∞) = 0, F(∞) = 1, and F(a) ≤ F(b) if a < b.

Variable Resultcat ' abc123 xy Z'

cats 'abc123xyZ'

catt ' abc123 xyZ'

catx 'abc**123**xy**Z'

F x( ) Prob X x≤( )=

Page 45: 32883311 Sas Functions Pocketref

cdf

List of Functions | 41

The random variable is an integer for discrete distributions.The shape, location, scale, mean, noncentrality, degrees-of-freedom, and other params vary by dist. You can truncate distto the first four characters.

Distribution Function callBernoulli cdf('bernoulli',x,prob_success)

Beta cdf('beta',x,a,b[,left_theta,right_theta])

Binomial cdf('binomial',num_successes,prob_success,num_trials)

Cauchy cdf('cauchy',x[,theta,lambda])

Chi-square cdf('chisquare',x,df[,nc])

Exponential cdf('exponential',x[,lambda])

F cdf('f',x,ndf,ddf[,nc])

Gamma cdf('gamma',x,a[,lambda])

Geometric cdf('geometric',num_failures,prob_success)

Hypergeometric cdf('hypergeometric',x,pop_size,category_size,sample_size[,odds_ratio])

Laplace cdf('laplace',x[,theta,lambda])

Logistic cdf('logistic',x[,theta,lambda])

Lognormal cdf('lognormal',x[,theta,lambda])

Negative binomial cdf('negbinomial',num_failures,prob_success,num_successes)

Normal cdf('normal'|'gauss',x[,theta,lambda])

Normal mixture cdf('normalmix',x,num_mixtures,proportions,means,stdevs)

Pareto cdf('pareto',x,a[,k])

Poisson cdf('poisson',n,lambda)

t cdf('t',x,df[,nc])

Uniform cdf('uniform',x[,left_theta,right_theta])

Page 46: 32883311 Sas Functions Pocketref

ceil

42 | SAS Functions Pocket Reference

Examples:

cdf('bern',0,0.5) → 0.5.

cdf('beta',0.2,2,1) → 0.04.

cdf('binom',6,0.5,10) → 0.828125.

cdf('chisq',5.5,10) → 0.144621493.

cdf('expo',1) → 0.632120558.

cdf('f',3.5,4,5) → 0.899032601.

cdf('lognormal',0.5,1,2) → 0.198616419.

cdf('normal',1.96) → 0.9750021049.

cdf('poisson',2,1) → 0.9196986029.

cdf('t',1.0,20) → 0.835371711.

cdf('uniform',3.0,2,4) → 0.5.

See also: logcdf (p. 100), pdf (p. 114), quantile (p. 127), sdf (p. 146).

ceil(x)

Returns the smallest integer ≥ x. If x is within 10-12 of an inte-ger, ceil returns that integer. Examples: The following tableshows examples of ceil, floor (p. 74), int (p. 87), and round(p. 133). See also: ceilz (p. 43).

Wald (Inv. Gauss) cdf('wald'|'igauss',x,d)

Weibull cdf('weibull',x,a[,lambda])

x ceil() floor() int() round()1.1 2 1 1 1

1.9 2 1 1 2

–1.1 –1 –2 –1 –1

–1.9 –1 –2 –1 –2

Distribution Function call

Page 47: 32883311 Sas Functions Pocketref

close

List of Functions | 43

ceilz(x) 9+Returns the smallest integer ≥ x, using zero fuzzing. Example:Compare ceilz(1+1.e-13) → 2 with ceil(1+1.e-13) → 1.See also: ceil (p. 42), fuzz (p. 79).

cexist(entry[,'u'])

Determines whether a SAS catalog or SAS catalog entry exists,returning one (1) if it exists; zero otherwise. entry is a SAS cat-alog or the name of an entry in a catalog. A one- or two-levelentry is assumed to be the name of a catalog. A three- or four-level entry tests for the existence of an entry within a catalog.Specify 'u' to test whether the entry exists and can be openedfor updating. Example: cexist('lib.cat1.x.program') → 1.See also: exist (p. 65), fexist (p. 68), fileexist (p. 69).

choosec(n,str1[,str2,...]) 9+Returns the n-th string. If n is negative, choosec counts back-ward. Examples: choosec(2,'one','two','three') → ‘two’.choosec(-1,'A','B','C') → ‘C’. See also: choosen (p. 43).

choosen(n,num1[,num2,...]) 9+Returns the n-th number. If n is negative, choosen counts back-ward. Examples: choosen(99,1,2,3,4) → . (missing—bad n).choosen(-2,11,12*2,13) → 24. See also: choosec (p. 43).

cinv(prob,df[,nc])

Returns the prob-th quantile from the chi-square distributionwith degrees of freedom df (> 0, noninteger allowed) andnoncentrality parameter nc (≥ 0). The probability that anobservation from a chi-square distribution is less than orequal to the returned quantile is prob (0 ≤ x < 1). If nc is omit-ted or is zero, cinv uses the central chi-square distribution.Large nc values can cause cinv to return a missing value. cinvis the inverse of probchi (p. 117). Example: cinv(0.95,3) →7.8147279033. See also: cnonct (p. 44).

close(dataset_id)

Closes a SAS dataset, returning zero (0) if successful; nonzerootherwise. dataset_id is the dataset identifier that open(p. 111) returns. To free memory you should close opendatasets when they’re no longer needed. Note that SAS auto-

Page 48: 32883311 Sas Functions Pocketref

cnonct

44 | SAS Functions Pocket Reference

matically closes all datasets opened within a DATA step whenthe step ends. See also: dclose (p. 56), fclose (p. 67).

cnonct(x,df,prob)

Returns the nonnegative noncentrality parameter from a non-central chi-squared distribution with the specified parame-ters. x (≥ 0) is a random variable, df (> 0, noninteger allowed)is the degrees of freedom, and prob (0 < prob < 1) is a proba-bility. cnonct returns a missing value if prob is greater thanthe probability from the specified distribution.

Examples:

cnonct(2,4,0.05) → 4.6466335598.

cnonct(2,4,0.3) → . (missing—bad prob).

cnonct(2,4,probchi(2,4,1.5)) → 1.5.

See also: cinv (p. 43), fnonct (p. 75), probchi (p. 117), tnonct (p. 156).

coalesce(x1[,x2,...]) 9+Returns the first nonmissing value from a list of numericarguments. The list can contain numeric values, missingnumeric values, and names of numeric variables. If only onevalue is listed, coalesce returns that value. If all the valuesare missing, coalesce returns a missing value. Examples:coalesce(.,22,44) → 22. coalesce(.,.,.) → . (missing).coalesce(7,8,9) → 7. See also: coalescec (p. 44).

coalescec(str1[,str2,...]) 9+Returns the first nonmissing value from a list of characterarguments. The list can contain character values, missingcharacter values, and names of character variables. If only onevalue is listed, coalescec returns that value. If all the valuesare missing, coalescec returns a missing value. Examples:coalescec('','a','z') → ‘a’. coalescec('','','') → ‘’(missing). coalescec('c1','c2') → ‘c1’. See also: coalesce(p. 44).

Page 49: 32883311 Sas Functions Pocketref

comb

List of Functions | 45

collate(start_pos[,end_pos])collate(start_pos[,,len])

Returns a subset of the ASCII or EBCDIC collation sequence.start_pos is the first character in the collating sequence toreturn. Specify either end_pos (start_pos ≤ end_pos ≤ 255) asthe last character to return or len as the number of charactersto return. If you omit end_pos and len, collate returns con-secutive characters from start_pos to the end of the sequenceor up to 255 characters, whichever comes first. If you specifyboth end_pos and len, end_pos prevails. See also: byte (p. 18),rank (p. 131).

Examples:

ASCIIcollate(48,,10) or collate(48,57) → ‘0123456789’.

EBCDICcollate(240,,10) or collate(240,249) → ‘0123456789’.

comb(n,r)

Returns the number of combinations of n elements taken r ata time (0 ≤ r ≤ n). Use comb to determine the total possiblenumber of groups for a given number of items. A combina-tion is any set or subset of items, regardless of their order.Combinations are distinct from permutations, for which theorder is significant. comb(n,r) also is known as the binomialcoefficient and is read “n choose r”. The number of combina-tions is n!/(r!(n–r)!) where n and r are integers and the symbol! denotes a factorial. comb(n,r) is the same as fact(n)/(fact(r)*(fact(n-r)). See also: fact (p. 66), perm (p. 115).

Examples:

comb(8,0) → 1.

comb(8,1) → 8.

comb(8,2) → 28.

comb(8,6) → 28.

comb(8,8) → 1.

Page 50: 32883311 Sas Functions Pocketref

compare

46 | SAS Functions Pocket Reference

comb(4,8) → . (missing—n < r).

comb(8.5,4) → . (missing—noninteger n).

comb(int(8.5),4) → 70.

compare(str1,str2[,modifiers]) 9+Compares two strings and returns the position of the leftmostcharacter by which they differ, or zero (0) if they match. Theresult is negative if str1 precedes str2 in a sort sequence; posi-tive otherwise. modifiers is one or more of the values listed inthe following table.

The order of modifiers is significant: 'ln' first removes lead-ing spaces from the strings and then removes quotes from n-literals; 'nl' reverses these two operations. Examples: See thefollowing table. See also: compged (p. 47), complev (p. 48),nliteral (p. 108).

modifiers Meansi Ignore the case of str1 and str2l Remove leading spaces from str1 and str2n Remove quotes from any argument that is an n-literal

and ignore the case of str1 and str2

: (colon) Truncate the longer of str1 or str2 to the length of the shorter string or to length one, whichever is greater; if you omit this modifier, the shorter string is padded with spaces to equal the length of the longer string

str1 str2 modifiers compare()'abc' 'abc' 0

'123' 'abc' –1

'abc' '123' 1

'abc' 'abx' –3

'abc' 'abcdef' –4

'abc' 'abcdef' ':' 0

'abc' 'aBc' 2

Page 51: 32883311 Sas Functions Pocketref

compged

List of Functions | 47

compbl(str)

Replaces runs of two or more consecutive spaces with a singlespace. The result’s default length is the length of str.

Examples:

compbl('New York, NY 10014') →‘New York, NY 10014’.

compbl(' ') → ‘ ’.

See also: compress (p. 48), trim (p. 158).

compged(str1,str2[,cutoff][,modifiers]) 9+Compares two strings and returns the generalized edit distance(GED), which is the minimum-cost sequence of operations(deletions, insertions, or replacements of single characters)needed to transform str1 into str2. Use call compcost (p. 19)to set the costs of edit operations. The returned GED won’texceed cutoff. See compare (p. 46) for modifiers.

GED is a generalization of Levenshtein edit distance (LED),which is a measure of dissimilarity between two strings. Usecomplev (p. 48) to compute LED. Computing GED takesmuch more time than does computing LED, but GED usuallyis more useful for applications like fuzzy merging and textmining. Examples: See the following table for examples ofcomged and complev. See also: spedis (p. 148).

'abc' 'aBc' 'i' 0

' abc' 'abc' –1

' abc' 'abc' 'l' 0

'abc' 'abc ' 0

'' 'abc' ':' –1

str1 str2 compged() and operations complev()

'baboon' 'baboon' 0 (match) 0

'baXboon' 'baboon' 100 (insert) 1

str1 str2 modifiers compare()

Page 52: 32883311 Sas Functions Pocketref

complev

48 | SAS Functions Pocket Reference

complev(str1,str2[,cutoff][,modifiers]) 9+Compares two strings and returns the Levenshtein edit dis-tance (LED). LED is a special case of the generalized edit dis-tance, computed with compged (p. 47). The returned LEDwon’t exceed cutoff. See compare (p. 46) for modifiers. Exam-ples: See compged. See also: nliteral (p. 108), spedis (p. 148).

compound(init_amt,fut_amt,int_rate,num_periods)

Returns a compound-interest value. init_amt (≥ 0) is the ini-tial amount. fut_amt (≥ 0) is the future amount at the end ofnum_periods. int_rate (≥ 0) is the interest rate, expressed as afraction. num_periods (≥ 0) is the integer number of com-pounding periods. Provide any three nonmissing argumentsand compound returns the fourth.

Example:

Returns num_periodscompound(1,2,0.06,.) → 11.8957.

compress(str[,chars][,modifiers])

Removes or keeps specified characters in a string. chars is thelist of literal characters to keep or remove in str. By default, thecharacters in chars are removed from str. If you specify k inmodifiers, only the characters in chars are kept in str. To add tothe chars list, set modifiers to one or more of the values listedin the following table. If chars and modifiers are omitted, only

'baoon' 'baboon' 100 (delete) 1

'baXoon' 'baboon' 100 (replace) 1

'baboonX' 'baboon' 50 (append) 1

'baboo' 'baboon' 10 (truncate) 1

'babboon' 'baboon' 20 (double) 1

'bab,oon' 'baboon' 30 (punctuation) 1

'bXaYoon' 'baboon' 200 (insert+replace) 2

'axoo' 'baboon' 310 (fdelete+replace+truncate)

3

str1 str2 compged() and operations complev()

Page 53: 32883311 Sas Functions Pocketref

compress

List of Functions | 49

spaces are removed from str. The result’s default length is thelength of str.

Examples:

s='ABC 123 xyz’';

compress(s) → ‘ABC123abc’.

compress(s,'a') → ‘BC 123 abc’.

modifiers Meansa Add letters (A–Z, a–z) to charsc Add control characters to charsd Add digits (0–9) to charsf Add the underscore (_) and letters (A–Z, a–z) to charsg Add graphic characters to charsi Ignore the case of characters to be kept or removedk Keep the characters in chars instead of removing themn Add the underscore (_), letters (A–Z, a–z), and digits

(0–9) to charsl Add lowercase letters (a–z) to charso Process chars and modifiers only once instead of every

time compress is called. Using this modifier in the DATA step (excluding WHERE clauses) or PROC SQL can make compress run much faster in a loop where chars and modifiers don’t change.

p Add punctuation marks to charss Add whitespace characters to chars (space, horizontal

tab, vertical tab, carriage return, line feed, and form feed)

t Trim trailing spaces from str and charsu Add uppercase letters (A–Z) to charsw Add printable characters to charsx Add hexadecimal characters to chars

Page 54: 32883311 Sas Functions Pocketref

constant

50 | SAS Functions Pocket Reference

compress(s,'A','ki') → ‘Aa’.

compress(s,'A ','d') → ‘BCabc’.

lengthc(compress(s,' ','k')) → 3 (counts spaces in s).

compress('(123) 456-7890','(-) ') → ‘1234567890’.

compress('(123) 456-7890',,'kd') → ‘1234567890’.

See also: compbl (p. 47), trim (p. 158).

constant(const[,param])

Returns the mathematical and machine constants listed in thefollowing table.

Constant Function callNatural base e (2.7183...) constant('e')

Euler constant (0.5772...) constant('euler')

Pi (3.14159...) constant('pi')

Largest integer represent-able in len bytes (2 ≤ len ≤ 8, default 8)

constant('exactint'[,len])

Largest 8-byte floating-point number

constant('big')

Log base (default e) of big constant('logbig'[,base])

Square root of big constant('sqrtbig')

Smallest positive 8-byte floating-point number

constant('small')

Log base (default e) of small

constant('logsmall'[,base])

Square root of small constant('sqrtsmall')

Machine precision—the difference between 1 and the next greater number

constant('maceps')

Log base (default e) of maceps

constant('logmaceps'[,base])

Square root of maceps constant('sqrtmaceps')

Page 55: 32883311 Sas Functions Pocketref

cosh

List of Functions | 51

Examples:

On a Windows 32-bit machine

constant('e') → 2.7182818285.

constant('pi') → 3.1415926536.

constant('exactint') → 9.0071993E15.

constant('big') → 1.797693E308.

constant('sqrtbig') → 1.340781E154.

constant('small') → 2.22507E-308.

constant('maceps') → 2.220446E-16.

convx(yld_to_mat,freq,cf0,cf1,...,cfn)

Returns the convexity of cash flows. For a description of theparameters see dur (p. 63).

Example: convx(0.06,2,10,20,30,40) → 3.51168.

See also: convxp (p. 51), durp (p. 63).

convxp(par,cpn_rate,cpn_freq,remaining_cpns,time_to_next_cpn,yld_to_mat)

Returns the convexity of a bond. For a description of theparameters see pvp (p. 126).

Example: convxp(1000,0.01,4,14,0.33/2,0.1) → 11.6023.

See also: convx (p. 51), durp (p. 63).

cos(angle)

Returns the cosine of an angle. angle is expressed in radians.To convert degrees to radians, multiply degrees by π/180.Note that cos(-x) = cos(x) and 1/cos(x) is the secant of x.Examples: cos(0) → 1. cos(1.0472) → 0.5 (1.0472 radians ≈60 degrees). See also: arcos (p. 12), sin (p. 146), tan (p. 155).

cosh(x)

Returns the hyperbolic cosine of x, defined by (ex + e–x)/2.Examples: cosh(0) → 1. cosh(exp(1)) → 7.6101. See also:sinh (p. 147), tanh (p. 155).

Page 56: 32883311 Sas Functions Pocketref

count

52 | SAS Functions Pocket Reference

count(str,substr[,modifiers]) 9+Returns the number of times that a substring appears within astring, or zero (0) if the substring isn’t found. See find (p. 71)for modifiers. If two occurrences of substr overlap in str, countreturns inconsistent results.

Examples:

str='This is a thistle? Yes, this is a thistle';

count(str,'this') → 3.

count(str,'this','i') → 4.

count(str,'is') → 6.

count('gogogo','gogo') → 1 or 2 (inconsistent).

See also: countc (p. 52), index (p. 85), rxmatch (p. 134).

countc(str,chars[,modifiers]) 9+Returns the number of specific characters that either appearor don’t appear in a string, or zero (0) if none are found. Seefindc (p. 72) for modifiers.

Examples:

str='Baboons Eat Bananas ';

countc(str,'a') → 5.

countc(str,'b') → 1.

countc(str,'b','i') → 3.

countc(str,'ab') → 6.

countc(str,'ab','i') → 8.

countc(str,'ab','v') → 16.

countc(str,'ab','vit') → 11.

countc(str,' ') → 5.

countc(str,' ','t') → 0.

See also: count (p. 52), indexc (p. 85), verify (p. 162).

Page 57: 32883311 Sas Functions Pocketref

daccdbsl

List of Functions | 53

css(x1[,x2,...])

Returns the corrected sum of squares of nonmissing argu-ments. If all the arguments are missing values, the result is amissing value. Example: x1=3; x2=4; x3=5; css(2,of x1-x3,6,.) → 10. See also: uss (p. 159).

curobs(dataset_id)

Returns the observation number of the current observation.dataset_id is the dataset identifier that open (p. 111) returns.Use curobs with only an uncompressed SAS dataset that isaccessed by using a native library engine. curobs returns amissing value if the engine doesn’t support observation num-bers. For SAS views, curobs returns the number of the obser-vation within the view (not the observation number of anyunderlying dataset). See also: fetchobs (p. 68).

cv(x1,x2[,x3,...])

Returns the coefficient of variation of nonmissing arguments.At least two nonmissing arguments are required or the resultis a missing value. Example: x1=3; x2=4; x3=5; y1=.;cv(2,of x1-x3,6,y1) → 39.528470752.

daccdb(age,init_value,lifetime,rate)

Returns the accumulated declining balance depreciation. ageis the period of calculation. For a noninteger age, the depreci-ation is prorated between the two consecutive time periodsthat precede and follow the fractional period. init_value is theasset’s depreciable initial value. lifetime (> 0) is the asset’s life-time. rate (> 0) is rate of depreciation, expressed as a fraction.

Example: daccdb(10,1000,15,2) → 760.93228394.

See also: depdb (p. 56).

daccdbsl(age,init_value,lifetime,rate)

Returns the accumulated declining balance with conversionto a straight-line depreciation. See daccdb (p. 53) for a descrip-tion of the parameters (except that age must be an integer).

Example: daccdbsl(10,1000,15,2) → 772.65327087.

See also: depdbsl (p. 56).

Page 58: 32883311 Sas Functions Pocketref

daccsl

54 | SAS Functions Pocket Reference

daccsl(age,init_value,lifetime)

Returns the accumulated straight-line depreciation. See daccdb(p. 53) for a description of the parameters.

Example: daccsl(10,1000,15) → 666.66666667.

See also: depsl (p. 57).

daccsyd(age,init_value,lifetime)

Returns the accumulated sum-of-years-digits depreciation. Seedaccdb (p. 53) for a description of the parameters.

Example: daccsyd(10,1000,15) → 875.

See also: depsyd (p. 57).

dacctab(age,init_value,t1,...,tn)

Returns the accumulated depreciation using per-period rates.See daccdb (p. 53) for a description of age and init_value. t1,..., tn are fractions of depreciation for each time period wheret1 + t2 + ... + tn ≤ 1.

Example: dacctab(3,1000,0.4,0.3,0.2,0.1) → 900.

See also: deptab (p. 57).

dairy(x)

Returns the derivative of the Airy function airy (p. 10). Exam-ple: dairy(2) → -0.053090384.

datdif(start_date,end_date,basis)

Counts the number of days between start_date and end_date.basis is one of the values listed in the following table.

basis Count days by using

'30/360'|'360'

30-day months and 360-day years, regardless of the actual number of days each month or year has

'act/act'|'actual'

The actual number of days between the dates, divided by 365 plus the number of days that fall in 366-day years divided by 366

Page 59: 32883311 Sas Functions Pocketref

datetime

List of Functions | 55

Examples:

datdif('1feb2007'd,'31mar2007'd,'30/360') → 60.

datdif('1feb2007'd,'31mar2007'd,'act/act') → 58.

datdif('29dec2005'd,'1jan2007'd,'360') → 362.

datdif('29dec2005'd,'1jan2007'd,'actual') → 368.

See also: yrdif (p. 164).

date()

Returns the current date as a SAS date (the number of dayssince 1-Jan-1960). date is the same as today (p. 156).

Example: date() → 16681 (today is 2-Sep-2005).

See also: datetime (p. 55), dhms (p. 59), hms (p. 82), mdy (p. 101), time (p. 155).

datejul(julian_date)

Converts a Julian date to a SAS date. Set julian_date to yydddor yyyyddd (1 ≤ ddd ≤ 366).

Example: datejul(2005245) → 16681 (2-Sep-2005).

See also: juldate (p. 92), juldate7 (p. 92).

datepart(sas_datetime)

Extracts the date, as a SAS date, from a SAS datetime.

Example: datepart(datetime()) → 16681 (now is 2-Sep-2005 22:19:49).

See also: datetime (p. 55), timepart (p. 155).

datetime()

Returns the current date and time of day as a SAS datetime(the number of seconds between 1-Jan-1960 and now).

Example: datetime() → 1441311980.6 (now is 2-Sep-2005 20:26:21).

See also: datepart (p. 55), dhms (p. 59), hms (p. 82), mdy (p. 101), time (p. 155), timepart (p. 155), today (p. 156).

Page 60: 32883311 Sas Functions Pocketref

day

56 | SAS Functions Pocket Reference

day(sas_date)

Extracts the day of the month (1–31) from a SAS date.

Example: day(today()) → 2 (today is 2-Sep-2005).

See also: month (p. 104), qtr (p. 126), week (p. 163), weekday (p. 163), year (p. 163).

dclose(dir_id)

Closes a directory, returning zero (0) if successful; nonzerootherwise. dir_id is the directory identifier that dopen (p. 62)returns. dclose also closes any open members. To free mem-ory you should close open directories and members whenthey’re no longer needed. Note that SAS automatically closesall directories or members opened within a DATA step whenthe step ends. See also: close (p. 43), fclose (p. 67).

dcreate(dir_name[,parent_dir]) 9+Creates an external directory, returning the complete path-name of the new directory if successful, or an empty stringotherwise. dir_name is the name of the directory to create(this value can’t include a pathname). parent_dir is the com-plete pathname of the directory in which to create the newdirectory. If parent_dir is omitted, the current directory isused. Example: (Windows) dcreate('newdir','c:\mydir\')→ Creates (and returns) the directory c:\mydir\newdir.

depdb(age,init_value,lifetime,rate)

Returns the declining balance depreciation. See daccdb (p. 53)for a description of the parameters.

Example: depdb(10,1000,15,2) → 36.7796.

depdbsl(age,init_value,lifetime,rate)

Returns the declining balance with conversion to a straight-line depreciation. See daccdb (p. 53) for a description of theparameters (except that age must be an integer).

Example: depdbsl(10,1000,15,2) → 45.4693.

See also: daccdbsl (p. 53).

Page 61: 32883311 Sas Functions Pocketref

dequote

List of Functions | 57

depsl(age,init_value,lifetime)

Returns the straight-line depreciation. See daccdb (p. 53) for adescription of the parameters.

Example: depsl(10,1000,15) → 66.6667.

See also: daccsl (p. 54).

depsyd(age,init_value,lifetime)

Returns sum-of-years-digits depreciation. See daccdb (p. 53)for a description of the parameters.

Example: depsyd(10,1000,15) → 50.

See also: daccsyd (p. 54).

deptab(age,init_value,t1,...,tn)

Returns the depreciation using per-period rates. See daccdb(p. 53) for a description of age and init_value. t1,..., tn are frac-tions of depreciation for each time period where t1 + t2 + ... +tn ≤ 1.

Example: deptab(3,1000,0.4,0.3,0.2,0.1) → 200.

See also: dacctab (p. 54).

dequote(str)

Removes interior matching quotes, reduces quoted quotes,and deletes everything to the right of the closing quote. Theresult’s default length depends on the length of str and its val-ue depends on str’s first one or two characters:

• If the first character of str isn’t a single or double quote, the result is str unchanged.

• If the first two characters of str are both single or double quotes, the result is an empty string.

• If the first character of str is a single quote, dequote removes that quote and then scans str from left to right for more single quotes. All paired single quotes are reduced to one single quote. The first non-paired single quote and all characters to its right are removed.

Page 62: 32883311 Sas Functions Pocketref

deviance

58 | SAS Functions Pocket Reference

• If the first character of str is a double quote, dequote removes that quote and then scans str from left to right for for more double quotes. All paired double quotes are reduced to one double quote. The first non-paired double quote and all characters to its right are removed.

If str is a quote-enclosed constant, those quotes aren’t part ofthe value of the string; don’t use dequote to remove thequotes that denote a constant.

Examples: See the following table. See also: quote (p. 127).

deviance(dist,rand_var,shape_params[,eps])

Computes the deviance for the distributions listed in the fol-lowing table. eps is an optional small bounding value. Example:deviance('normal',5,2) → 9.

str dequote()xxx xxx

xxx "yyy" zzz xxx "yyy" zzz

"" xxx <zero-length string>

"xxx" or 'xxx' xxx

"xxx ""yyy"" zzz" xxx "yyy" zzz

'xxx ''yyy'' zzz' xxx 'yyy' zzz

'x "yy" zz ""ww"" tt' x "yy" zz ""ww"" tt

"xxx xxx

xxx'y zzz xxx'y zzz

"xxx" yyy "zzz" xxx

Distribution Function callBernoulli deviance('bernoulli'|'bern',

zero_or_one,prob_success[,eps])

Binomial deviance('binomial'|'bino',num_successes,mean,num_trials[,eps])

Gamma deviance('gamma',rand_var,mean[,eps])

Page 63: 32883311 Sas Functions Pocketref

dif

List of Functions | 59

dhms(sas_date,hour,minute,second)

Creates a SAS datetime from a SAS date, hour, minute, andsecond. The result is the number of seconds before (negative)or after (positive) midnight, 1-Jan-1960.

Examples:

dhms('10may1936'd,00,00,00) → -746150400 (10-May-1936 midnight).

dhms('1jan1960'd,00,00,00) → 0 (1-Jan-1960 midnight).

dhms('22jun2001'd,6,0,0.56) → 1308808800.56 (22-Jun-2001 06:00:00.56).

dhms(today(),0,0,time()) → 1441313508.5 (now is 2-Sep-2005 20:51:49).

See also: datetime (p. 55), hms (p. 82), mdy (p. 101), time (p. 155), today (p. 156).

dif(x)difn(x)

Returns the difference between the current number x and thex supplied in the n-th preceding dif call. n ranges from 1 (thedefault) to 100. dif1 is equivalent to dif. dif returns a miss-ing value the first n calls. Note that dif2(x) isn’t equivalentto dif(dif(x)), rather difn(x) is equivalent to x-lagn(x).See also: lag (p. 96).

Example:

Show the difference between dif and lagdata difs;

Normal deviance('normal'|'gaussian',rand_var,mean)

Poisson deviance('poisson'|'pois',rand_var,lambda[,eps])

Wald (Inv. Gaussian)

deviance('wald'|'igauss',rand_var,mean[,eps])

Distribution Function call

Page 64: 32883311 Sas Functions Pocketref

digamma

60 | SAS Functions Pocket Reference

input x @@;d1=dif(x);d2=dif2(x);l1=lag(x);l2=lag2(x);datalines;1 2 4 3 8 6 -3;

run;

The resulting dataset is shown in the following table.

digamma(x)

Returns the value of the digamma function Γ´(x)/Γ(x), whereΓ() and Γ´() are the functions gamma (p. 80) and its derivative,respectively, and x is a nonzero real number that isn’t a nega-tive integer. If x > 0 then digamma is the derivative of lgamma(p. 98). Examples: digamma(6) → 1.7061176684. digamma(1)→ -0.577215665. See also: trigamma (p. 157).

dim(array[,n])dimn(array)

Returns the size (number of elements) of array, or the size ofdimension n (≥ 1) of a multidimensional array. If omitted, ndefaults to 1. dimn(array) and dim(array,n) are equivalentcalls.

Examples:

array a{2:3,10:12,5} x1-x30;

x d1 d2 l1 l21 . . . .

2 1 . 1 .

4 2 3 2 1

3 –1 1 4 2

8 5 4 3 4

6 –2 3 8 3

–3 –9 –11 6 8

Page 65: 32883311 Sas Functions Pocketref

dinfo

List of Functions | 61

dim(a) or dim(a,1) → 2.

dim2(a) or dim(a,2) → 3.

dim3(a) or dim(a,3) → 5.

Iterate over array ararray ar[4] x1 x2 x3 x4;do i=1 to dim(ar);

Do something with ar{i} hereend;

Count the number of character variables in a datasetarray char_vars{*} _character_;num_char_vars=dim(char_vars);

See also: hbound (p. 82), lbound (p. 96).

dinfo(dir_id,info_item)

Returns information about a directory. dir_id is the directoryidentifier that dopen (p. 62) returns. info_item is a string thatidentifies the item to retrieve. The available info_items vary byOS. Use doptname (p. 62) to get item names and doptnum(p. 62) to get the number of available items. dinfo returns ablank if info_item is invalid.

Example:

Create a dataset with information-item names and valuesdata dinfo;length info_item $ 12 item_value $ 40;keep info_item item_value;rc=filename('mydir','physical_name');dir_id=dopen('mydir');num_items=doptnum(dir_id);do i=1 to num_items;info_item=dinfo_item(dir_id,i);item_value=dinfo(dir_id,info_item);output;

end;run;

See also: finfo (p. 72).

Page 66: 32883311 Sas Functions Pocketref

dnum

62 | SAS Functions Pocket Reference

dnum(dir_id)

Returns the number of members (files) in a directory. dir_idis the directory identifier that dopen (p. 62) returns. Use dnumto determine the highest number that you can pass to dread(p. 62).

dopen(fileref)

Opens a directory (or subdirectory, MACLIB, or partitioneddataset, depending on your OS) and returns a unique numer-ic directory identifier if successful; zero otherwise. Use this IDto identify the open directory to other directory functions.fileref is a fileref assigned to the directory by the FILENAMEstatement or the function filename (p. 69). See also: dclose(p. 56), fopen (p. 75), mopen (p. 104), open (p. 111).

doptname(dir_id,n)

Returns the name of the n-th information item for a directory.dir_id is the directory identifier that dopen (p. 62) returns. n isthe sequence number of the item. The returned item namesvary by OS. Use doptnum (p. 62) to get the number of itemsand dinfo (p. 61) to get item values. Example: See dinfo. Seealso: foptname (p. 77).

doptnum(dir_id)

Returns the number of information items that are availablefor a directory. dir_id is the directory identifier that dopen(p. 62) returns. The returned number varies by OS. Usedoptname (p. 62) to get item names and dinfo (p. 61) to getitem values. Example: See dinfo. See also: foptnum (p. 77).

dread(dir_id,n)

Returns the name of a directory member, or a blank if unsuc-cessful. dir_id is the directory identifier that dopen (p. 62)returns. n is the sequence number of the member (file) in thedirectory. Use dnum (p. 62) to determine the maximum n.

dropnote(dataset_id|file_id,note_id)

Deletes a note created by note (p. 110) or fnote (p. 75) from aSAS dataset or an external file, returning zero (0) if successful;nonzero otherwise. dataset_id is the dataset identifier thatopen (p. 111) returns. file_id is the file identifier that fopen

Page 67: 32883311 Sas Functions Pocketref

erfc

List of Functions | 63

(p. 75) or mopen (p. 104) returns. note_id is the note identifierthat note or fnote returns. See also: fpoint (p. 77), point(p. 116).

dsname(dataset_id)

Returns the name of a SAS dataset. dataset_id is the datasetidentifier that open (p. 111) returns. dsname returns an emptystring if dataset_id is invalid.

dur(yld_to_mat,freq,cf0,cf1,...,cfn)

Returns the modified duration of cash flows. yld_to_mat (0 <yld_to_mat < 1) is the effective per-period yield-to-maturity,expressed as a fraction. freq (> 0) is the integer frequency ofcash flows per period. cf0, cf1,..., cfn is a list of cash flows.

Example: dur(0.06,2,10,20,30,40) → 1.40123.

See also: convx (p. 51).

durp(par,cpn_rate,cpn_freq,remaining_cpns,time_to_next_cpn,yld_to_mat)

Returns the modified duration of a bond. For a description ofthe parameters see pvp (p. 126).

Example: durp(1000,0.01,4,14,0.33/2,0.1) → 3.2649.

See also: convxp (p. 51).

erf(x)

Returns the value of the normal error function, given by

.

Examples: erf(0) → 0. erf(1) → 0.8427007929. erf(-1) → -0.84270079.

See also: erfc (p. 63).

erfc(x)

Returns the complement of the normal error function, that is,1-erf(x). Examples: erfc(0) → 1. erfc(1) → 0.1572992071.erfc(-1) → 1.8427007929. See also: erf (p. 63).

erf x( ) 2π------- e z–

2

zd0

x

∫=

Page 68: 32883311 Sas Functions Pocketref

eurocurr

64 | SAS Functions Pocket Reference

eurocurr(amount,from_ccy,to_ccy)

Converts amount units of currency from_ccy to to_ccy. Usethe currency codes listed in the following table.

ccy Currencyats Austrian schilling

bef Belgian franc

chf Swiss franc

czk Czech koruna

dem Deutsche mark

dkk Danish krone

esp Spanish peseta

eur or blank Euro

fim Finnish markka

frf French franc

gbp British pound sterling

grd Greek drachma

huf Hungarian forint

iep Irish pound

itl Italian lira

luf Luxembourg franc

nlg Dutch guilder

nok Norwegian krone

plz Polish zloty

pte Portuguese escudo

rol Romanian leu

rur Russian ruble

sek Swedish krona

sit Slovenian tolar

trl Turkish lira

yud Yugoslavian dinar

Page 69: 32883311 Sas Functions Pocketref

exp

List of Functions | 65

Examples:

Euros to Deutsche markseurocurr(1,'eur','dem') → 1.95583.

Swiss francs to Euroseurocurr(50,'chf','eur') → 31.166240728.

exist(member_name[,member_type[,generation]])

Determines whether a SAS data library member exists,returning one (1) if it exists; zero otherwise.

member_name is a SAS data library member. member_namedefaults _LAST_ if omitted, blank, or an empty string.

member_type is the type of SAS data library member (typical-ly data (the default), access, view, or catalog).

generation is the dataset’s generation number and is ignored ifmember_type isn’t the value data.

Example: exist('new','data') → 1.

See also: cexist (p. 43), fexist (p. 68), fileexist (p. 69).

exp(x)

Returns e raised to the power x, where e ≈ 2.7182818285. Toget the approximate value of e, use exp(1) or constant('e')(p. 50). To calculate powers of other bases, use the exponenti-ation operator (**). exp is the inverse of log (p. 99).

Examples:

exp(-2) → 0.1353352832.

exp(-1) → 0.3678794412.

exp(0) → 1.

exp(1) → 2.7182818285.

exp(2) → 7.3890560989.

exp(log(2)) or log(exp(2)) → 2.

exp(.) → . (missing).

Page 70: 32883311 Sas Functions Pocketref

fact

66 | SAS Functions Pocket Reference

fact(n)

Returns the factorial of n (≥ 0), given by n! = 1 × 2 × ⋅⋅⋅ × n,where n is an integer. The special case 0! is defined to be equalto 1. Note that fact(n) equals gamma(n+1).

Examples:

fact(0) → 1.

fact(1) → 1.

fact(8) → 40320.

fact(-8) → . (missing—n < 0).

fact(8.5) → . (missing—noninteger n).

fact(int(8.5)) → 40320.

fact(20) → 2.432902E18.

See also: comb (p. 45), gamma (p. 80), perm (p. 115).

fappend(file_id[,cc])

Appends the record that is currently in the File Data Buffer(FDB) to the end of an external file, returning zero (0) if suc-cessful; nonzero otherwise. file_id is the file identifier thatfopen (p. 75) or mopen (p. 104) returns. cc is one of the car-riage-control characters listed in the following table. See also:fread (p. 78), fwrite (p. 79).

cc Meansblank Start the record on a new line

0 Skip one blank line before a new line

- Skip two blank lines before a new line

1 Start the line on a new page

+ Overstrike the line on the previous line

P Interpret the line as a terminal prompt

= Interpret the line as carriage-control information

All else Start the record on a new line

Page 71: 32883311 Sas Functions Pocketref

fetch

List of Functions | 67

fclose(file_id)

Closes an external file, directory, or directory member,returning zero (0) if successful; nonzero otherwise. file_id isthe file identifier that fopen (p. 75) or mopen (p. 104) returns.To free memory you should close open files when they’re nolonger needed. In some operating systems SAS automaticallycloses all files opened within a DATA step when the step endswhereas in other OSes you must fclose files explicitly. Fordetails see the SAS documentation for your OS. See also:close (p. 43), dclose (p. 56).

fcol(file_id)

Returns the current column position in the File Data Buffer(FDB). file_id is the file identifier that fopen (p. 75) or mopen(p. 104) returns. Use fcol with fpos (p. 77) to manipulatedata in the FDB. See also: fget (p. 69), fput (p. 78).

fdelete(fileref|dir)

Deletes an external file or an empty directory, returning zero(0) if successful; nonzero otherwise. fileref is an unconcatenat-ed fileref assigned to the external file by the FILENAME state-ment or the function filename (p. 69). dir is an emptydirectory that you have permission to delete.

Example:

Assign a fileref, delete a file, and then unassign the filerefdata _null_;fileref="tempfile";rc=filename(fileref,"physical_filename");if rc=0 and fexist(fileref) thenrc=fdelete(fileref);

msg=sysmsg();if msg ne ' ' then put msg=;rc=filename(fileref);

run;

See also: fexist (p. 68), sysmsg (p. 153).

fetch(dataset_id[,'noset'])

Reads the next nondeleted observation from a SAS datasetinto the Dataset Data Vector (DDV), returning zero (0) if suc-

Page 72: 32883311 Sas Functions Pocketref

fetchobs

68 | SAS Functions Pocket Reference

cessful, nonzero if unsuccessful, or -1 for end-of-dataset. Usesysmsg (p. 153) to get the error message for a nonzero result.

dataset_id is the dataset identifier that open (p. 111) returns.

'noset' prevents the automatic passing of dataset variablevalues to DATA-step variables or macro variables even if callset (p. 31) has been called.

See also: fetchobs (p. 68), getvarc (p. 81), getvarn (p. 81).

fetchobs(dataset_id,n[,options])

Reads the n-th observation from a SAS dataset into theDataset Data Vector (DDV), returning zero (0) if successful,nonzero if unsuccessful, or -1 for end-of-dataset. Use sysmsg(p. 153) to get the error message for a nonzero result.

dataset_id is the dataset identifier that open (p. 111) returns.

n is the number of the observation to read. fetchobs treats nas a relative observation number (unless you use the 'abs'option); n may or may not coincide with the physical observa-tion number on disk because fetchobs skips observationsmarked for deletion. When a WHERE clause is active,fetchobs counts only observations that meet the WHEREcondition.

options is one or both of the following space-separatedoptions: 'abs' means that n is absolute (that is, deleted obser-vations are counted) and 'noset' prevents the automaticpassing of dataset variable values to DATA-step or macrovariables even if call set (p. 31) has been called.

See also: fetch (p. 67), getvarc (p. 81), getvarn (p. 81).

fexist(fileref)

Determines whether the external file associated with a filerefexists, returning one (1) if it exists; zero otherwise. fileref is afileref assigned to the external file by the FILENAME state-ment or the function filename (p. 69). Example: See fdelete(p. 67). See also: cexist (p. 43), exist (p. 65), fileexist(p. 69).

Page 73: 32883311 Sas Functions Pocketref

filename

List of Functions | 69

fget(file_id,var[,len])

Copies data from the File Data Buffer (FDB) into a charactervariable, returning zero (0) if successful, or -1 for end-of-buff-er or no-more-tokens conditions. After fget is executed, thecolumn pointer moves to the next read position in the FDB.

file_id is the file identifier that fopen (p. 75) or mopen (p. 104)returns.

var, in a DATA step, is a character variable that holds the data.In a macro, it’s a macro variable that holds the data. If var is amacro variable and it doesn’t exist, it’s created.

len is the number of characters to retrieve from the FDB. If lenis omitted or the buffer contains fewer than len characters, allthe FDB characters from the current column position to thenext delimiter are returned. The default delimiter, which isn’tretrieved, is a blank. Use fsep (p. 78) to change the delimiter.

See also: fcol (p. 67), fpos (p. 77), fput (p. 78).

fileexist(filename)

Determines whether an external file exists, returning one (1)if it exists; zero otherwise. filename is a fully qualified physicalfilename of the external file in the target OS. In a DATA step,filename is a character expression, a quoted string, or aDATA-step variable. In a macro, filename can be any expres-sion. See also: cexist (p. 43), exist (p. 65), fexist (p. 68),filename (p. 69).

filename(fileref,filename[,device_type[,host_options[,dir_ref]]])

Assigns or unassigns a fileref to an external file, directory, oroutput device, returning zero (0) if successful; nonzero other-wise. The association between a fileref and a physical file lastsonly for the duration of the current SAS session or until youchange or unassign the association by using the FILENAMEstatement or the filename function again.

fileref is, in a DATA step, the fileref to assign to the externalfile. In a macro (in the %sysfunc function, for example), filerefis the name of a macro variable (without an ampersand)

Page 74: 32883311 Sas Functions Pocketref

filename

70 | SAS Functions Pocket Reference

whose value contains the fileref to assign to the external file.A blank fileref (‘’) causes an error. If the fileref is a DATA-stepcharacter variable with a blank value and a minimum lengthof eight characters, or if a macro variable named in fileref hasa null value, then a fileref is autogenerated for you.

filename is the external file. An empty string unassigns a pre-viously assigned fileref.

device_type is the type of device or the access method that’sused if fileref points to an input or output device or locationthat isn’t a physical file. filename supports OS-specific devic-es (see the SAS documentation for your OS) and the devicesin the following table.

host_options is host-specific details such as file attributes andprocessing attributes. For details see the SAS documentationfor your OS.

device_type Devicedisk|base Disk drive (when you assign a fileref to an on-disk

file, you don’t have to use disk)

dummy The output to the file is discarded (use for testing)

gterm Graphics device that will be receiving graphics data

pipe Unnamed pipe (some OSes don’t support pipes)

plotter Unbuffered graphics output device

printer Printer or printer spool file

tape Tape drive

temp Temporary file that exists only as long as the file-name is assigned. The file is accessed through only the logical name and is available only while the logi-cal name exists. Don’t specify a physical pathname. Files manipulated by the temp device can have the same attributes and behave identically to disk files

terminal User’s terminal

uprinter Universal Printing printer definition name

Page 75: 32883311 Sas Functions Pocketref

find

List of Functions | 71

dir_ref is the fileref that was assigned to the directory or parti-tioned dataset in which the external file resides.

See also: dopen (p. 62), fexist (p. 68), fileexist (p. 69), fileref (p. 71), fopen (p. 75), mopen (p. 104).

fileref(fileref)

Determines whether a fileref has been assigned to a physicalfile for the current SAS session, returning a positive number iffileref isn’t assigned; a negative number if fileref exists but itsassociated file doesn’t; or zero if fileref and the associated fileboth exist. You can assign fileref to an external file by usingthe FILENAME statement or the function filename (p. 69).

find(str,substr[,modifiers][,start_pos]) 9+find(str,substr[,start_pos][,modifiers]) 9+

Searches a string for a substring and returns the first positionat which it’s found, or zero (0) if it’s not found. For a descrip-tion of start_pos see any<chartype> (p. 10). modifiers is oneor more of the values listed in the following table.

Examples:

str='She sells seashells? Yes, she does.';

find(str,'she ') → 27.

find(str,'she ','i') → 1.

find(str,'she ','i',5) → 27.

find(str,'she ','it',5) → 14.

find(str,'she ','it') → 1.

See also: count (p. 52), findc (p. 72), index (p. 85), rxmatch (p. 134).

modifiers Meansi Ignore character case during the search. If this modifier

is omitted, the search is case-sensitive.

t Trim trailing spacing from str and substr.

Page 76: 32883311 Sas Functions Pocketref

findc

72 | SAS Functions Pocket Reference

findc(str,chars[,modifiers][,start_pos]) 9+findc(str,chars[,start_pos][,modifiers]) 9+

Searches a string for any of specific characters that eitherappear or don’t appear in the string and returns the first posi-tion at which it’s found, or zero (0) if it’s not found. For adescription of start_pos see any<chartype> (p. 10). modifiersis one or more of the values listed in the following table.

Examples:

str='Hi there, Ian!';

findc(str,'Hi') → 1.

findc(str,'hi') → 2.

findc(str,'Hi','v') → 3.

findc(str,'Hi ','vi') → 4.

findc(str,'Hi','i',4) → 5.

See also: countc (p. 52), find (p. 71), indexc (p. 85), verify (p. 162).

finfo(file_id,info_item)

Returns information about an external file. file_id is the fileidentifier that fopen (p. 75) or mopen (p. 104) returns.info_item is a string that identifies the item to retrieve. Theavailable info_items vary by OS. Use foptname (p. 77) to getitem names and foptnum (p. 77) to get the number of availableitems. finfo returns a blank if info_item is invalid.

modifiers Meansi Ignore character case during the search. If this modifier

is omitted, the search is case-sensitive.

o Process chars and modifiers only once at the first call to findc. Changes to chars or modifiers in subsequent calls are ignored.

t Trim trailing spaces from str and chars.

v Count only the characters that don’t appear in chars.

Page 77: 32883311 Sas Functions Pocketref

fipname

List of Functions | 73

Example:

Create a dataset with information-item names and valuesdata finfo;length info_item item_value $60;drop rc file_id num_items i close;rc=filename('myfile','physical_filename');file_id=fopen('myfile');num_items=foptnum(file_id);do i=1 to num_items;info_item=foptname(file_id,i);item_value=finfo(file_id,info_item);output;

end;close=fclose(file_id);

run;

See also: dinfo (p. 61).

finv(prob,ndf,ddf[,nc])

Returns the prob-th quantile from the F distribution withnumerator degrees of freedom ndf (> 0, noninteger allowed),denominator degrees of freedom ddf (> 0, nonintegerallowed), and noncentrality parameter nc (≥ 0). The probabil-ity that an observation from an F distribution is less than orequal to the returned quantile is prob (0 ≤ x <1). If nc is omit-ted or is zero, finv uses the central F distribution. Large ncvalues can cause finv to return a missing value. finv is theinverse of probf (p. 117). Example: finv(0.95,2,10) →4.102821015. See also: fnonct (p. 75).

fipname(fips_code)

Converts a FIPS code to its state or U.S. territory name inuppercase (≤ 20 characters). fips_code is a U.S. Federal Infor-mation Processing Standards numeric code. Examples: The fol-lowing table shows examples of fipname, fipnamel (p. 74),and fipstate (p. 74).

fips_code fipname() fipnamel() fipstate()6 CALIFORNIA California CA

7 INVALID CODE Invalid Code --

Page 78: 32883311 Sas Functions Pocketref

fipnamel

74 | SAS Functions Pocket Reference

Create a table of FIPS codesdata fips;do i=1 to 95;fips_code=i;state_upper=fipname(i);state_mixed=fipnamel(i);state_abbrev=fipstate(i);output;

end;drop i;

run;

See also: stfips (p. 150), stname (p. 150), zipfips (p. 165), zipname (p. 166).

fipnamel(fips_code)

Converts a FIPS code to its state or U.S. territory name inmixed case (≤ 20 characters). fips_code is a U.S. Federal Infor-mation Processing Standards numeric code. Examples: Seefipname (p. 73). See also: fipstate (p. 74), stfips (p. 150),stnamel (p. 150), zipfips (p. 165), zipnamel (p. 166).

fipstate(fips_code)

Converts a FIPS code to its two-character state or U.S. territo-ry postal code or GSA geographic code in uppercase. fips_codeis a U.S. Federal Information Processing Standards numericcode. Examples: See fipname (p. 73). See also: fipnamel (p. 74),stfips (p. 150), zipfips (p. 165), zipstate (p. 166).

floor(x)

Returns the largest integer ≤ x. If x is within 10-12 of an inte-ger, floor returns that integer. Examples: See ceil (p. 42). Seealso: floorz (p. 75), int (p. 87), round (p. 133).

11 DISTRICT OF COLUMBIA

District of Columbia

DC

36 NEW YORK New York NY

72 PUERTO RICO Puerto Rico PR

95 PALMYRA ATOLL Palmyra Atoll --

fips_code fipname() fipnamel() fipstate()

Page 79: 32883311 Sas Functions Pocketref

fopen

List of Functions | 75

floorz(x) 9+Returns the largest integer ≤ x, using zero fuzzing. Example:Compare floorz(1-1.e-13) → 0 with floor(1-1.e-13) →1. See also: floor (p. 74), fuzz (p. 79).

fnonct(x,ndf,ddf,prob)

Returns the nonnegative noncentrality parameter from a non-central F distribution with the specified parameters. x (≥ 0) isa random variable, ndf (> 0, noninteger allowed) is thenumerator degrees of freedom, ddf (> 0, noninteger allowed)is the denominator degrees of freedom, and prob (0 < prob <1) is a probability. fnonct returns a missing value if prob isgreater than the probability from the specified distribution.

Examples:

fnonct(2,4,5,0.05) → 20.5828152.

fnonct(2,4,5,0.80) → . (missing—bad prob).

fnonct(2,4,5,probf(2,4,5,1.5)) → 1.5.

See also: cnonct (p. 44), finv (p. 73), probf (p. 117), tnonct (p. 156).

fnote(file_id)

Returns a unique numeric record ID for the last record thatwas read in an external file. file_id is the file identifier thatfopen (p. 75) or mopen (p. 104) returns. Use the returned IDwith fpoint (p. 77) to later return to the marked record. Todelete the ID, use dropnote (p. 62). See also: frewind (p. 78),note (p. 110).

fopen(fileref[,open_mode[,rec_len[,rec_format]]])

Opens an external file for reading or updating, returning aunique numeric file identifier if successful; zero otherwise.Use this ID to identify the open file to other file functions.

fileref is a fileref assigned to the external file by the FILE-NAME statement or the function filename (p. 69).

Page 80: 32883311 Sas Functions Pocketref

fopen

76 | SAS Functions Pocket Reference

open_mode is the type of access to the file. Omit open_modeto use input mode ('i' mode), or use one of the values listedin the following table.

rec_len is the file’s logical record length. Omit rec_len or set itto zero to use the existing logical record length.

rec_format is the file’s record format. Omit rec_format to usethe existing record format, or use one of the values listed inthe following table.

open_mode Modea Append mode allows writing new records after the

current end-of-file

i Input mode allows reading only (the default)

o Output mode defaults to the open mode specified in the OS option in the FILENAME statement or filename (p. 69) function; if no OS option is spec-ified, this option allows writing new records at the beginning of the file (output mode overwrites the file’s current contents without warning)

s Sequential input mode is used for pipes and other sequential devices such as hardware ports

u Update mode allows both reading and writing

rec_format Record formatb Data are interpreted as binary data

d Use the default record format

e Use an editable record format

f File contains fixed-length records

p File contains printer carriage control in OS-depen-dent record format (for z/OS datasets with FBA or VBA record format, use this option)

v File contains variable-length records

Page 81: 32883311 Sas Functions Pocketref

fpos

List of Functions | 77

To free memory, you should close open files when they’re nolonger needed. In some OSes SAS automatically closes filesopened within a DATA step when the step ends whereas inother OSes you must fclose (p. 67) files explicitly. For detailssee the SAS documentation for your OS.

See also: dopen (p. 62), mopen (p. 104), open (p. 111).

foptname(file_id,n)

Returns the name of the n-th information item for an externalfile. file_id is the file identifier that fopen (p. 75) or mopen(p. 104) returns. n is the sequence number of the item. Thereturned item names vary by OS. Use foptnum (p. 77) to getthe number of items and finfo (p. 72) to get item values.Example: See finfo. See also: doptname (p. 62).

foptnum(file_id)

Returns the number of information items that are availablefor an external file. file_id is the file identifier that fopen(p. 75) or mopen (p. 104) returns. The returned number variesby OS. Use foptname (p. 77) to get item names and finfo(p. 72) to get item values. Example: See finfo. See also: doptnum(p. 62).

fpoint(file_id,note_id)

Moves to the record marked previously by fnote (p. 75),returning zero (0) if successful; nonzero otherwise. file_id isthe file identifier that fopen (p. 75) or mopen (p. 104) returns.note_id is the note identifier that fnote returns. fpoint deter-mines only the record to read next, not which record is writ-ten next. When you open the file for update, fwrite (p. 79)writes to the most recently read record. See also: dropnote(p. 62), fread (p. 78), frewind (p. 78), point (p. 116).

fpos(file_id,n)

Sets the position of the column pointer in the File Data Buffer(FDB), returning zero (0) if successful; nonzero otherwise.file_id is the file identifier that fopen (p. 75) or mopen (p. 104)returns. n is the column at which to set the pointer. If n is pastthe end of the current record, the record size is increased

Page 82: 32883311 Sas Functions Pocketref

fput

78 | SAS Functions Pocket Reference

appropriately (except for fixed-block and VBA files). See also:fcol (p. 67), fget (p. 69), fput (p. 78).

fput(file_id,fdata)

Moves data to the File Data Buffer (FDB) of an external filestarting at the FDB’s current column position, returning zero(0) if successful; nonzero otherwise. file_id is the file identifierthat fopen (p. 75) or mopen (p. 104) returns. fdata is the filedata. In a DATA step, fdata is a quoted string or DATA-stepvariable; in a macro, fdata is a macro variable. The variable’slength determines the number of bytes moved to the FDB.The value of the column pointer is increased to one positionpast the end of the new text. See also: fcol (p. 67), fget (p. 69),fpos (p. 77).

fread(file_id)

Reads a record from an external file into the File Data Buffer(FDB), returning zero (0) if successful; nonzero otherwise.file_id is the file identifier that fopen (p. 75) or mopen (p. 104)returns. The file-pointer position updates automatically aftera read so that successive freads read successive records. Toposition a file pointer explicitly, use fpoint (p. 77) or frewind(p. 78). See also: frlen (p. 78), fwrite (p. 79).

frewind(file_id)

Moves the file pointer to the start of a file, returning zero (0) ifsuccessful; nonzero otherwise. file_id is the file identifier thatfopen (p. 75) or mopen (p. 104) returns. frewind doesn’t affecta file opened with sequential access. See also: fpoint (p. 77),fread (p. 78), rewind (p. 133).

frlen(file_id)

Returns the length of the last record read or, if the file isopened for output, returns the length of the current record.file_id is the file identifier that fopen (p. 75) or mopen (p. 104)returns. See also: fread (p. 78).

fsep(file_id,delims)

Sets the token delimiters for fget (p. 69), returning zero (0) ifsuccessful; nonzero otherwise. file_id is the file identifier thatfopen (p. 75) or mopen (p. 104) returns. delims (default blank)

Page 83: 32883311 Sas Functions Pocketref

gaminv

List of Functions | 79

is one or more delimiters that separate tokens in the File DataBuffer (FDB). Each listed character is a delimiter. If delims is‘#@’, for example, either ‘#’ or ‘@’ can separate tokens. Multi-ple consecutive delimiters, such as ‘@#@’ in this example, aretreated as a single delimiter.

fuzz(x)

Returns the nearest integer if x is within 10-12 of an integer;otherwise returns x. Example: fuzz(1.99999999999) → 2(exactly). See also: ceilz (p. 43), floorz (p. 75), intz (p. 91),modz (p. 104), roundz (p. 134).

fwrite(file_id[,cc])

Writes the next record to a file, returning zero (0) if success-ful; nonzero otherwise. file_id is the file identifier that fopen(p. 75) or mopen (p. 104) returns. cc is one of the carriage-con-trol characters listed in the following table.

fwrite moves text from the File Data Buffer (FDB) to theexternal file. To use the carriage-control characters, you mustopen the file with the record format of 'p' (print format) infopen. See also: fappend (p. 66), fread (p. 78).

gaminv(prob,a)

Returns the prob-th quantile from the gamma distributionwith shape parameter a (> 0). The probability that an obser-vation from a gamma distribution is less than or equal to the

cc Meansblank Start the record on a new line

0 Skip one blank line before a new line

- Skip two blank lines before a new line

1 Start the line on a new page

+ Overstrike the line on a previous line

P Interpret the line as a terminal prompt

= Interpret the line as carriage-control information

All else Start the line record on a new line

Page 84: 32883311 Sas Functions Pocketref

gamma

80 | SAS Functions Pocket Reference

returned quantile is prob (0 ≤ x < 1). gaminv is the inverse ofprobgam (p. 117).

Example: gaminv(0.5,9.0) → 8.668951184.

See also: gamma (p. 80).

gamma(x)

Returns the value of the gamma function Γ(x), defined as

where x is a nonzero real number but not a negative integer.For positive integers, gamma(n) equals fact(n-1). Examples:gamma(6) → 120. gamma(6.5) → 287.8852778. gamma(-6) →. (missing). See also: digamma (p. 60), fact (p. 66), gaminv(p. 79), lgamma (p. 98), probgam (p. 117), trigamma (p. 157).

geomean(x1[,x2,...]) 9+Returns the geometric mean of nonmissing arguments. If allthe arguments are missing values, the result is a missing value.The geometric mean is defined by

where all x ≥ 0. If any argument is zero, the geometric mean iszero. geomean uses fuzzing and returns zero if any x is within10-12 of zero. Example: x1=3; x2=4.5; x3=1; geomean(2,ofx1-x3,6,.) → 2.7663237344. See also: fuzz (p. 79), geomeanz(p. 80).

geomeanz(x1[,x2,...]) 9+Same as geomean (p. 80) but doesn’t fuzz close-to-zero x’s.

getoption(opt_name[,reporting_opts])

Returns the value of a SAS system option or graphics option.opt_name is the option name (with no trailing ‘=’). Passwordoptions, such as EMAILPW, return ‘xxxxxxxx’ instead of theactual password. Specify each of the reporting_opts as a sepa-

Γ x( ) t x 1– e t– td0

∫=

x1x2…xnn

Page 85: 32883311 Sas Functions Pocketref

harmean

List of Functions | 81

rate argument or separate them by spaces in a single argu-ment; use the values listed in the following table.

Examples:

getoption('center') → NOCENTER.

getoption('yearcutoff') → 1920.

getoption('PAGESIZE','keyword') → PAGESIZE=55.

getvarc(dataset_id,n)

Returns the character value of the n-th variable of the currentobservation in a SAS dataset. dataset_id is the dataset identifi-er that open (p. 111) returns. Use varnum (p. 161) or PROCCONTENTS to get n. See also: fetch (p. 67), fetchobs (p. 68),getvarn (p. 81), var<attr> (p. 160).

getvarn(dataset_id,n)

Returns the numeric value of the n-th variable of the currentobservation in a SAS dataset. dataset_id is the dataset identifi-er that open (p. 111) returns. Use varnum (p. 161) or PROCCONTENTS to get n. See also: fetch (p. 67), fetchobs (p. 68),getvarc (p. 81), var<attr> (p. 160).

harmean(x1[,x2,...]) 9+Returns the harmonic mean of nonmissing arguments. If allthe arguments are missing values, the result is a missing value.The harmonic mean is defined by

reporting_opts Returnsin Graphic units in inches

cm Graphic units in centimeters

keyword Option values in KEYWORD= format suitable for use in OPTIONS or GOPTIONS statements

n1x1----- 1

x2----- … 1

xn-----+ + +

-----------------------------------------

Page 86: 32883311 Sas Functions Pocketref

harmeanz

82 | SAS Functions Pocket Reference

where all x ≥ 0. If any argument is zero, the harmonic mean iszero. harmean uses fuzzing and returns zero if any x is within10-12 of zero. Example: x1=3; x2=4.5; x3=1; harmean(2,ofx1-x3,6,.) → 2.25. See also: fuzz (p. 79), harmeanz (p. 82).

harmeanz(x1[,x2,...]) 9+Same as harmean (p. 81) but doesn’t fuzz close-to-zero x’s.

hbound(array[,n])hboundn(array)

Returns the upper bound of array, or the upper bound ofdimension n (≥ 1) of a multidimensional array. If omitted, ndefaults to 1. hboundn(array) and hbound(array,n) areequivalent calls.

Examples:

array a{2:3,10:12,5} x1-x30;

hbound(a) or hbound(a,1) → 3.

hbound2(a) or hbound(a,2) → 12.

hbound3(a) or hbound(a,3) → 5.

Iterate over array ararray ar[4] x1 x2 x3 x4;do i=lbound(ar) to hbound(ar);

Do something with ar{i} hereend;

See also: dim (p. 60), lbound (p. 96).

hms(hour,minute,second)

Creates a SAS time from an hour, minute, and second. Theresult is the number of seconds after midnight.

Examples:

hms(0,0,15) → 15 (00:00:15).

hms(20,26,21.56) → 73581.56 (20:26:21.56).

See also: datetime (p. 55), dhms (p. 59), mdy (p. 101), time (p. 155), today (p. 156).

Page 87: 32883311 Sas Functions Pocketref

htmlencode

List of Functions | 83

hour(sas_time|sas_datetime)

Extracts the hour (0–23) from a SAS time or SAS datetime.

Example: hour(time()) → 22 (the time is 10:19:49.44 PM).

See also: minute (p. 102), second (p. 146).

htmldecode(html)

Decodes a string that contains HTML numeric character ref-erences and character entity references. htmldecode recog-nizes the character entity references listed in the followingtable. For numeric character references, use &#nnn; wherennn is a decimal number, or &#Xhhh; where hhh is a hexadec-imal number. All unrecognized entities are returned unmodi-fied in the output string.

Example: htmldecode('G&amp;&#84;') → ‘G&T’.

See also: htmlencode (p. 83), urldecode (p. 159).

htmlencode(str[,options])

Encodes a string as HTML text. By default, the characters >,<, and & are encoded as &gt;, &lt;, and &amp;. The returnedstring can be longer than str—assign the result to a sufficient-ly long output variable to avoid truncation. In SAS 9 and later,you can use the options listed in the following table to modifythe default encoding behavior.

Entity Decoded character&amp; &&lt; <&gt; >&quot; "

&apos; 9+ '

options Character Character entity referenceamp & &amp;

gt > &gt;

Page 88: 32883311 Sas Functions Pocketref

ibessel

84 | SAS Functions Pocket Reference

Examples:

htmlencode("I'm sure x<y") → ‘I'm sure x&lt;y’.

htmlencode("I'm sure x<y",'apos lt gt') → ‘I&apos;m sure x&lt;y’.

htmlencode('80'x,'7bit') → ‘&#x20AC;’ (€ symbol).

See also: htmldecode (p. 83), urlencode (p. 159).

ibessel(nu,x,kode)

Returns the value of the modified Bessel function of order nu(≥ 0) evaluated at x (≥ 0). When kode (a non-negative integer)equals 0, the Bessel function is returned; otherwise the valueof the following function is returned.

Example: ibessel(1,2,1) → 0.2152692892. See also: jbessel (p. 92).

ifc(condition,result_if_true,result_if_false[,result_if_missing]) 9+

Returns a character value based on whether a condition istrue, false, or missing. condition is a logical expression thatevaluates to true if it’s not zero or not missing. Ifresult_if_missing is omitted, result_if_false is returned ifcondition evaluates to missing. Example: The statement

grade=ifc(score>60,'pass','fail');

is equivalent to

lt < &lt;

apos ' &apos;

quot " &quot;

7bit Any character not rep-resented in 7-bit ASCII encoding

&#Xhhh; (Unicode; hhh is a hexadecimal number.)

options Character Character entity reference

e x– Imn x( )

Page 89: 32883311 Sas Functions Pocketref

indexw

List of Functions | 85

if score>60 then grade='pass'; else grade='fail';

See also: ifn (p. 85).

ifn(condition,result_if_true,result_if_false[,result_if_missing]) 9+

Returns a numeric value based on whether a condition is true,false, or missing. condition is a logical expression that evalu-ates to true if it’s not zero or not missing. If result_if_missing isomitted, result_if_false is returned if condition evaluates tomissing. Example: The statement

bonus=ifn(sales>500,10000,0);

is equivalent to

if sales>500 then bonus=10000; else bonus=0;

See also: ifc (p. 84).

index(str,substr)

Returns the first position in a string in which a case-sensitivesubstring appears, or zero (0) if the substring isn’t found.

Example: index('abcABC','AB') → 4.

See also: count (p. 52), find (p. 71), indexc (p. 85), indexw (p. 85).

indexc(str,substr1[,substr2,...])

Returns the first position in a string in which any of the char-acters in the substrings appear, or zero (0) if none are found.

Example: indexc('abcABC','xyz','12','CD') → 6.

See also: countc (p. 52), findc (p. 72), index (p. 85), indexw (p. 85).

indexw(str,word[,delims])

Returns the first position in which a word appears in a string,or zero (0) if the word isn’t found. One or more delims areword separators in str. The default delimiter is a space. SASignores leading and trailing delims in word. To match, wordmust begin and end on word boundaries. The boundaries are

Page 90: 32883311 Sas Functions Pocketref

input

86 | SAS Functions Pocket Reference

are delims, the beginning of str, and the end of str. If both strand word are empty strings or contain only spaces, the resultis 1. If word is an empty string or contains only spaces, and strcontains a character or numeric value, the result is 0.

Examples:

indexw('asdf adog dog','dog') → 11.

indexw('asdf adog dog',' ') → 0.

indexw('asdf@ adog@ dog',' dog','@') → 12.

indexw('asdf@adog%dog','adog','%@') → 6.

See also: index (p. 85), indexc (p. 85).

input(str,[?|??]informat.)

Uses an informat to read a string at run time. The ? optionsuppresses log messages. The ?? option suppresses log mes-sages and doesn’t let SAS set _ERROR_ to 1. informat deter-mines whether the result is numeric or character. You can useinput to convert character values to numeric values. informatis a literal argument; don’t use an expression or quoted string.The result’s default length is the informat width.

Examples:

input('$20,000.00',comma10.2) → Read as 20000.

input('1234',4.2) → Read as 12.34.

input('kidnap',$3.) → Read as ‘kid’.

input('20jan2005',date9.) → Read as 16456.

input('(34%)',percent5.) → Read as -0.34.

See also: inputc (p. 86), inputn (p. 87), put (p. 125), putc (p. 126), putn (p. 126), INPUT statement.

inputc(str,char_informat.[,w])

Uses a character informat to read a string at run time. Specify-ing w overrides any width specification in char_informat. Theresult’s default length is the length of str.

Page 91: 32883311 Sas Functions Pocketref

intck

List of Functions | 87

Example: inputc('sas','$upcase3.') → Read as ‘SAS’.

See also: input (p. 86), inputn (p. 87), put (p. 125), putc (p. 126), putn (p. 126).

inputn(str,num_informat.[,w[,d]])

Uses a numeric informat to read the string at run time. Speci-fying w or d overrides any width or decimal-places specifica-tion in num_informat.

Example: inputn('1234','F4.2') → Read as 12.34.

See also: input (p. 86), inputc (p. 86), put (p. 125), putc (p. 126), putn (p. 126).

int(x)

Returns the integer part of x, removing the fractional part. If xis within 10-12 of an integer, int returns that integer. If x ispositive, int returns the same result as floor (p. 74). If x isnegative, int returns the same result as ceil (p. 42). Examples:See ceil. See also: intz (p. 91), mod (p. 102), modz (p. 104),round (p. 133).

intck(interval,from,to)

Counts the number of intervals between from and to, whichare two SAS dates, two SAS times, or two SAS datetimes. Thegeneral form of interval is unit[mult][.start].

unit is the interval unit; use one of the values listed in the fol-lowing table.

unit Interval unit and shift

day Daily intervals, starting each day (shift days)

week Weekly intervals, starting Sunday (shift days: 1 = Sunday,..., 7 = Saturday)

weekday[daysW] Daily intervals, starting each day with Fri-Sat-Sun counted as the same day (shift days)

tenday Ten-day intervals, starting the 1st, 11th, and 21st of each month (shift ten-day periods)

Page 92: 32883311 Sas Functions Pocketref

intck

88 | SAS Functions Pocket Reference

mult (default 1) is a positive number that multiplies unit.year2 is a two-year interval, for example.

start (default 1) is the interval’s starting point. start > 1 shiftsthe start to a later point within the interval. The shifting unit,which depends on unit, is shown parenthesized in the preced-ing table. For example, year.3 is the yearly period March 1through February 28 (or 29) the following year.

The following table shows some example intervals.

semimonth Half-month intervals, starting the 1st and 16th of each month (shift semi-monthly periods)

month Monthly intervals, starting the 1st of each month (shift months)

qtr Quarterly (three-month) intervals, starting Jan 1, Apr 1, Jul 1, and Oct 1 (shift months)

semiyear Semiannual (six-month) intervals, starting Jan 1 and Jul 1 (shift months)

year Yearly intervals, starting Jan 1 (shift months)

second Second intervals, starting midnight (shift seconds)

minute Minute intervals, starting midnight (shift seconds)

hour Hour intervals, starting midnight (shift sec-onds)

Datetime intervals Prepend dt to any interval unit (dtmonth, for example)

interval Interval

day3 Three-day intervals starting on Sunday

week.7 Weekly with Saturday as the first day of the week

week6.13 Six-week intervals starting on second Fridays

week2 Biweekly intervals starting on first Sundays

unit Interval unit and shift

Page 93: 32883311 Sas Functions Pocketref

intck

List of Functions | 89

week1.1 Same as week

week.2 Weekly intervals starting on Mondays

week6.3 Six-week intervals starting on first Tuesdays

week6.11 Six-week intervals starting on second Wednes-days

week4 Four-week intervals starting on first Sundays

weekday1w Six-day week with Sunday as a weekend day

weekday35w Five-day week with Tuesday and Thursday as weekend days (the trailing W means that day 3 and day 5 are weekend days)

weekday17w Same as weekday

weekday67w Five-day week with Friday and Saturday as weekend days

weekday3.2 Three-weekday intervals with Saturday and Sunday as weekend days (with the cycle three-weekday intervals aligned to Monday 4 Jan 1960)

tenday4.2 Four ten-day periods starting at the second tenday period

semimonth2.2 Intervals from the sixteenth of one month through the fifteenth of the next month

month2.2 Feb–Mar, Apr–May, Jun–Jul, Aug–Sep, Oct–Nov, and Dec–Jan of the following year

month2 Jan–Feb, Mar–Apr, May–Jun, Jul–Aug, Sep–Oct, Nov–Dec

qtr3.2 Three-month intervals starting on Apr 1, Jul 1, Oct 1, and Jan 1

semiyear.3 Six-month intervals, Mar–Aug and Sep–Feb

year.10 Fiscal years starting in October

year2.7 Biennial intervals starting in July of even years

year2.19 Biennial intervals starting in July of odd years

year4.11 Four-year intervals starting in November of leap years (U.S. presidential elections)

interval Interval

Page 94: 32883311 Sas Functions Pocketref

intnx

90 | SAS Functions Pocket Reference

Examples:

intck('qtr','10jan2005'd,'01jul2005'd) → 2.

intck('week2.2','01jan2005'd,'31mar2005'd) → 6.

intck('year','01jan2005'd,'31dec2005'd) → 0.

intnx(interval,start,increment[,alignment])

Increments a date, time, or datetime by a specified interval orintervals. The starting point start is a SAS date, time, or date-time. increment is the integer number of intervals to shiftstart. The general form of interval, unit[mult][.start], isdescribed in intck (p. 87). alignment sets the position of theresult; use one of the values listed in the following table.

Examples:

intnx('year','05feb2005'd,3) → 17532 (1-Jan-2008).

intnx('dtday','8jun2005:08:25:30'dt,0) → 1433808000 (8-Jun-2005 00:00:00).

year4.35 Four-year intervals starting in November of even years between leap years (U.S. midterm elections)

dtmonth13 Thirteen-month intervals starting at midnight of 1-Jan-1960, such as 1-Nov-1957, 1-Dec-1958, 1-Jan-1960, 1-Feb-1961, and 1-Mar-1962

hour8.7 Eight-hour intervals starting at 6 AM, 2 PM, and 10 PM

alignment The returned date is aligned to thebeginning|b Beginning of the interval (the default)

middle|m Midpoint of the interval

end|e End of the interval

sameday|same|s Same calendar date with the corresponding interval increment

interval Interval

Page 95: 32883311 Sas Functions Pocketref

irr

List of Functions | 91

intnx('month','01jan2005'd,5,'b') → 16588 (1-Jun-2005).

intnx('month','01jan2005'd,5,'e') → 16617 (30-Jun-2005).

intrr(freq,cf0,cf1,...,cfn)

Returns the internal rate of return, as a fraction. freq (> 0) isthe number of payments over a specified base period of timeassociated with the desired IRR (set freq = 0 for continuouscompounding). Set freq = 3, for example, for a quarterly IRR(three-month base period) with monthly payments. cf0, cf1,...,cfn are two or more start-of-period cash payments (< 0) orinflows (> 0) occurring at equally spaced times 0, 1,..., n. irr(p. 91) returns the same result but as a percentage.

Example: intrr(1,-400,100,200,300) → 0.19438.

intz(x) 9+Returns the integer part of x, removing the fractional part andusing zero fuzzing. Example: Compare intz(1-1.e-13) → 0with int(1-1.e-13) → 1. See also: fuzz (p. 79), int (p. 87),mod (p. 102), modz (p. 104).

iorcmsg()

Returns the error-message text of the current value of theautomatic variable _IORC_. The result’s default length is 200._IORC_ is created when you use the MODIFY statement orthe SET statement with the KEY= option. The _IORC_ valueis internal and meant to be read in conjunction with the%sysrc autocall macro. Don’t set _IORC_ yourself.

iqr(x1[,x2,...]) 9+Returns the interquartile range (third quartile minus the firstquartile) of nonmissing arguments. If all the arguments aremissing values, the result is a missing value. Example: x1=3;x2=4; x3=5; iqr(-10,2,of x1-x3,.,123) → 3. See also:mad (p. 100), pctl (p. 113).

irr(freq,cf0,cf1,...,cfn)

Returns the internal rate of return. irr is the same as intrr(p. 91), except that the result is a percentage, not a fraction.

Page 96: 32883311 Sas Functions Pocketref

jbessel

92 | SAS Functions Pocket Reference

jbessel(nu,x)

Returns the value of the Bessel function of order nu (≥ 0)evaluated at x (≥ 0). Example: jbessel(1,2) → 0.5767248078.See also: ibessel (p. 84).

juldate(sas_date)

Converts a SAS date to a Julian date. If sas_date falls withinthe 100-year span defined by the YEARCUTOFF= systemoption, the result is yyddd; otherwise the result is yyyyddd (1≤ ddd ≤ 366). For example, if YEARCUTOFF=1920, juldatereturns 97001 for January 1, 1997 and 1878365 for December31, 1878. Use juldate7 (p. 92) to avoid two-digit years.

Example: juldate(today()) → 05245 (today is 2-Sep-2005).

See also: datejul (p. 55).

juldate7(sas_date)

Converts a SAS date to a Julian date. juldate7 is the same asjuldate (p. 92), except that its result always has seven digitsyyyyddd (1 ≤ ddd ≤ 366).

Example: juldate7(today()) → 2005245 (today is 2-Sep-2005).

See also: datejul (p. 55).

kcompare(str1,[start_pos,[len,]]str2)

(DBCS) Compares two strings and returns a negative numberif str1 < str2, zero if str1 = str2, or a positive number if str1 >str2. start_pos is the starting position in str1 to begin the com-parison. If start_pos is omitted, all of str1 is compared. Ifstart_pos < 0, str1 is assumed to be extended DBCS data thatcontains no SO/SI characters. len is the number of bytes tocompare. If len is omitted, all of str1 that follows start_pos iscompared (trailing spaces are ignored).

kcompress(str[,chars])

(DBCS) Removes characters from a string. chars lists thecharacter(s) to remove. If chars is omitted, all single- and dou-ble-byte spaces are removed. See also: kleft (p. 93), kright(p. 94), ktrim (p. 95).

Page 97: 32883311 Sas Functions Pocketref

klength

List of Functions | 93

kcount(str)

(DBCS) Counts double-byte characters in a string.

kcvt(text,in_encoding,new_encoding[,options])

(DBCS) Converts data from one encoding to another. SeeEncoding Values in SAS National Language Support (NLS):User’s Guide. The options are listed in the following table.

kindex(str,substr)

(DBCS) Scans a string from left to right and returns the firstposition at which a substring appears within the string, orzero (0) if the substring isn’t found. See also: kindexc (p. 93).

kindexc(str,substr1[,substr2,...])

(DBCS) Scans a string from left to right and returns the firstposition in the string at which any of the characters in thesubstrings appear, or zero (0) if no characters in any of thesubstrings are found. See also: kindex (p. 93).

kleft(str)

(DBCS) Left-aligns a string by removing leading DBCS spacesand SO/SI. See also: kcompress (p. 92), kright (p. 94), ktrim(p. 95).

klength(expression)

(DBCS) Returns the length of its argument. The length is theposition of the rightmost nonspace character in expression. If

options Meansnososi|noshift

No shift code or Hankaku characters

inplace Replace character data by conversion

kana Include Hankaku katakana characters in columns of character data

upcase Convert 2-byte alphabet to uppercase characters

lowcase Convert 2-byte alphabet to lowercase characters

kata2hira Convert Katakana data to Hiragana

hira2kata Convert Hiragana data to Katakana

Page 98: 32883311 Sas Functions Pocketref

klowcase

94 | SAS Functions Pocket Reference

expression is a missing value, the result is 1. If expression is anuninitialized numeric variable, klength returns 12 and logs anote that numbers have been converted to characters.

klowcase(str)

(DBCS) Converts all uppercase letters in a string to lowercase.See also: kupcase (p. 95).

kreverse(str)

(DBCS) Reverses the order of characters in a string.

kright(str)

(DBCS) Right-aligns a string by removing trailing DBCSspaces and SO/SI. See also: kcompress (p. 92), kleft (p. 93),ktrim (p. 95).

kscan(str,n[,delims])

(DBCS) Returns the n-th word in a string. See scan (p. 145)for a description of the parameters.

kstrcat(str1,str2[,str3,...])

(DBCS) Concatenates two or more single-byte or double-bytestrings, removing SO/SI pairs between the strings.

ksubstr(str,start_pos[,len])

(DBCS) Extracts a substring from a string. start_pos (≥ 1) isthe position of the first character in the substring. len is thelength of the substring to extract. If len is zero, negative, orgreater than the length of str after start_pos, ksubstr extractsthe remainder of str and logs an invalid-length note. If len isomitted, ksubstr extracts the remainder of str. The result’sdefault length is the length of str.

Example: ksubstr('kidnap',4,3) → ‘nap’.

See also: ksubstrb (p. 94).

ksubstrb(str,start_pos[,len])

(DBCS) Same as ksubstr (p. 94), except that start_pos and nare expressed in byte units. If len is greater than the length (inbyte units) of str after start_pos, ksubstrb extracts theremainder of str.

Page 99: 32883311 Sas Functions Pocketref

kupdateb

List of Functions | 95

ktranslate(str,to1,from1[,to2,from2,...])

(DBCS) Replaces all occurrences of from characters in strwith to characters. ktranslate is the same as translate(p. 156), except that ktranslate can translate a single-bytecharacter expression to a double-byte one, and vice versa.

ktrim(str)

(DBCS) Removes trailing DBCS spaces and SO/SI from astring. Is str is blank, the result is one blank. If the trimmedvalue is shorter than the target variable, SAS pads the resultwith spaces as it assigns it to the variable. See also: kcompress(p. 92), kleft (p. 93), kright (p. 94).

ktruncate(str,number,len)

(DBCS) Truncates a full-length number (stored as a double)to len bytes and pads the truncated bytes with 0s. This processduplicates the effect of storing numbers at less than full lengthand then reading them.

kupcase(str)

(DBCS) Converts all single-byte lowercase letters in a stringto uppercase. See also: klowcase (p. 94).

kupdate(str,start_pos[,n][,chars])

(DBCS) Replaces str with chars, starting at start_pos andreplacing n characters, where n ≤ the number of characters instr after start_pos. n and chars both are optional but at leastone must be specified. If n is omitted, kupdate uses all thecharacters in chars to replace str.

Examples:

kupdate('abcde',3,2) → ‘abe’.

kupdate('abcde',3,'xx') → ‘abxx’.

kupdate('abcde',3,2,'xx') → ‘abxxe’.

See also: kupdateb (p. 95).

kupdateb(str,start_pos[,n][,chars])

(DBCS) Same as kupdate (p. 95), except that start_pos and nare expressed in byte units.

Page 100: 32883311 Sas Functions Pocketref

kurtosis

96 | SAS Functions Pocket Reference

kurtosis(x1,x2,x3,x4[,x5,...])

Returns the kurtosis (peakedness) of nonmissing arguments.At least four nonmissing arguments are required or the resultis a missing value. Example: x1=-3; x2=0.5; x3=50; y1=.;kurtosis(2,of x1-x3,6,y1) → 4.5733146626.

kverify(str,substr1[,substr2,...])

(DBCS) Returns the position of the first character in a stringthat’s not in any substring, or zero if every character in str is inat least one substr.

lag(value)lagn(value)

Returns the queued value supplied in the n-th preceding lagfunction call. n ranges from 1 (the default) to 100. lag1 isequivalent to lag. value is numeric or character. Each occur-rence of a lagn in a program generates its own queue of val-ues. The lagn queue is initialized with n missing values.When lagn executes, its queue’s front value is removed andreturned; the remaining values are shifted frontward; and thenew value is placed at the back of the queue. Missing valuesare returned for the first n executions of lagn, after which thelagged values of value start to appear. When value is character,the result’s default length is 200. Example: See dif (p. 59).

largest(n,x1[,x2,...]) 9+Returns the n-th largest of nonmissing values. If n is missing,a noninteger, less than one, or greater than the number ofnonmissing values, then largest returns a missing value.Example: x1=-3.5; x2=-4; x3=5; largest(2,2,of x1-x3,6,.) → 5. See also: max (p. 101), min (p. 101), ordinal(p. 112), pctl (p. 113), smallest (p. 147).

lbound(array[,n])lboundn(array)

Returns the lower bound of array, or the lower bound ofdimension n (≥ 1) of a multidimensional array. If omitted, ndefaults to 1. lboundn(array) and lbound(array,n) areequivalent calls.

Page 101: 32883311 Sas Functions Pocketref

length

List of Functions | 97

Examples:

array a{2:3,10:12,5} x1-x30;

lbound(a) or lbound(a,1) → 2.

lbound2(a) or lbound(a,2) → 10.

lbound3(a) or lbound(a,3) → 1.

Iterate over array ararray ar[10:13] x1 x2 x3 x4;do i=lbound(ar) to hbound(ar);

Do something with ar{i} hereend;

See also: dim (p. 60), hbound (p. 82).

left(str)

Left-aligns a string by moving leading spaces to the end. Thelength of str doesn’t change. The result’s default length is thelength of str. Examples: The following table shows examples ofleft, right (p. 133), strip (p. 150), trim (p. 158), and trimn(p. 158).

Prints s="aaa |bbb" (two spaces follow the ‘aaa’)s=cat(left(' aaa '),'|',left(' bbb '));put s= $quote20.;

length(str)

Returns the length of a string, not counting trailing spaces,returning 1 if str is an empty string or a string of one or morespaces. If str is a number, length returns 12 and logs a notethat numbers have been converted to characters. Examples:

Function ‘ abc ’ ‘ ‘ (3 spaces) ‘’ (empty string)left() ‘abc ’ ‘ ’ (3 spaces) ‘ ’ (1 space)

right() ‘ abc’ ‘ ’ (3 spaces) ‘ ’ (1 space)

strip() ‘abc’ ‘’ (empty string) ‘’ (empty string)

trim() ‘ abc’ ‘ ’ (1 space) ‘ ’ (1 space)

trimn() ‘ abc’ ‘’ (empty string) ‘’ (empty string)

Page 102: 32883311 Sas Functions Pocketref

lengthc

98 | SAS Functions Pocket Reference

The following table shows examples of length, lengthc(p. 98), lengthm (p. 98), and lengthn (p. 98).

lengthc(str) 9+Returns the length of a string, counting trailing spaces. If str is anumber, lengthc returns 12 and logs a note that numbers havebeen converted to characters. Examples: See length (p. 97). Seealso: lengthm (p. 98), lengthn (p. 98).

lengthm(str) 9+Returns the number of bytes allocated to a string. If str is anumber, lengthm returns 12 and logs a note that numbershave been converted to characters. Examples: See length(p. 97). See also: lengthc (p. 98), lengthn (p. 98).

lengthn(str) 9+Returns the length of a string, not counting trailing spaces,returning 0 if str is an empty string or a string of one or morespaces. If str is a number, lengthn returns 12 and logs a notethat numbers have been converted to characters. Examples: Seelength (p. 97). See also: lengthc (p. 98), lengthm (p. 98).

lgamma(x)

Returns the natural log of the function gamma (p. 80), definedfor x > 0. lgamma(x) is the same as log(gamma(x)). Examples:lgamma(6) → 4.7874917428. lgamma(1) → 0. lgamma(0) → .(missing). See also: digamma (p. 60), log (p. 99), trigamma(p. 157).

libname(libref[,data_lib[,engine[,options]]])

Assigns or unassigns a libref for a SAS data library, returningzero (0) if successful; or nonzero on an error (failure), warn-

str length() lengthc() lengthm() lengthn()‘abc’ 3 3 3 3

‘abc ’ 3 6 6 3

‘ ’ (3 spaces) 1 3 3 0

‘’ (empty string) 1 1 1 0

42 (number) 12 12 12 12

Page 103: 32883311 Sas Functions Pocketref

log2

List of Functions | 99

ing (success), or note (success). For nonzero values, examinethe SAS log to determine whether the function was successful.

libref is the libref that is assigned to a SAS data library.

data_lib is the physical name of the SAS data library that’sassociated with libref, specified as required by the host OS. Ifdata_lib is omitted or is an empty string, libref disassociatesfrom data_lib.

engine is the engine that’s used to access SAS files opened indata_lib (specify 'remote' if you’re using a SAS/SHARE serv-er).

options is one or more space-delimited engine options.

See also: libref (p. 99), open (p. 111).

libref(libref)

Determines whether a libref is assigned, returning zero (0) iflibref is assigned; nonzero otherwise. Librefs are assigned to aSAS library by the LIBNAME statement or the functionlibname (p. 98).

log(x)

Returns the natural (base e) logarithm of x (> 0). log is theinverse of exp (p. 65). See also: log10 (p. 99), log2 (p. 99).

Examples:

log(0.5) → -0.693147181.

log(1.0) → 0.

log(constant('e')) → 1.

log(exp(2)) or exp(log(2)) → 2.

log10(x)

Returns the base-10 logarithm of x (> 0). Examples: log10(.01)→ -2. log10(1000) → 3. See also: log (p. 99), log2 (p. 99).

log2(x)

Returns the base-2 logarithm of x (> 0). Examples: log2(0.5)→ -1. log2(256) → 8. See also: log (p. 99), log10 (p. 99).

Page 104: 32883311 Sas Functions Pocketref

logbeta

100 | SAS Functions Pocket Reference

logbeta(a,b) 9+Returns the natural logarithm of the function beta (p. 17).See beta for a description of the parameters. logbeta is thesame as log(beta()). See also: gamma (p. 80), log (p. 99).

logcdf(dist,quantile[,param1,param2,...]) 9+Returns the natural logarithm of the left cumulative distribu-tion function cdf (p. 40). See cdf for a description of theparameters. logcdf is the same as log(cdf()). See also: log(p. 99).

logpdf(dist,quantile[,param1,param2,...])

Returns the natural logarithm of the probability density(mass) function pdf (p. 114). See cdf (p. 40) for a descriptionof the parameters. logpdf is the same as log(pdf()). Alias:logpmf. See also: log (p. 99).

logsdf(dist,quantile[,param1,param2,...])

Returns the natural logarithm of the survival function sdf(p. 146). See cdf (p. 40) for a description of the parameters.logsdf is the same as log(sdf()). See also: log (p. 99).

lowcase(str)

Converts all uppercase letters in a string to lowercase. Theresult’s default length is the length of str.

Examples:

lowcase('Place de l''Étoile') → ‘place de l'étoile’.

Make a word proper case (sets t = ‘Proper’)s='proPER';t=upcase(substr(s,1,1))||lowcase(substr(s,2));

See also: propcase (p. 120), upcase (p. 158).

mad(x1[,x2,...]) 9+Returns the median absolute deviation from the median ofnonmissing arguments. If all the arguments are missing val-ues, the result is a missing value. Example: x1=3; x2=4; x3=5;mad(-10,2,of x1-x3,.,999) → 1.5. See also: iqr (p. 91),median (p. 101), pctl (p. 113).

Page 105: 32883311 Sas Functions Pocketref

min

List of Functions | 101

max(x1,x2[,x3,...])

Returns the largest of nonmissing arguments. If all the argu-ments are missing values, the result is a missing value. Exam-ple: x1=-3; x2=-4; x3=5; max(2,of x1-x3,6,.) → 6. Seealso: largest (p. 96), min (p. 101), smallest (p. 147).

mdy(month,day,year)

Creates a SAS date from a month (1–12), day (1–31), and year.year is two or four digits. The YEARCUTOFF= system optiondefines the year value for two-digit years. The result is thenumber of days before (negative) or after (positive) 1-Jan-1960.

Examples:

mdy(5,10,1936) → -8636 (10-May-1936).

mdy(1,1,1960) → 0 (1-Jan-1960).

mdy(9,2,2005) → 16681 (2-Sep-2005).

mdy(5,33,2006) → . (missing—bad day).

See also: datetime (p. 55), dhms (p. 59), hms (p. 82), time (p. 155), today (p. 156).

mean(x1[,x2,...])

Returns the arithmetic mean (average) of nonmissing argu-ments. If all the arguments are missing values, the result is amissing value. Example: x1=3; x2=4; x3=5; mean(2,of x1-x3,6,.) → 4. See also: median (p. 101), n (p. 106), sum (p. 153).

median(x1[,x2,...]) 9+Returns the median of nonmissing arguments. If all the argu-ments are missing values, the result is a missing value. Exam-ple: x1=3; x2=4; x3=5; median(-10,2,of x1-x3,.,123)→ 3.5. See also: mad (p. 100), mean (p. 101), n (p. 106).

min(x1,x2[,x3,...])

Returns the smallest of nonmissing arguments. If all the argu-ments are missing values, the result is a missing value. Exam-ple: x1=-3; x2=-4; x3=5; min(2,of x1-x3,6,.) → -4. Seealso: largest (p. 96), max (p. 101), smallest (p. 147).

Page 106: 32883311 Sas Functions Pocketref

minute

102 | SAS Functions Pocket Reference

minute(sas_time|sas_datetime)

Extracts the minute (0–59) from a SAS time or SAS datetime.

Example: minute(time()) → 19 (the time is 10:19:49.44 PM).

See also: hour (p. 83), second (p. 146).

missing(num_expr|char_expr)

Returns one (1) if its argument contains a missing value; zerootherwise.

num_expr is a numeric expression. It’s defined as missing ifthe expression’s result is missing (.) or if it contains specialcharacters that you used to differentiate among missing val-ues. The special characters are the uppercase or lowercase let-ters A–Z and the underscore (_), preceded by a dot.

char_expr is the name of a character variable or an expressionthat evaluates to a character value. It’s defined as missing if theexpression’s result is empty or contains only spaces.

Examples:

missing(10) → 0.

missing('abc') → 0.

missing(.) → 1.

missing(.a) → 1.

missing(._) → 1.

missing('') → 1.

missing(' ') → 1.

missing(' ') → 1.

See also: call missing (p. 21), n (p. 106), nmiss (p. 109).

mod(x,divisor)

Returns the remainder when x is divided by divisor, returningzero if the result is within 10-12 of zero or divisor to avoid mostunexpected floating-point results. Nonzero results have thesame sign as x; the sign of divisor is ignored.

Page 107: 32883311 Sas Functions Pocketref

mod

List of Functions | 103

Examples:

The following table shows examples of mod and modz (p. 104)

Split mydata randomly into three (near) equal-sized datasets

First, add a uniform random variabledata mydata;set mydata;rand=ranuni(12345);

run;

Use the new variable to randomize the row orderproc sort data=mydata;by rand;

run;

Pick every n-th row for each sub-datasetdata dsrand1 dsrand2 dsrand3;set mydata;drop rand pick_obs;pick_obs = mod(_N_,3);select (pick_obs);when(0) output dsrand1;when(1) output dsrand2;otherwise output dsrand3;

end;run;

See also: int (p. 87), intz (p. 91).

x divisor mod() modz()10 3 1 1

3 10 3 3

–10 4 –2 –2

–10 –4 –2 2

0.3 –0.1 0 0.1

1.5 0.6 0.3 0.3

0.9 0.3 0 5.551115E–17

12345678 10**4 5678 5678

Page 108: 32883311 Sas Functions Pocketref

modulec

104 | SAS Functions Pocket Reference

modulec([ctrl_str,]module_name[,arg1,arg2,...])

Calls an external routine and returns a character value. Fordetails see call module (p. 21). See also: modulen (p. 104).

moduleic([ctrl_str,]module_name[,arg1,arg2,...])

Calls an external routine and returns a character value (inIML environment only). For details see call module (p. 21).See also: modulein (p. 104).

modulein([ctrl_str,]module_name[,arg1,arg2,...])

Calls an external routine and returns a numeric value (in IMLenvironment only). For details see call module (p. 21). Seealso: moduleic (p. 104).

modulen([ctrl_str,]module_name[,arg1,arg2,...])

Calls an external routine and returns a numeric value. Fordetails see call module (p. 21). See also: modulec (p. 104).

modz(x,divisor) 9+Returns the remainder when x is divided by divisor, usingzero fuzzing. Nonzero results have the same sign as x; the signof divisor is ignored. Examples: See mod (p. 102). See also: fuzz(p. 79), int (p. 87), intz (p. 91).

month(sas_date)

Extracts the month (1–12) from a SAS date.

Example: month(today()) → 9 (today is 2-Sep-2005).

See also: day (p. 56), qtr (p. 126), week (p. 163), weekday (p. 163), year (p. 163).

mopen(dir_id,member_name[,open_mode[,rec_len[,rec_format]]])

Opens the file identified by directory ID and member nameand returns a unique numeric file identifier if successful; zerootherwise. Use this ID to identify the open file to other filefunctions as you would use the ID returned by fopen (p. 75).

dir_id is the directory identifier that dopen (p. 62) returns.

member_name is the member name in the directory.

Page 109: 32883311 Sas Functions Pocketref

mopen

List of Functions | 105

open_mode is the type of access to the file. Omit open_modeto use input mode ('i' mode), or use one of the values listedin the following table.

rec_len is the file’s logical record length. Omit rec_len or set itto zero to use the existing logical record length.

rec_format is the file’s record format. Omit rec_format to usethe existing record format, or use one of the values listed inthe following table.

open_mode Modea Append mode allows writing new records after the

current end-of-file

i Input mode allows reading only (the default)

o Output mode defaults to the OPEN mode specified in the OS option in the FILENAME statement or the function filename (p. 69); if no OS option is specified, this option allows writing new records at the beginning of the file (output mode overwrites the file’s current contents without warning)

s Sequential input mode is used for pipes and other sequential devices such as hardware ports

u Update mode allows both reading and writing

w Sequential update mode is used for pipes and other sequential devices such as ports

rec_format Record format

b Data are interpreted as binary data

d Use the default record format

e Use an editable record format

f File contains fixed-length records

p File contains printer carriage control in OS-depen-dent record format (for z/OS datasets with FBA or VBA record format, use p)

v File contains variable-length records

Page 110: 32883311 Sas Functions Pocketref

mort

106 | SAS Functions Pocket Reference

To free memory, you should close open files when they’re nolonger needed. In some OSes SAS automatically closes filesopened within a DATA step when the step ends, whereas inother OSes you must fclose (p. 67) files explicitly. For detailssee the SAS documentation for your OS.

See also: dopen (p. 62), fopen (p. 75), open (p. 111).

mort(init_amt,pmt,int_rate,num_periods)

Returns an amortization value. init_amt is the initial amount.pmt is the periodic payment. int_rate is the interest rate,expressed as a fraction. num_periods (≥ 0) is the integer num-ber of compounding periods. Provide any three nonmissingarguments and mort returns the fourth.

Example:

Returns pmtmort(100000,.,0.06/12,30*12) → 599.55.

n(x1[,x2,...])

Counts nonmissing arguments.

Examples:

n(10,11) → 2.

n(10,.,11) → 2.

n(.,.,.) → 0.

x1=3; x2=4; x3=5;n(.,.,2,of x1-x3,6) → 5.

See also: missing (p. 102), nmiss (p. 109).

netpv(int_rate,freq,cf0,cf1,...,cfn)

Returns the net present value as a fraction. int_rate is theinterest rate, expressed as a fraction, over some base period oftime. freq (> 0) is the number of payments during the baseperiod of time used for int_rate (set freq = 0 for continuousdiscounting). cf0, cf1,..., cfn are two or more start-of-periodcash payments (< 0) or inflows (> 0) occurring at equallyspaced times 0, 1,..., n.

Page 111: 32883311 Sas Functions Pocketref

nldate

List of Functions | 107

Example: netpv(0.06,0.5,-400,100,200,300) → 58.9065.

See also: npv (p. 110).

nldate(sas_date,descriptors) 9+Converts a SAS date to a text date by using date-format modi-fiers. Use the case-sensitive descriptors listed in the followingtable to specify how the output date is formatted.

Examples:

nldate('24feb2003'd,'%B-%d.log') → ‘February-24.log’.

descriptors Date format%% The % character

%a Mon–Sun

%A Monday–Sunday

%b Jan–Dec

%B January–December

%C January–December (space padding)

%d 01–31 (zero padding)

%e 1–31 (space padding)

%F Monday–Sunday (space padding)

%j 001–366 (zero padding)

%m 01–12 (zero padding)

%o 1–12 (space padding)

%u 1–7 (Monday-Sunday)

%U U algorithm

%V V algorithm

%w 0–6 (Sunday-Saturday)

%W W algorithm

%y 00–99 (two-digit year)

%Y 1970–2069 (four-digit year)

Page 112: 32883311 Sas Functions Pocketref

nldatm

108 | SAS Functions Pocket Reference

If OPTIONS LOCALE=English_unitedstatesnldate('24feb2003'd,'%A') → Monday.

If OPTIONS LOCALE=German_Germanynldate('24feb2003'd,'%A') → Montag.

See also: nldatm (p. 108), nltime (p. 109).

nldatm(sas_datetime,descriptors) 9+Converts a SAS datetime to a text datetime by using datetime-format modifiers. See nldate (p. 107) and nltime (p. 109) forthe date and time format descriptors. descriptors is case-sensi-tive. Example: nldatm(datetime(),'%Y-%m-%d %h:%M') →2006-04-12 16:56.

nliteral(str) 9+Converts a string to a SAS name literal (an n-literal). str isn’tconverted if, under the default rules, it’s a SAS variable name.These default rules are in effect when the SAS system optionVALIDVARNAME=V7; that is, str isn’t converted if it beginswith an English letter or underscore, all subsequent charactersare English letters, underscores, or digits, and its length is 32or fewer characters. If you assign the result to a variable that’stoo short, nliteral returns a blank string, logs an error mes-sage, and sets _ERROR_ to 1. The result’s default length is200. If str contains an ampersand (&), a percent sign (%), ormore double quotes than single quotes, nliteral encloses theresult in single quotes; otherwise it encloses it in doublequotes.

Examples:

nliteral("abc_123") → abc_123.

nliteral("This and That") → "This and That"N.

nliteral("Ed's profits (%)") → 'Ed' 's profits (%)'N.

nliteral('"double quotes"') → ' "single quotes" 'N.

nliteral("'single quotes'") → " 'single quotes' "N.

See also: compare (p. 46), dequote (p. 57), nvalid (p. 110).

Page 113: 32883311 Sas Functions Pocketref

normal

List of Functions | 109

nltime(sas_time|sas_datetime,descriptors) 9+Converts a SAS time or SAS datetime to a text time by usingtime-format modifiers. Use the descriptors listed in the fol-lowing table to specify how the output time is formatted.

Examples:

If OPTIONS LOCALE=English_unitedstatesnltime('12:39:43't,'%I%p') → 00 PM.

If OPTIONS LOCALE=German_Germanynltime('12:39:43't,'%I%p') → 00 nachm.

See also: nldate (p. 107), nldatm (p. 108).

nmiss(x1[,x2,...])

Counts missing arguments.

Examples:

nmiss(10,11) → 0.

nmiss(10,.,11) → 1.

nmiss(.,.,.) → 3.

x1=3; x2=4; x3=5;nmiss(.,.,2,of x1-x3,6) → 2.

See also: missing (p. 102), n (p. 106).

normal(seed)

Same as rannor (p. 131).

descriptors Time format%% The % character

%h 00–23 (24-hour clock)

%i 01–12 (12-hour clock)

%M 00–59 (minutes)

%p AM or PM

%s 00–59 (seconds)

Page 114: 32883311 Sas Functions Pocketref

not<chartype>

110 | SAS Functions Pocket Reference

not<chartype>(str[,start_pos]) 9+Group of functions that searches a string for a character thatisn’t a particular type and returns the first position at whichit’s found, or zero (0) if it’s not found. See any<chartype>(p. 10) for a description of <chartype> and start_pos.

Examples:

s='ABC.123 _xyZ_';

notdigit(s) → 1.

notdigit(s,0) → 0.

notupper(s,1) → 4.

notupper(s,-4) → 4.

notalpha(s,999) → 0.

notalpha(s,-999) → 13.

notcntrl(s) → 1.

note(dataset_id)

Returns a unique numeric observation ID for the currentobservation of a SAS dataset. dataset_id is the dataset identifi-er that open (p. 111) returns. Use the returned ID with point(p. 116) to later return to the marked observation. To deletethe ID, use dropnote (p. 62). See also: fnote (p. 75), rewind(p. 133).

npv(int_rate,freq,cf0,cf1,...,cfn)

Returns the net present value. npv is the same as netpv(p. 106), except that int_rate is expressed as a percentage, nota fraction.

nvalid(str[,validvarname]) 9+Determines whether a string is valid for use as a SAS variablename in a SAS statement. nvalid returns one (1) if str is validor zero (0) otherwise (trailing spaces are ignored).

validvarname is one of the validity rules listed in the followingtable. If validvarname is omitted, nvalid uses the value of the

Page 115: 32883311 Sas Functions Pocketref

open

List of Functions | 111

system option VALIDVARNAME= to determine whether stris a valid SAS variable name.

See also: compare (p. 46), dequote (p. 57), nliteral (p. 108).

open([dataset_name[,open_mode]])

Opens a SAS dataset and returns a unique numeric datasetidentifier if successful; zero otherwise. Use this ID to identifythe open dataset to other dataset functions.

dataset_name is the name of the SAS dataset or SQL view toopen. It takes the form[libref.]member_name[(dataset_options)]

and defaults to _LAST_. libref is the libref assigned to a SASlibrary by the LIBNAME statement or the function libname(p. 98). Use any dataset_options except OBS= and FIRSTOBS=to control how the dataset is read.

open_mode is the type of access to the dataset. Omitopen_mode to use random-access mode ('i' mode), or useone of the values listed in the following table.

validvarname Means str is a valid SAS variable name if itv7 Begins with an English letter or underscore, all

subsequent characters are English letters, under-scores, or digits, and its length is 32 or fewer char-acters

any Contains 32 or fewer characters of any type, includ-ing spaces

nliteral Is in the form of a SAS name literal ('name'N) or if it’s a valid name under VALIDVARNAME=V7

open_mode Mode

i Opens the dataset in random-access input mode (the default); values can be read but not modified (if the engine doesn’t support random access, open defaults to in mode automatically and logs a warning)

Page 116: 32883311 Sas Functions Pocketref

ordinal

112 | SAS Functions Pocket Reference

You should close (p. 43) an open dataset when it’s no longerneeded. If you open a dataset within a DATA step, SAS closesit automatically when the step ends.

Example: open('mydata.dsn','i') → 1.

See also: attrc (p. 13), attrn (p. 15), dopen (p. 62), fopen (p. 75), mopen (p. 104).

ordinal(n,x1,x2[,x3,...])

Sorts values in ascending order and returns the n-th value inthe sorted list. Missing values are sorted as the lowest values.If n is missing, a noninteger, less than one, or greater than thenumber of values, ordinal returns a missing value. ordinaldiffers from smallest (p. 147) in that smallest ignores miss-ing values whereas ordinal counts them.

Examples:

x1=-3.5; x2=-4; x3=5;

ordinal(1,8,6,of x1-x3,6,.) → . (missing value in list).

ordinal(2,8,6,of x1-x3,6,.) → -4.

ordinal(5,8,6,of x1-x3,6,.) → 6.

ordinal(6,8,6,of x1-x3,6,.) → 6.

ordinal(7,8,6,of x1-x3,6,.) → 8.

ordinal(5.5,8,6,of x1-x3,6,.) → . (missing—bad n).

ordinal(999,8,6,of x1-x3,6,.) → . (missing—bad n).

See also: largest (p. 96), max (p. 101), min (p. 101), pctl (p. 113).

in Opens the dataset in input mode; observations are read sequentially and can be revisited

is Opens the dataset in input mode; observations are read sequentially but can’t be revisited

open_mode Mode

Page 117: 32883311 Sas Functions Pocketref

pctl

List of Functions | 113

pathname(libref|fileref[,search_ref])

Returns the physical pathname of a SAS library or an externalfile, or returns a blank for a bad reference. The result’s defaultlength is 200.

fileref is a fileref assigned to the external file by the FILE-NAME statement or the function filename (p. 69).

libref is the libref assigned to a SAS library by the LIBNAMEstatement or the function libname (p. 98).

If fileref and libref have identical names, set search_ref to 'f'to search for a fileref, or 'l' to search for a libref. If search_refis omitted, pathname searches first for a fileref and, if itdoesn’t exist, then for a libref.

Example:

Get the name of the external file associated with myfiledata _null_;length path $ 200;assigned=fileref('myfile');if (assigned=0) then do;path=pathname('myfile');put path=;

end;run;

See also: fexist (p. 68), fileexist (p. 69), fileref (p. 71).

pctl[def](percentage,x1[,x2,...]) 9+Returns a percentile of nonmissing arguments. If all the argu-ments are missing values, the result is a missing value.

def is a digit from 1 to 5 (default 5) that specifies the definitionof the percentile to compute. pctl() and pctl5() are equiva-lent function calls. In general, differences among percentiledefinitions are pronounced only for small samples or samplesthat contain many ties. For information about how pctl cal-culates percentiles see PROC UNIVARIATE in Base SAS Pro-cedures Guide.

percentage (0 ≤ percentage ≤ 100) is the percentile to compute.

Page 118: 32883311 Sas Functions Pocketref

pdf

114 | SAS Functions Pocket Reference

Examples:

pctl(25,2,4,1,3) → 1.5 (lower quartile).

pctl2(25,2,4,1,3) → 1 (lower quartile, def = 2).

pctl(100/3,2,4,1,3) → 2 (lower tertile).

pctl3(100/3,2,4,1,3) → 2 (lower tertile, def = 3).

pctl(50,2,4,1,3) → 2.5 (median).

pctl(200/3,2,4,1,3) → 3 (upper tertile).

pctl(75,2,4,1,3) → 3.5 (upper quartile).

See also: largest (p. 96), max (p. 101), min (p. 101), ordinal (p. 112), smallest (p. 147).

pdf(dist,quantile[,param1,param2,...])

Returns the probability density (mass) function. See cdf(p. 40) for a description of the parameters. Alias: pmf. Example:pdf('normal',1.96) → 0.058441. See also: logpdf (p. 100),quantile (p. 127), sdf (p. 146).

peek(addr[,len])

Same as peeklong (p. 115) but for 32-bit systems only. Seealso: addr (p. 10), call poke (p. 22), peekc (p. 114).

peekc(addr[,len])

Same as peekclong (p. 114) but for 32-bit systems only. Seealso: addr (p. 10), call poke (p. 22), peek (p. 114).

peekclong(addr[,len])

Returns the character contents stored at an address in memo-ry on 32-bit or 64-bit systems. addr is the memory address.len (1 ≤ len ≤ 32767, default 8) is the length of the result.

Example:

Prints contents=‘ab’ (32-bit system)x='abcde';addr=addrlong(x);contents=peekclong(addr,2);put contents=;

Page 119: 32883311 Sas Functions Pocketref

perm

List of Functions | 115

See also: addrlong (p. 10), call pokelong (p. 22), peeklong (p. 115), ptrlongadd (p. 125).

peeklong(addr[,len])

Returns the numeric contents stored at an address in memoryon 32-bit or 64-bit systems. addr is the memory address. len(32-bit: 1 ≤ len ≤ 4, default 4. 64-bit: 1 ≤ len ≤ 8, default 8) isthe length of the result.

Example:

Prints contents=1 (32-bit system)length x $4;x=put(1,ib4.);addr=addrlong(x);contents=peeklong(addr,4);put contents=;

See also: addrlong (p. 10), call pokelong (p. 22), peekclong (p. 114).

perm(n[,r])

Returns the number of permutations of n elements taken r ata time (0 ≤ r ≤ n). If r is omitted, perm(n) returns the factorialof n. A permutation is any set or subset of items where orderis significant. Permutations are distinct from combinations,for which order doesn’t matter. The number of permutationsis n!/(n–r)! where n and r are integers and the symbol !denotes the factorial. perm(n,r) is the same as fact(n)/fact(n-r). See also: comb (p. 45), fact (p. 66).

Examples:

perm(8,0) → 1.

perm(8,1) → 8.

perm(8,2) → 56.

perm(8,6) → 20160.

perm(8,8) → 40320.

perm(8) → 40320.

Page 120: 32883311 Sas Functions Pocketref

point

116 | SAS Functions Pocket Reference

perm(4,8) → . (missing—n < r).

perm(8.5,4) → . (missing—noninteger n).

perm(int(8.5),4) → 1680.

See also: call allperm (p. 18), call ranperk (p. 28), call ranperm (p. 28).

point(dataset_id,note_id))

Moves to the observation marked previously by note (p. 110),returning zero (0) if successful; nonzero otherwise. dataset_idis the dataset identifier that open (p. 111) returns. note_id isthe note identifier that note returns. point prepares the pro-gram to read from the dataset. The Dataset Data Vector(DDV) isn’t updated until a read is done by using fetch (p. 67)or fetchobs (p. 68). See also: dropnote (p. 62), fpoint (p. 77),rewind (p. 133).

poisson(lambda,n)

Returns the probability that an observation from a Poissondistribution with mean lambda (≥ 0) is less than or equal to n(≥ 0). The probability that an observation is equal to a givenvalue of n is the difference of two probabilities from the Pois-son distribution for n and n–1. Example: poisson(2.5,2) →0.5438131159. See also: cdf (p. 40).

probbeta(x,a,b)

Returns the probability that an observation from a beta distri-bution with shape parameters a (> 0) and b (> 0) is less thanor equal to x (0 ≤ x ≤ 1). probbeta is the inverse of betainv(p. 17). Example: probbeta(0.2,2,1) → 0.04. See also: beta(p. 17), cdf (p. 40).

probbnml(prob_success,num_trials,num_successes)

Returns the probability that an observation from a binomialdistribution with probability of success prob_success (0 ≤prob_success ≤ 1) and number of trials num_trials (≥ 1) is lessthan or equal to the number of successes num_successes (0 ≤num_successes ≤ num_trials). The probability that an observa-tion equals a given value of num_successes is the difference oftwo probabilities from the binomial distribution for

Page 121: 32883311 Sas Functions Pocketref

probgam

List of Functions | 117

num_successes and num_successes–1. Example: probbnml(0.5,10,5) → 0.62304687. See also: cdf (p. 40).

probbnrm(x,y,corr)

Returns the probability that an observation (X,Y) from a stan-dard bivariate normal distribution with mean 0, variance 1,and correlation coefficient corr (-1 ≤ corr ≤ 1), is less than orequal to (x, y) (that is, the probability that X ≤ x and Y ≤ y).The probability is

where r is corr and u and v represent the random variables xand y. Example: probbnrm(0.5,-0.5,0.5) → 0.272239352.See also: cdf (p. 40).

probchi(x,df[,nc])

Returns the probability that an observation from a chi-squaredistribution with degrees of freedom df (> 0, nonintegerallowed) and noncentrality parameter nc (≥ 0) is less than orequal to x (≥ 0). If nc is omitted or is zero, probchi uses thecentral chi-square distribution. probchi is the inverse of cinv(p. 43). Example: probchi(5.5,10) → 0.144621493. See also:cdf (p. 40), cnonct (p. 44).

probf(x,ndf,ddf[,nc])

Returns the probability that an observation from an F distri-bution with numerator degrees of freedom ndf (> 0, noninte-ger allowed), denominator degrees of freedom ddf (> 0,noninteger allowed), and noncentrality parameter nc (≥ 0) isless than or equal to x (≥ 0). If nc is omitted or is zero, probfuses the central F distribution. probf is the inverse of finv(p. 73). Example: probf(3.5,4,5) → 0.899032601. See also:cdf (p. 40), fnonct (p. 75).

probgam(x,a)

Returns the probability that an observation from a gammadistribution with shape parameter a (> 0) is less than or equalto x (≥ 0). probgam is the inverse of gaminv (p. 79). Example:

12π 1 r2–( )------------------------- u2 2ruv v2+–

2 1 r2–( )-----------------------------------– v uddexp

∞–

y

∫∞–

x

Page 122: 32883311 Sas Functions Pocketref

probhypr

118 | SAS Functions Pocket Reference

probgam(2,3) → 0.323323583. See also: cdf (p. 40), gamma(p. 80).

probhypr(pop_size,category_size,sample_size,x[,odds_ratio])

Returns the probability that an observation from an extendedhypergeometric distribution with population size pop_size(≥ 1), number of items in the category of interest category_size(0 ≤ category_size ≤ pop_size), sample size sample_size (0 ≤sample_size ≤ pop_size), and odds ratio odds_ratio (≥ 0,default 1), is less than or equal to x (max(0, category_size +sample_size–pop_size) ≤ x ≤ min(category_size, sample_size)).Example: probhypr(100,50,10,2) → 0.045823577. See also:cdf (p. 40).

probit(prob)

Returns the prob-th quantile from the standard normal distri-bution. The probability that an observation from the standardnormal distribution is less than or equal to the returned quan-tile is prob (0 < x < 1). Extreme results may be truncated to fallbetween -8.222 and 7.941. probit is the inverse of probnorm(p. 119). Example: probit(0.025) → -1.95996398. See also: cdf(p. 40).

probmc(dist,quantile,prob,df,num_treatments[,sample_size_params])

Returns the quantile or the probability from various distribu-tions for multiple comparisons of means.

dist is one of the values listed in the following table. quantile isthe quantile of dist. prob is the left probability of dist.

dist Distributiondunnett1 One-sided Dunnett

dunnett2 Two-sided Dunnett

maxmod Maximum modulus

range Studentized range

williams Williams

Page 123: 32883311 Sas Functions Pocketref

probt

List of Functions | 119

prob is the probability that the random variable is less thanquantile. Calculate p-values as 1-prob (prob=0.95 computesthe critical value for a 5% significance level, for example).Specify either quantile or prob but not both; use a dot (.) forthe omitted parameter.

df is the degrees of freedom (a missing value is interpreted asan infinite value).

sample_size_params is a set of num_treatments parameterswhose meaning depends on dist. If sample_size_params isomitted, equal sample sizes are assumed.

Example: probmc('williams',.,.95,42,6) → 1.806524758.

See also: cdf (p. 40).

probnegb(prob_success,num_successes,num_failures)

Returns the probability that an observation from a negativebinomial distribution with probability of success prob_success(0 ≤ prob_success ≤ 1) and number of successes num_successes(≥ 1) is less than or equal to num_failures (≥ 0). The probabil-ity that an observation equals a given value of num_failures isthe difference of two probabilities from the negative binomialdistribution for num_failures and num_failures–1. Example:probnegb(0.5,4,3) → 0.5. See also: cdf (p. 40).

probnorm(x)

Returns the probability that an observation from the standardnormal distribution is less than or equal to x. probnorm is theinverse of probit (p. 118). Example: probnorm(1.96) →≈0.975. See also: cdf (p. 40).

probt(x,df[,nc])

Returns the probability that an observation from a Student’s tdistribution with degrees of freedom df (> 0, nonintegerallowed) and noncentrality parameter nc is less than or equalto x. If nc is omitted or is zero, probt uses the central t distri-bution. probt is the inverse of tinv (p. 156). The significancelevel of a two-tailed t test is (1-probt(abs(x),df))*2. Exam-ple: probt(0.95,10) → 0.817746627. See also: cdf (p. 40),tnonct (p. 156).

Page 124: 32883311 Sas Functions Pocketref

propcase

120 | SAS Functions Pocket Reference

propcase(str[,delims]) 9+Converts all the words in a string to proper case (title case).propcase sets to uppercase each letter preceded by a space,forward slash, hyphen, open parenthesis, dot, or tab (otherletters are set to lowercase). To change the default delimiters,set delims to one or more characters. If you specify delims thenthe default delimiters, including the space, are no longer ineffect. The result’s default length is the length of str.

Examples:

propcase('john q. PUBLIC') → ‘John Q. Public’.

propcase('WINSTON-SALEM, n.c. (u.s.)') → ‘Winston-Salem, N.C. (U.S.)’.

propcase('place de l''étoile') → ‘Place De L'étoile’.

propcase('place de l''étoile' "'") → ‘Place de l'Étoile’.

propcase('FLANN O''BRIEN', " '") → ‘Flann O'Brien’.

See also: lowcase (p. 100), upcase (p. 158).

prxchange(prx_id|perl_regex,n,str) 9+Matches and replaces a pattern.

prx_id is the pattern identifier that prxparse (p. 121) returns.

perl_regex is a Perl regular expression (see prxparse).

n is the number of times to search for a match and replace amatching pattern. If n = -1, all matching patterns are replaced.

str is the character expression on which to perform a searchand replace.

See also: call prxchange (p. 22).

prxmatch(prx_id|perl_regex,str) 9+Returns the position at which a pattern-match is found in astring, or zero (0) if it’s not found. prx_id is the pattern identi-fier that prxparse (p. 121) returns. perl_regex is a Perl regularexpression (see prxparse). Example: See prxparse. See also:call prxsubstr (p. 26).

Page 125: 32883311 Sas Functions Pocketref

prxparse

List of Functions | 121

prxparen(prx_id) 9+Returns the last bracket match for which there is a match in apattern. prx_id is the pattern identifier that prxparse (p. 121)returns. prxparen can find the largest capture-buffer numberthat can be passed to call prxposn (p. 25).

Example:

Prints paren1=2 paren2=1 paren3=3data _null_;str1='A cat is here';str2='A dog is here';str3='An ape is here';prx_id=prxparse('/(dog)|(cat)|(ape)/');

pos=prxmatch(prx_id,str1);if pos then paren1=prxparen(prx_id);

pos=prxmatch(prx_id,str2);if pos then paren2=prxparen(prx_id);

pos=prxmatch(prx_id,str3);if pos then paren3=prxparen(prx_id);

put paren1= paren2= paren3=;call prxfree(prx_id);

run;

prxparse(perl_regex) 9+Parses and compiles a Perl regular expression (pattern) andreturns a unique numeric pattern identifier that other PRXfunctions can use. prxparse returns a missing value if a pars-ing error occurs. The corresponding function for SAS regularexpressions is rxparse (p. 135).

perl_regex is a Perl regular expression. If perl_regex is a con-stant or if it uses the /o option, perl_regex is compiled once.Successive prxparse calls don’t recompile but return the pat-tern ID from the preceding compile. This behavior meansthat you don’t need to use an initialization block (if _N_=1) toinitialize Perl regular expressions. This compile-once behav-ior occurs only in a DATA step, for all other uses perl_regex isrecompiled on each prxparse call. You can use metacharac-

Page 126: 32883311 Sas Functions Pocketref

prxparse

122 | SAS Functions Pocket Reference

ters to construct a Perl regular expression. The following tablelists common metacharacters.

Metacharacter Means\ Mark the next character as either a special charac-

ter, a literal, a back reference, or an octal escape. "n" matches the character "n"; "\n" matches a new-line character; "\\" matches "\"; "\(" matches "(".

| Use an OR condition when comparing alphanu-meric strings.

^ Match the position at the beginning of the string.

$ Match the position at the end of the string.

* Match the preceding subexpression zero or more times (equivalent to {0}). "zo*" matches "z" and "zoo".

+ Match the preceding subexpression one or more times (equivalent to {1,}). "zo+" matches "zo" and "zoo" but not "z".

? Match the preceding subexpression zero or one time (equivalent to {0,1}). "do(es)?" matches the "do" in "do" or "does".

{n} n is a non-negative integer that matches exactly n times. "o{2}" matches the two o’s in "food" but not the "o" in "Bob".

{n,} n is a non-negative integer that matches n or more times (o{1,} is equivalent to "o+"; o{0,} is equiva-lent to "o*"). "o{2,}" matches all the o’s in "foooood" but not the "o" in "Bob".

{n,m} m and n (n ≤ m) are non-negative integers match-ing at least n but at most m times (o{0,1} is equiva-lent to "o?"). "o{1,3}" matches the first three o’s in "fooooood". Don’t put a space on either side of the comma.

dot (.) Match any single character except a newline. To match any character, including a newline, use a pattern like "[.\n]".

Page 127: 32883311 Sas Functions Pocketref

prxparse

List of Functions | 123

(pattern) Match a pattern and create a capture buffer for the match. To retrieve the position and length of the captured match, use call prxposn (p. 25). To retrieve the value of the capture buffer, use prxposn (p. 124). To match a parenthesis charac-ter, use "\(" or "\)".

x|y Match either x or y. "z|food" matches "z" or "food"; "(z|f)ood" matches "zood" or "food".

[xyz] A character set that matches any one of the enclosed characters. "[abc]" matches the "a" in "plain".

[^xyz] A character set that matches any character that isn’t enclosed. "[^abc]" matches the "p" in "plain".

[a-z] A range of characters that matches any character in the range. "[a-z]" matches any lowercase alpha-betic character in the range "a" through "z".

[^a-z] A range of characters that matches any character that isn’t in the range. "[^a-z]" matches any char-acter that isn’t in the range lowercase "a" through "z".

\b Match a word boundary (the position between a word and a space). "er\b" matches the "er" in "nev-er" but not the "er" in "verb".

\B Match a non-word boundary. "er\B" matches the "er" in "verb" but not the "er" in "never".

\d Match a digit character (equivalent to [0-9]).

\D Match a nondigit character (equivalent to [^0-9]).

\s Match a whitespace character: space, tab, form feed, and so on (equivalent to [\f\n\r\t\v]).

\S Match a nonwhitespace character (equivalent to [^\f\n\r\t\v]).

\t Match a tab character (equivalent to "\x09").

\w Match a word character including the underscore (equivalent to [A-Za-z0-9_]).

Metacharacter Means

Page 128: 32883311 Sas Functions Pocketref

prxposn

124 | SAS Functions Pocket Reference

Example:

Validate a list of telephone numbers that have the form (xxx) xxx-xxxx or xxx-xxx-xxxxdata _null_;if _N_=1 then do;paren="\([2-9]\d\d\) ?[2-9]\d\d-\d\d\d\d";dash="[2-9]\d\d-[2-9]\d\d-\d\d\d\d";regexp="/(" ||paren|| ")|(" ||dash|| ")/";retain re;re=prxparse(regexp);if missing(re) then do;put "Bad regexp: " regexp;stop;

end;end;length first last home business $ 16;input first last home business;if ^prxmatch(re,home) thenput "Bad home phone: " first last home;

if ^prxmatch(re,business) thenput "Bad bus phone: " first last business;

run;

See also: call prxchange (p. 22), call prxdebug (p. 23), call prxfree (p. 23), call prxnext (p. 24), call prxposn (p. 25), call prxsubstr (p. 26), prxchange (p. 120), prxmatch (p. 120), prxparen (p. 121), prxposn (p. 124).

prxposn(prx_id,buffer,str) 9+Returns the value for a capture buffer. A capture buffer is partof a match, enclosed in parentheses, in a regular expression.prxposn returns the capture-buffer text directly, without theneed for substr (p. 151). To return a capture buffer, use the

\W Match a non-word character (equivalent to [^A-Za-z0-9_]).

\n Match n, where n is a positive integer that refers back to captured matches. "(.)\1" matches two consecutive identical characters.

Metacharacter Means

Page 129: 32883311 Sas Functions Pocketref

put

List of Functions | 125

results of call prxchange (p. 22), call prxnext (p. 24),call prxsubstr (p. 26), or prxmatch (p. 120).

prx_id is the pattern identifier that prxparse (p. 121) returns.

buffer is a number identifying the capture buffer from whichto retrieve a value. If buffer is zero, prxposn returns the entirematch. If buffer is between 1 and the number of open paren-theses, prxposn returns the value for that capture buffer. Ifbuffer is greater than the number of open parentheses thenprxposn returns a missing value.

str is the text from which to extract capture buffers.

See also: call prxposn (p. 25).

ptrlongadd(ptr_addr[,add_amt])

Performs pointer arithmetic and returns a pointer address as astring on 32-bit or 64-bit systems. ptr_addr is a pointeraddress, as a string. add_amt is the amount to add to theaddress (add_amt can be negative). See also: addrlong (p. 10).

put(value,format.)

Applies a format to a character or numeric value at run time.format must be the same type (character or numeric) as value.put always returns a string. If value is numeric, the result isright aligned. If value is character, the result is left aligned. Tooverride the default alignment, add an alignment specifica-tion to format: -L (left), -C (center), or -R (right). format is aliteral argument; don’t use an expression or quoted string. Theresult’s default length is the format width. Use put to convert anumeric value to a character value. put writes (or produces areformatted result) only while it executes. To preserve theresult, assign it to a variable.

Examples:

put(1234,hex4.) → ‘04D2’.

put('1234',F4.2) → ‘12.34’.

See also: input (p. 86), inputc (p. 86), inputn (p. 87), putc (p. 126), putn (p. 126), PUT statement.

Page 130: 32883311 Sas Functions Pocketref

putc

126 | SAS Functions Pocket Reference

putc(str,char_format.[,w])

Applies the character format char_format to the string str atrun time. Specifying w overrides any width specification inchar_format. The result’s default length is the length of str.

Example: putc('123*#6','$revers.',4) → ‘*321’.

See also: input (p. 86), inputc (p. 86), inputn (p. 87), put (p. 125), putn (p. 126).

putn(num,num_format.[,w[,d]])

Applies a numeric format num_format to the number num atrun time. Specifying w or d overrides any width or decimal-places specification in num_format. The result’s default lengthis 200.

Example: putn(10000,'dollar12.2') → $10,000.00.

See also: input (p. 86), inputc (p. 86), inputn (p. 87), put (p. 125), putc (p. 126).

pvp(par,cpn_rate,cpn_freq,remaining_cpns,time_to_next_cpn,yld_to_mat)

Returns the present value of a bond. par (> 0) is the par (face)value. cpn_rate (0 ≤ cpn_rate < 1) is the nominal per-yearcoupon rate, expressed as a fraction. cpn_freq (> 0) is the inte-ger number of coupons per year. remaining_cpns (> 0) is theinteger number of remaining coupons. time_to_next_cpn (> 0and ≤ 1/cpn_freq) is the time from now to the next coupondate, expressed as a fractional number of years. yld_to_mat(> 0) is the nominal per-year yield-to-maturity, expressed as afraction.

Example: pvp(1000,0.01,4,14,0.33/2,0.1) → 743.168.

qtr(sas_date)

Returns the quarter (1–4) of the year in which a SAS date falls.

Example: qtr(today()) → 3 (today is 2-Sep-2005).

See also: day (p. 56), month (p. 104), week (p. 163), weekday (p. 163), year (p. 163), yyq (p. 165).

Page 131: 32883311 Sas Functions Pocketref

quote

List of Functions | 127

quantile(dist,prob[,param1,param2,...]) 9+Returns the quantile from the specified distribution. prob (0 <prob < 1) is a random variable. See cdf (p. 40) for a descrip-tion of dist and param.

Examples:

quantile('bern',0.75,0.25) → 0.

quantile('chisq',0.5,10) → 9.341817765.

quantile('expo',0.95) → 2.995732273.

quantile('normal',0.95) → 1.64485362.

quantile('normal',0.975) → 1.959963984.

quantile('poisson',0.9,2) → 4.

quantile('t',0.95,20) → 0.

quantile('uniform',0.5,5,10) → 7.5.

See also: pdf (p. 114), sdf (p. 146).

quote(str)

Adds double quotes to a string and doubles existing doublequotes. Quoted single quotes are reduced. The target variablemust be long enough to contain str (including trailing spaces),leading and trailing quotes, and any doubled quotes; if thisvariable is too short, quote returns a blank string and logs aninvalid-argument note. The result’s default length is 200.

Examples: See the following table for examples.

See also: dequote (p. 57).

str quote()'x"y' "x""y"

'x''y' "x'y"

'Don''t' "Don't"

'Don''t "worry"' "Don't ""worry"""

Page 132: 32883311 Sas Functions Pocketref

ranbin

128 | SAS Functions Pocket Reference

ranbin(seed,num_trials,prob_success)

Returns a random variate from a binomial distribution withmean np and variance np(1-p), where n is the integer number oftrials (num_trials ≥ 1) and p is the probability of success (0 <prob_success < 1). seed is an integer (< 231-1). If seed ≤ 0, thetime of day is used to initialize the seed stream. To change seedduring execution, use call ranbin (p. 26) instead of ranbin.

rancau(seed)

Returns a random variate from a Cauchy distribution withlocation parameter 0 and scale parameter 1. seed is an integer(< 231-1). If seed ≤ 0, the time of day is used to initialize theseed stream. To change seed during execution, use callrancau (p. 26) instead of rancau.

Example:

Generate a Cauchy variate x with location parameter alpha and scale parameter betax=alpha+beta*rancau(seed);

rand(dist[,param1,param2,...])

Generates a random number from the continuous and dis-crete distributions listed in the following table. The location,scale, shape, degrees-of-freedom, and other params vary bydistribution. You can truncate dist to its first four characters.Use call streaminit (p. 35) to set the seed for rand.

Distribution Function callBernoulli rand('bernoulli',prob_success)

Beta rand('beta',a,b)

Binomial rand('binomial',prob_success,num_trials)

Cauchy rand('cauchy')

Chi-square rand('chisquare',df)

Erlang rand('erlang',a)

Exponential rand('exponential')

F rand('f',ndf,ddf)

Page 133: 32883311 Sas Functions Pocketref

ranexp

List of Functions | 129

Examples:

rand('bern',0.5) → 0.

rand('normal') → -1.24987951.

ranexp(seed)

Returns a random variate from an exponential distributionwith parameter 1. seed is an integer (< 231-1). If seed ≤ 0, thetime of day is used to initialize the seed stream. To changeseed during execution, use call ranexp (p. 27) instead ofranexp.

Examples:

Generate an exponential variate x with parameter lambdax=ranexp(seed)/lambda;

Generate an extreme-value variate x with location parameter alpha and scale parameter betax=alpha-beta*log(ranexp(seed));

Gamma rand('gamma',a)

Geometric rand('geometric',prob_success)

Hypergeometric rand('hypergeometric',pop_size,category_size,sample_size)

Lognormal rand('lognormal')

Negative binomial

rand('negbinomial',prob_success,num_successes)

Normal rand('normal'[,mean,stdev])

Poisson rand('poisson',lambda)

t rand('t',df)

Tabled rand('table',prob1,prob2,...)

Triangular rand('triangle',height)

Uniform rand('uniform')

Weibull rand('weibull',a,b)

Distribution Function call

Page 134: 32883311 Sas Functions Pocketref

rangam

130 | SAS Functions Pocket Reference

Generate a geometric variate x with parameter px=floor(-ranexp(seed)/log(1-p));

rangam(seed,a)

Returns a random variate from a gamma distribution withshape parameter a (> 0). seed is an integer (< 231-1). If seed ≤0, the time of day is used to initialize the seed stream. Tochange seed during execution, use call rangam (p. 27) insteadof rangam.

Examples:

Generate a gamma variate x with shape parameter alpha and scale betax=beta*rangam(seed,alpha);

Generate a chi-square variate x with 2*alpha degrees of freedom (provided 2*alpha is an integer)x=2*rangam(seed,alpha);

Generate an Erlang variate x (the distribution of the sum of n independent exponential variates whose means are beta)x=beta*rangam(seed,n);

Generate a beta variate x with parameters alpha and betay1=rangam(seed,alpha);y2=rangam(seed,beta);x=y1/(y1+y2);

range(x1[,x2,...])

Returns the range of nonmissing arguments. The range is theabsolute difference between the largest and the smallest val-ues. If all the arguments are missing values, the result is amissing value.

Examples:

range(-2,1,6) → 8.

range(-2,1,-6) → 7.

range(10) → 0.

range(-10,-10) → 0.

x1=3; x2=4; x3=5;range(2,of x1-x3,6,.) → 4.

Page 135: 32883311 Sas Functions Pocketref

rantbl

List of Functions | 131

rank(char)

Returns a character’s position in the ASCII or EBCDIC collat-ing sequence. Examples: rank('A') → 65 (ASCII). rank('A')→ 193 (EBCDIC). See also: byte (p. 18), collate (p. 45).

rannor(seed)

Returns a random variate from a normal distribution withmean 0 and variance 1. seed is an integer (< 231-1). If seed ≤ 0,the time of day is used to initialize the seed stream. To changeseed during execution, use call rannor (p. 27) instead ofrannor. rannor is the same as normal (p. 109).

Examples:

Generate a normal variate x with mean m2 and variance s2x=m2+sqrt(s2)*rannor(seed);

Generate a lognormal variate x with mean exp(m2+s2/2) and variance exp(2*m2+2*s2)-exp(2*m2+s2)x=exp(m2+sqrt(s2)*rannor(seed));

ranpoi(seed,lambda)

Returns a random variate from a Poisson distribution withmean lambda (≥ 0). seed is an integer (< 231-1). If seed ≤ 0, thetime of day is used to initialize the seed stream. To changeseed during execution, use call ranpoi (p. 28) instead ofranpoi.

rantbl(seed,prob1,...,probn)

Returns a random variate (positive integer) from the proba-bility mass function defined by prob1 through probn.

rantabl returns 1 with probability prob1, 2 with prob2,..., nwith probn, where 0 ≤ probi ≤ 1 for i = 1, 2,..., n. If the probssum to less than one, rantabl can return n+1. If the probssum to greater than one, rantabl ignores the extra probs.

seed is an integer (< 231-1). If seed ≤ 0, the time of day is usedto initialize the seed stream. To change seed during execution,use call rantbl (p. 29) instead of rantbl.

Page 136: 32883311 Sas Functions Pocketref

rantri

132 | SAS Functions Pocket Reference

Example:

Assign to x one of the values m1, m2, or m3 with probabilities of occurrence p1, p2, and p3, respectively (p1+p2+p3=1)array m{3} m1-m3;x=m{rantbl(seed,of p1-p3)};

rantri(seed,height)

Returns a random variate from a triangular distribution onthe interval (0,1) with mode height (0 < height < 1). seed is aninteger (< 231-1). If seed ≤ 0, the time of day is used to initial-ize the seed stream. To change seed during execution, usecall rantri (p. 29) instead of rantri.

Example:

Generate a triangular variate x on [a,b] with mode c (a ≤ c ≤ b)x=(b-a)*rantri(seed,(c-a)/(b-a))+a;

ranuni(seed)

Returns a random variate from a uniform distribution on theinterval (0,1). seed is an integer (< 231-1). If seed ≤ 0, the timeof day is used to initialize the seed stream. To change seedduring execution, use call ranuni (p. 29) instead of ranuni.ranuni is the same as uniform (p. 158).

Example:

Generate a uniform variate x on the interval (b,a+b)x=a*ranuni(seed)+b;

repeat(str,n)

Returns a string repeated n+1 times (n ≥ 0). The result’sdefault length is 200. Example: repeat('Go',2) → ‘GoGoGo’.

resolve(macro_expr)

Resolves a macro expression and returns it. The result’sdefault length is 200. For details see Resolve in SAS MacroLanguage: Reference. See also: symget (p. 153).

reverse(str)

Reverses the order of characters in a string. The result’sdefault length is the length of str. Example: reverse('Neverodd or even') → ‘neve ro ddo reveN’.

Page 137: 32883311 Sas Functions Pocketref

round

List of Functions | 133

rewind(dataset_id)

Moves the dataset pointer to the beginning of a SAS dataset,returning zero (0) if successful; nonzero otherwise. dataset_idis the dataset identifier that open (p. 111) returns; the datasetcan’t be opened in 'is' mode. If a WHERE clause is active, thepointer moves to the first satisfying observation. To read thefirst observation, call fetch (p. 67) after rewind. See also:frewind (p. 78), point (p. 116).

right(str)

Right-aligns a string by moving trailing spaces to the start. Thelength of str doesn’t change. The result’s default length is thelength of str. Examples: See left (p. 97). See also: strip (p. 150),trim (p. 158), trimn (p. 158).

rms(x1[,x2,...]) 9+Returns the root mean square of nonmissing arguments. If allthe arguments are missing values, the result is a missing value.The root mean square is defined by

.

Example: x1=3; x2=4.5; x3=10; rms(-2,of x1-x3,6,.) →5.8180752831. See also: mean (p. 101), n (p. 106), sqrt (p. 149),sum (p. 153).

round(x[,unit]) 9+Rounds x to the nearest multiple of unit (> 0) or to the nearestinteger if unit is omitted. When unit < 1 and not the reciprocalof an integer, round—unlike roundz (p. 134)—fuzzes to try tomake the result agree with decimal arithmetic.

Examples:

round(18,4) → 20.

round(386.667) → 387.

round(386.667,5) → 385.

round(386.667,10) → 390.

x12 x2

2 … xn2+ + +

n----------------------------------------

Page 138: 32883311 Sas Functions Pocketref

rounde

134 | SAS Functions Pocket Reference

round(386.667,100) → 400.

round(386.667,0.1) → 386.7.

round(386.667,0.111) → 386.613.

round(386.667,0.25) → 386.75.

round(386.667,10.5) → 388.5.

See ceil (p. 42) for other examples.

See also: floor (p. 74), int (p. 87), rounde (p. 134), trunc (p. 158).

rounde(x[,unit]) 9+rounde is the same as round (p. 133), except that when x ishalfway between the two nearest multiples of unit, roundealways returns an even multiple rather than the multiple withthe greater absolute value. Example: Compare rounde(7.5,3)→ 6 with round(7.5,3) → 9. See also: roundz (p. 134).

roundz(x[,unit]) 9+Rounds x to the nearest multiple of unit (> 0), using zero fuzz-ing. If omitted, unit defaults to one. roundz is the same asround (p. 133), except that roundz returns an even multiplewhen x is exactly halfway between the two nearest multiplesof unit whereas round returns the multiple with the greaterabsolute value when x is approximately halfway between thetwo nearest multiples. Also, when unit < 1 and not the recip-rocal of an integer, roundz’s result might not agree exactlywith the decimal-arithmetic result whereas round fuzzes totry to make its result agree. Example: Compare roundz(2.5-10**(-12)) → 2 with round(2.5-10**(-12)) → 3. See also:fuzz (p. 79), rounde (p. 134).

rxmatch(rx_id,str)

Returns the position at which a pattern-match is found in astring, or zero (0) if it’s not found. rx_id is the pattern identifierthat rxparse (p. 135) returns. Example: See rxparse. See also:call rxchange (p. 30), call rxfree (p. 30), call rxsubstr(p. 30).

Page 139: 32883311 Sas Functions Pocketref

rxparse

List of Functions | 135

rxparse(sas_regex)

Parses and compiles a SAS regular expression (pattern) andreturns a unique numeric pattern identifier that other RXfunctions can use. rxparse returns a missing value if a pars-ing error occurs. sas_regex is a SAS regular expression madeup of the elements listed in the following table. The corre-sponding function for Perl regular expressions is prxparse(p. 121).

Element Means"string" Match a substring consisting of the charac-

ters in string.

letter Match the uppercase or lowercase letter in a substring.

digit Match the digit in a substring.

dot (.) Match a dot (.) in a substring.

underscore (_) Match an underscore (_) in a substring.

? Match any one character in a substring.

colon (:) Match any sequence of zero or more charac-ters in a substring.

$'pattern' or$"pattern"

Match any one character in a substring. Use a hyphen (-) to specify a range of alphanu-meric variables (rxparse("$'a-z'") matches any lowercase letter, for example). See “Character Classes” on page 137.

~'char_class' or ^'char_class' or ~"char_class" or ^"char_class"

Match any one character that is not matched by the corresponding character class. Use a hyphen (-) to specify a range of alphanumer-ic variables (rxparse("^'a-d'") excludes the letters a–d from a match, for example). See “Character Class Complements” on page 138.

pattern1 pattern2orpattern1||pattern2

Select any substring that pattern1 matches followed immediately by any substring that pattern2 matches (with no intervening spac-es).

Page 140: 32883311 Sas Functions Pocketref

rxparse

136 | SAS Functions Pocket Reference

pattern1|pattern2 Select any substring that pattern1 matches or any substring pattern2 matches. You can use an exclamation point (!) instead of a vertical bar (|).

(pattern) Match a substring that contains pattern. You can use parentheses to force the order of operations.

[pattern] or{pattern}

Match a substring that contains pattern or an empty string.

pattern* Match zero or more consecutive strings that pattern matches.

pattern+ Match one or more consecutive strings that pattern matches.

@pos Match the position of a variable if the next character is located in the column specified by the integer pos. @0 matches end-of-line. If pos is negative, it matches –pos positions from end-of-line.

reuse_char_class Reuse a character class that you defined pre-viously. See “Reusing Character Classes” on page 139.

pattern_abbrev Specify ways to shorten pattern representa-tion. See “Pattern Abbreviations” on page 140 and “Default Character Classes” on page 137.

balanced_symbols Specify the number of nested parentheses, brackets, braces, or less-than/greater-than symbols in a mathematical expression. See “Matching Balanced Symbols” on page 140.

special_symbol Specify a position in a string, or a score val-ue. See “Special Symbols” on page 141.

score_value Select the pattern with the highest score val-ue. See “Scores” on page 142.

<pattern> Retrieve a matched substring for use in a change expression. See “Tag Expression” on page 142.

Element Means

Page 141: 32883311 Sas Functions Pocketref

rxparse

List of Functions | 137

Character Classes

Using a character-class element is a shorthand way to specifya range of values for matching. You can use default classes(p. 137), define your own classes (p. 138), use class comple-ments (p. 138), or reuse classes (p. 139).

Default Character Classes

Specify a default character class with a dollar sign ($) followedby a single uppercase or lowercase letter, as listed in the fol-lowing table. A hyphen at the beginning or end of a characterclass is treated as a member of the class, not as a range symbol.See also “Character Class Complements” on page 138.

change_expr Specify a pattern change operation that replaces a string containing a matched sub-string by concatenating values to the replacement string. See “Change Expres-sions” on page 142.

change_item Specify items used for string manipulation. See “Change Items” on page 144.

Class Matches any$a or $A Alphabetic uppercase or lowercase letter in a sub-

string ($'a-zA-Z')

$c or $C Character allowed in a version 6 SAS name found in a substring ($'0-9a-zA-Z_')

$d or $D Digit in a substring ($'0-9')

$i or $I Initial character allowed in a version 6 SAS name found in a substring ($'a-zA-Z_')

$l or $L Lowercase letter in a substring ($'a-z')

$u or $U Uppercase letter in a substring ($'A-Z')

$w or $W Whitespace character (space, tab, backspace, carriage return, and so on) in a substring

Element Means

Page 142: 32883311 Sas Functions Pocketref

rxparse

138 | SAS Functions Pocket Reference

Example:

Prints pos1=8 match1=x pos2=1 match2=Adata _null_;str='ABC123 xyz';rx_id1=rxparse("$L");rx_id2=rxparse("$U");pos1=rxmatch(rx_id1,str);pos2=rxmatch(rx_id2,str);match1=substr(str,pos1,1);match2=substr(str,pos2,1);put pos1= match1= pos2= match2=;call rxfree(rx_id1);call rxfree(rx_id2);

run;

User-defined Character Classes

A user-defined character class begins with a dollar sign ($)and is followed by a quoted string. A character class matchesany one character within the quotes. A hyphen (-) indicates arange of values. See also “Character Class Complements” onpage 138.

Example:

Prints pos1=8 match1=x pos2=4 match2=1data _null_;str='ABC123 xyz';rx_id1=rxparse("$'vwxyz'");rx_id2=rxparse("$'0-9'");pos1=rxmatch(rx_id1,str);pos2=rxmatch(rx_id2,str);match1=substr(str,pos1,1);match2=substr(str,pos2,1);put pos1= match1= pos2= match2=;call rxfree(rx_id1);call rxfree(rx_id2);

run;

Character Class Complements

A character class complement begins with a caret (^) or a til-de (~) and is followed by a quoted string. A character class

Page 143: 32883311 Sas Functions Pocketref

rxparse

List of Functions | 139

complement matches any one character that’s not matched bythe corresponding character class.

Example:

Prints pos1=4 match1=1 pos2=7 match2=<space>data _null_;str='ABC123 xyz';rx_id1=rxparse('^u');rx_id2=rxparse("~'A-z1-9'");pos1=rxmatch(rx_id1,str);pos2=rxmatch(rx_id2,str);match1=substr(str,pos1,1);match2=substr(str,pos2,1);put pos1= match1= pos2= match2=;call rxfree(rx_id1);call rxfree(rx_id2);

run;

Reusing Character Classes

You can reuse character classes that you defined previously byusing one of the patterns $n, ~n, or ^n.

$n reuses the n-th character class, where n is a nonzero inte-ger. If n < 0, count backwards from the last pattern to identifythe character class for –n. For example,

rxparse("$'AB' $1 $'XYZ' $2 $-2")

is equivalent to

rxparse("$'AB' $'AB' $'XYZ' $'XYZ' $'AB'").

$1, $2, and $-2 are replaced by AB, XYZ, and AB, respectively.

~n or ^n reuses the complement n-th character class, where nis a nonzero integer. For example,

rxparse($'Al' $1 $'Jo' $2 $'Li' $3 ~2)

is equivalent to

rxparse($'Al' $'Al' $'Jo' $'Jo'$'Li' $'Li' $'Al' $'Li').

~2 matches patterns 1 (Al) and 3 (Li) and excludes 2 (Jo).

Page 144: 32883311 Sas Functions Pocketref

rxparse

140 | SAS Functions Pocket Reference

Pattern Abbreviations

You can use the elements listed in the following table in yourpattern.

Example:

Prints pos1=1 len1=4 pos2=20 len2=4data _null_;str='woodchucks eat firewood';rx_id1=rxparse("$p 'wood'");rx_id2=rxparse("'wood' $s");pos1=rxmatch(rx_id1,str); ‘wood’ in woodchuckspos2=rxmatch(rx_id2,str); ‘wood’ in firewoodcall rxsubstr(rx_id1,str,pos1,len1);call rxsubstr(rx_id2,str,pos2,len2);put pos1= len1= pos2= len2=;call rxfree(rx_id1);call rxfree(rx_id2);

run;

Matching Balanced Symbols

You can match mathematical expressions containing multiplesets of balanced parentheses, brackets, braces, and less-than/greater-than symbols. Both the symbols and the expressionswithin the symbols are part of the match. $(n) or $[n] or ${n}or $<n> indicates nesting level n, where n is a positive integer.

Example:

Prints match_pos=2 len=9data _null_;str='(((a+b)*5)/42)';rx_id=rxparse("$(2)");

Pattern Matches a$f or $F Floating-point number

$n or $N SAS name

$p or $P Prefix

$q or $Q Quoted string

$s or $S Suffix

Page 145: 32883311 Sas Functions Pocketref

rxparse

List of Functions | 141

call rxsubstr(rx_id,str,pos,len);match_pos=rxmatch(rx_id,str); Matches ‘((a+b)*5)’put match_pos= len=;call rxfree(rx_id);

run;

Special Symbols

You can use the special symbols listed in the following table inthe pattern.

Example:

Prints pos=6 match=owdata _null_;str='How now brown cow?';rx_id=rxparse("@3:\ow");pos=rxmatch(rx_id,str); Matches ‘ow’ in nowmatch=substr(str,pos,2);put pos= match=;call rxfree(rx_id);

run;

Symbol Means\ Set the beginning of a match to the current position.

/ Set the end of a match to the current position. If you use a backslash (\) in one alternative of a union (|), you must use a forward slash ( /) in all alternatives of the union, or in a position preceding or following the union.

$# Request the match with the highest score, regardless of the starting position. The position of this symbol with-in the pattern isn’t significant.

$- Scan a string from right to left. The position of this symbol within the pattern isn’t significant. Don’t con-fuse a hyphen (-) used to scan a string with a hyphen used in arithmetic operations.

$@ Require the match to begin where the scan of the text begins. The position of this symbol within the pattern isn’t significant.

Page 146: 32883311 Sas Functions Pocketref

rxparse

142 | SAS Functions Pocket Reference

Scores

When a pattern is matched by more than one substring begin-ning at a specific position, the longest substring is selected. Tochange this selection criterion, assign a score value to eachsubstring by using the # symbol followed by an integer.

The score for any substring begins at zero. When #n appearsin the pattern, n is added to the score. If two or more match-ing substrings begin at the same leftmost position, SAS selectsthe one with the highest score; if both have the same score,SAS selects the longer one. The following table lists score rep-resentations.

Tag Expression

To assign a substring of the searched string to a character var-iable, use the expression name=<pattern>. The substring thatmatches this expression is assigned to the variable name.

If you specify <pattern> (without the name=), SAS automati-cally assigns the first occurrence of the pattern to the variable_1, the second occurrence to _2, and so on. This assignment iscalled tagging. SAS tags the corresponding substring of thematched string.

Change Expressions

If you find a substring that matches a pattern, you can changethat substring by specifying, in the rxparse argument, thepattern expression, the TO keyword, and the change expres-

Item Means#n Add n to the score

#*n Multiply the score by nonnegative n

#/n Divide the score by positive n

#=n Assign the value of n to the score

#>n Find a match if the current score exceeds n

Page 147: 32883311 Sas Functions Pocketref

rxparse

List of Functions | 143

sion. You can specify multiple change expressions by separat-ing them with commas.

A change operation replaces a matched string by concatenat-ing values to the replacement string. This operation concate-nates all characters to the left of the match, the charactersspecified in the change expression, and all characters to theright of the match.

rxparse allows multiple parallel operations. For example, inrxparse("x TO y,y TO x") x in a substring is substituted for y,and y in a substring is substituted for x. A change expressioncan include the items listed in the following table.

Example:

Search for a semicolon (;) and replace it with a spacedata mydata (drop=rx_id); set mydata;rx_id=rxparse("$';' to ' '");call rxchange(rx_id,999,old_str);

run;

Item Concatenates the"string" Contents of string.

name Name, possibly in a different case.

number Number.

Dot (.) Dot (.).

Underscore (_) Underscore (_).

=n Value of the n-th tagged substring if n is posi-tive, or the –n-th-from-the-last tagged substring if n is negative. In a parallel change expression, the n-th or –n-th-from-the-last tag is counted within the component of the parallel change expression that yielded the match, and not over the entire parallel change expression.

== Entire matched substring.

Page 148: 32883311 Sas Functions Pocketref

saving

144 | SAS Functions Pocket Reference

Change Items

You can use the items listed in the following table to manipu-late the replacement string. Each item positions the cursorwithout affecting the replacement string.

See also: call rxchange (p. 30), call rxfree (p. 30), call rxsubstr (p. 30), rxmatch (p. 134).

saving(fut_amt,pmt,int_rate,num_periods)

Returns a periodic-savings value. fut_amt (≥ 0) is the futureamount at the end of num_periods. pmt (≥ 0) is the fixed peri-odic payment. int_rate (≥ 0) is the periodic interest rate,

Item Means@n Move the pointer to column n where the next string added

to the replacement string will start.

@= Move the pointer one column past the end of the matched substring.

>n Move the pointer to the right to column n. If the pointer is already to the right of column n, the pointer isn’t moved.

>= Move the pointer to the right, one column past the end of the matched substring.

<n Move pointer to the left to column n. If the pointer is already to the left of column n, the pointer isn’t moved.

<= Move the pointer to the left, one column past the end of the matched substring.

+n Move the pointer n columns to the right.

-n Move the pointer n columns to the left.

-L Left-align the result of the previous item or expression in parentheses.

-R Right-align the result of the previous item or expression in parentheses.

-C Center the result of the previous item or expression in parentheses.

*n Repeat the result of the previous item or expression in parentheses n–1 times, producing a total of n copies.

Page 149: 32883311 Sas Functions Pocketref

scanq

List of Functions | 145

expressed as a fraction. num_periods (≥ 0) is the integer num-ber of compounding periods. Provide any three nonmissingarguments and saving returns the fourth.

Example:

Returns num_periodssaving(10000,100,0.05/12,.) → 83.47.

scan(str,n[,delims])

Returns the n-th word in a string. If n < 0, scan counts wordsright to left. If |n| > the number of words in str, scan returnsan empty string. delims specifies character(s) that separatewords. The default ASCII delimiters are: space . < ( + & ! $ * ) ;^ - / , % | . The default EBCDIC delimiters are: space . < ( + | &! $ * ) ; ¬ - / , % | ¢. Contiguous delimiters are treated as one.Leading and trailing delimiters are ignored. The result’sdefault length is 200.

Examples:

scan('123 abc xyz',2) → ‘abc’.

scan('123 abc xyz',-3) → ‘123’.

scan('123 abc xyz',5) → ‘’.

scan('123,abc xyz',2,',') → ‘abc xyz’.

scan('123,abc xyz',2,' ,') → ‘abc’.

scan('123.abc(xyz)',3) → ‘xyz’.

See also: call scan (p. 30), call scanq (p. 31), scanq (p. 145).

scanq(str,n[,delims]) 9+Returns the n-th word in a string, ignoring quote-encloseddelimiters. If n < 0, scanq counts words right to left. If n = 0or |n| > the number of words in str, scanq returns an emptystring. Unmatched quotes in str make left-to-right and right-to-left scans return different words. delims specifies charac-ter(s) that separate words. The default delimiters are white-space characters: space, horizontal and vertical tab, carriagereturn, line feed, and form feed. You can’t use single or double

Page 150: 32883311 Sas Functions Pocketref

sdf

146 | SAS Functions Pocket Reference

quotes as delimiters. Contiguous delimiters are treated as one.Leading and trailing delimiters are ignored. The result’sdefault length is 200.

Examples:

scanq('12 "a c" xyz',2) → ‘a c’.

scanq('12 a c xyz',2) → ‘a’.

See also: call scan (p. 30), call scanq (p. 31), scan (p. 145).

sdf(dist,quantile[,param1,param2,...])

Returns the survival function. See cdf (p. 40) for a descriptionof the parameters. Example: sdf('normal',1.96) → ≈0.025.See also: logsdf (p. 100), pdf (p. 114), quantile (p. 127).

second(sas_time|sas_datetime)

Extracts the seconds (0–59.99...) from a SAS time or SASdatetime.

Example: second(time()) → 49.4400 (the time is 10:19:49.44 PM).

See also: hour (p. 83), minute (p. 102).

sign(x)

Determines the sign of x, returning 1 if x is positive, zero (0) ifx is 0, or -1 if x is negative. Examples: sign(-0.000006) → -1.sign(10.2) → 1. sign(0/5) → 0. See also: abs (p. 10).

sin(angle)

Returns the sine of an angle. angle is expressed in radians. Toconvert degrees to radians, multiply degrees by π/180. Notethat sin(-x) = -sin(x) and 1/sin(x) is the cosecant of x.

Examples:

sin(0) → 0.

sin(constant('pi')/2) → 1.

Create a trig tabledata trig_table;if _N_=1 then pi=constant('pi');

Page 151: 32883311 Sas Functions Pocketref

smallest

List of Functions | 147

retain pi;do degrees=0 to 360;radians=degrees * pi/180;sin=sin(radians);cos=cos(radians);tan=tan(radians);output;

end;drop pi;

run;

See also: arsin (p. 12), cos (p. 51), tan (p. 155).

sinh(x)

Returns the hyperbolic sine of x, defined by (ex – e–x)/2. Exam-ples: sinh(1) → 1.1752. sinh(-1) → -1.1752. See also: cosh(p. 51), tanh (p. 155).

skewness(x1,x2,x3[,x4,...])

Returns the skewness (asymmetry) of nonmissing arguments.At least three nonmissing arguments are required or the resultis a missing value. Example: x1=-3; x2=0.5; x3=50; y1=.;skewness(2,of x1-x3,6,y1) → 2.1170147309.

sleep(n[,unit])

Suspends program execution for a specified amount of time(< 46 days), returning the time slept. n (≥ 0) is the number ofunits of time. unit (defaults to 1.0 on Windows; 0.001 other-wise) is the unit of time, as a power of 10, that’s applied to n. 1is a second and 0.001 is a millisecond, for example.

Examples:

Suspend execution for one minutetime_slept=sleep(6000,0.01);

Suspend execution until 28-Mar-2006, at 10:00 AMsleep_until='28mar2006:10:00'dt;time_slept=sleep(sleep_until-datetime(),1);

See also: call sleep (p. 32).

smallest(n,x1[,x2,...]) 9+Returns the n-th smallest of nonmissing values. If n is miss-ing, a noninteger, less than one, or greater than the number of

Page 152: 32883311 Sas Functions Pocketref

soundex

148 | SAS Functions Pocket Reference

nonmissing values, then smallest returns a missing value.Example: x1=-3.5; x2=-4; x3=5; smallest(2,2,of x1-x3,6,.) → -3.5. See also: largest (p. 96), max (p. 101), min(p. 101), ordinal (p. 112), pctl (p. 113).

soundex(word)

Encodes a word by using the Soundex algorithm. Soundex isEnglish-biased. Example: soundex('Smythe') → ‘S53’ (sameresult as soundex('Smith')). See also: spedis (p. 148).

spedis(query_word,keyword)

Returns the likelihood of two words matching, as the asym-metric spelling distance between the words. query_word is theword to query for the likelihood of a match. keyword is a tar-get word for the query. Any trailing spaces in both words areignored. The result is a nonnegative value that is usually lessthan 100 but never greater than 200 with the default costs.The spelling distance is asymmetric because spedis convertskeyword to the query_word by using a sequence of operations,so swapping the arguments usually returns a different value.spedis is similar to, but much slower than, compged (p. 47)and complev (p. 48). The following table lists each operationand its default cost.

Operation Costmatch (no change) 0

singlet (delete one of a double letter) 25

doublet (double a letter) 50

swap (reverse the order of two consecutive letters) 50

truncate (delete a letter from the end) 50

append (add a letter to the end) 35

delete (delete a letter from the middle) 50

insert (insert a letter in the middle) 100

replace (replace a letter in the middle) 100

firstdel (delete the first letter) 100

Page 153: 32883311 Sas Functions Pocketref

stderr

List of Functions | 149

The returned distance is the sum of the costs divided by thelength of the query. If this ratio is greater than one, the resultis rounded down to the nearest whole number.

Examples: The following table shows spedis values for various arguments. See also: soundex (p. 148).

sqrt(x)

Returns the positive square root of x (≥ 0). Examples: sqrt(16)→ 4. sqrt(constant('pi')) → 1.7724538509.

std(x1,x2[,x3,...])

Returns the standard deviation of nonmissing arguments. Atleast two nonmissing arguments are required or the result is amissing value. Example: x1=3; x2=4; x3=5; y1=.; std(2,ofx1-x3,6,y1) → 1.5811388301. See also: var (p. 160).

stderr(x1,x2[,x3,...])

Returns the standard error of the mean of nonmissing argu-ments. At least two nonmissing arguments are required or the

firstins (insert a letter at the beginning) 200

firstrep (replace the first letter) 200

query_word keyword spedis()'baboon' 'baboon' 0

'baXboon' 'baboon' 14

'baoon' 'baboon' 10

'baXoon' 'baboon' 16

'baboonX' 'baboon' 5

'baboo' 'baboon' 10

'babboon' 'baboon' 7

'bab,oon' 'baboon' 14

'bXaYoon' 'baboon' 28

'axoo' 'baboon' 62

Operation Cost

Page 154: 32883311 Sas Functions Pocketref

stfips

150 | SAS Functions Pocket Reference

result is a missing value. Example: x1=3; x2=4; x3=5; y1=.;stderr(2,of x1-x3,6,y1) → 0.7071067812.

stfips(postal_code)

Converts a two-character state or U.S. territory postal code orGSA geographic code to its numeric FIPS (Federal Informa-tion Processing Standards) code. postal_code is case-insensi-tive with no leading spaces; trailing spaces are ignored. Examples:The following table shows examples of stfips, stname(p. 150), and stnamel (p. 150). See also: fipstate (p. 74),zipfips (p. 165).

stname(postal_code)

Converts a two-character state or U.S. territory postal code orGSA geographic code to its name in uppercase (≤ 20 charac-ters). postal_code is case-insensitive and can have trailing, butnot leading, spaces. Examples: See stfips (p. 150). See also:fipname (p. 73), stnamel (p. 150), zipname (p. 166).

stnamel(postal_code)

Converts a two-character state or U.S. territory postal code orGSA geographic code to its name in mixed case (≤ 20 charac-ters). postal_code is case-insensitive and can have trailing, butnot leading, spaces. Examples: See stfips (p. 150). See also:fipnamel (p. 74), stname (p. 150), zipnamel (p. 166).

strip(str) 9+Removes leading and trailing spaces from a string, returningan empty string (‘’) if str is blank. Assigning the result to a var-iable doesn’t affect the variable’s length; if necessary, strippads the result with new trailing spaces to match the targetvariable’s length. The result’s default length is the length of str.strip is useful for removing trailing spaces during concate-

postal_code stfips() stname() stnamel()CA 6 CALIFORNIA CaliforniaPR 72 PUERTO RICO Puerto RicoXX . (missing) ‘ ’ (missing) ‘ ’ (missing)

Page 155: 32883311 Sas Functions Pocketref

substr

List of Functions | 151

nation. strip() is equivalent to trimn(left()) but runs fast-er. Examples: See left (p. 97). See also: right (p. 133), trim(p. 158), trimn (p. 158).

subpad(str,start_pos[,len]) 9+Returns a substring that has a specified length, padding withtrailing spaces if necessary. start_pos (≥ 1) is the position ofthe first character in the substring. len (≥ 0) is the length ofthe substring. If len is omitted, the returned substring extendsfrom start_pos to the end of str. The result’s default length is200.

Examples:

subpad('abcde',2) → ‘bcde’.

subpad('abcde',2,3) → ‘bcd’.

subpad('abcde',2,6) → ‘bcde ’ (2 trailing spaces).

See also: substr (p. 151), substrn (p. 152).

substr(char_var|str,start_pos[,len])

Extracts (if right of =) or inserts (if left of =) a substring. Thesyntax of substr depends on its position. If it’s to the left of =,the syntax is

substr(char_var,start_pos[,len])=chars_to_replace

If it’s to the right of =, the syntax is

var=substr(str,start_pos[,len])

char_var is a character variable. var is a SAS variable name. stris a character expression. start_pos (≥ 1) is the position of thefirst character in the substring. chars_to_replace are the char-acters that will replace the contents of char_var. The meaningof len depends on the position of substr:

• If left of =, substr replaces the value of char_var with the expression on the right side, replacing len characters start-ing at start_pos. len can’t be greater than the length of the expression that remains in char_var after start_pos. If len is omitted, substr uses all of the characters on the right side

Page 156: 32883311 Sas Functions Pocketref

substrn

152 | SAS Functions Pocket Reference

to replace the values of char_var. The result’s default length is 8.

• If right of =, substr returns a portion of str, beginning at start_pos and continuing for len characters. If len is zero, negative, or greater than the length of str after start_pos, substr extracts the remainder of str and logs an invalid-length note. If len is omitted, substr extracts the remain-der of str. The result’s default length is the length of str.

Examples:

c='kidnap';substr(c,1,3)='cat' → c becomes ‘catnap’.

s=substr('kidnap',4,3) → Assigns ‘nap’ to s.

See also: subpad (p. 151), substrn (p. 152).

substrn(str,start_pos[,len]) 9+Returns a substring. start_pos is the position of the first char-acter in the substring. len is the length of the substring. If lenis omitted, the result extends from start_pos to the end of str.The result’s default length is the length of str.

substrn, unlike substr (p. 151), returns a zero-length resultand doesn’t log an invalid-argument note and set _ERROR_=1 for bad values of start_pos or len. If start_pos or len is amissing value, the result is a zero-length string. If start_pos ≤0, the result is truncated at the beginning: The result’s firstcharacter is the first character of str and its length is reducedaccordingly. If len extends beyond the end of str, the result istruncated at the end: The result’s last character is the last char-acter of str.

Examples:

substrn('abcde',2) → ‘bcde’.

substrn('abcde',-2) → ‘abcde’.

substrn('abcde',2,3) → ‘bcd’.

substrn('abcde',2,99) → ‘bcde’.

Page 157: 32883311 Sas Functions Pocketref

sysmsg

List of Functions | 153

substrn('abcde',2,-6) → ‘’ (empty string).

See also: subpad (p. 151).

sum(x1[,x2,...])

Returns the sum of nonmissing arguments. If all the argu-ments are missing values, the result is a missing value. Exam-ple: x1=3; x2=4; x3=5; y1=.; sum(2,of x1-x3,6,y1) →20. See also: mean (p. 101), n (p. 106).

symexist(macro_var_name) 9+Returns one (1) if a macro variable exists; zero otherwise. Fordetails see Symexist in SAS Macro Language: Reference.

symget(macro_var_name)

Returns a macro-variable value during DATA-step execution.The result’s default length is 200. For details see Symget in SASMacro Language: Reference. See also: call symput (p. 36),call symputx (p. 36).

symglobl(macro_var_name) 9+Returns one (1) if a macro variable is in a global scope to thecalling DATA step; zero otherwise. For details see Symglobl inSAS Macro Language: Reference. See also: symlocal (p. 153).

symlocal(macro_var_name) 9+Returns one (1) if a macro variable is in a local scope to thecalling DATA step; zero otherwise. For details see Symlocal inSAS Macro Language: Reference. See also: symglobl (p. 153).

sysget(env_var)

Returns the value of an OS environment variable. env_var isthe case-sensitive name of the environment variable. Trailingspaces are significant; use trim (p. 158) to remove them. Theresult’s default length is 200. sysget logs a warning if it trun-cates the result, or returns a missing value if env_var is unde-fined. Example: sysget('username') → ‘judy’ (Windows).

sysmsg()

Returns the text of error messages or warning messages thatare generated when a dataset or external-file access functionencounters an error condition. If no error message is avail-

Page 158: 32883311 Sas Functions Pocketref

sysparm

154 | SAS Functions Pocket Reference

able, the result is blank. The internally stored error message isreset to blank after calling sysmsg, so subsequent sysmsg callsbefore another error condition occurs return blank values. Seealso: sysrc (p. 155).

sysparm()

Returns the value of the SYSPARM= system option or anempty string if SYSPARM= is unspecified. The result’s defaultlength is 200.

sysprocessid()

Returns the process ID of the current SAS process, as a 32-character hexadecimal string. To get the process name, passthe ID to sysprocessname (p. 154).

sysprocessname([process_id])

Returns the process name associated with process_id or, ifprocess_id is omitted, the name of the current SAS process.You can use the value returned by sysprocessid (p. 154) orthe values of the automatic macro variables &sysprocessid or&sysstartid as the sysprocessname argument.

Examples:

sysprocessname() → ‘DMS Process’.

sysprocessname(sysprocessid()) → ‘DMS Process’.

%let id=&sysprocessid;%let name=%sysfunc(sysprocessname(&id));%put &name; (logs ‘DMS Process’)

sysprod(product_name)

Determines whether a SAS product is licensed, returning 1 ifproduct_name is a licensed SAS product, 0 if it’s unlicensed(inaccessible), or -1 if it’s not a SAS product. A product islicensed if its license expiration date hasn’t passed. A SASproduct can exist on your system but no longer be licensed.product_name takes the case-insensitive values access, base(always returns true), connect, ets, graph, share, stat, andso on. You can prefix a product name with ‘SAS/’. Example:sysprod('stat') → 1. See also: PROC SETINIT.

Page 159: 32883311 Sas Functions Pocketref

timepart

List of Functions | 155

sysrc()

Returns the error number for the most recent system errorencountered by a call to an external-file or dataset function.See also: sysmsg (p. 153).

system(os_command)

Executes an OS command during a SAS session and returnsthe OS-dependent return code. os_command (≤ 1024 charac-ters) is a system command enclosed in quotes, an expressionwhose value is a system command, or the name of a charactervariable whose value is a system command. See also: X state-ment, X command, call system (p. 36).

tan(angle)

Returns the tangent of an angle. angle is expressed in radiansand can’t be an odd multiple of π/2. To convert degrees toradians, multiply degrees by π/180. Note that 1/tan(x) is thecotangent of x, and tan(x) = sin(x)/cos(x). Examples:tan(0.7854) → 1 (0.7854 radians ≈ 45 degrees). tan(0) → 0.tan(constant('pi')/2) → . (missing—bad angle). See also:atan (p. 12), atan2 (p. 13), cos (p. 51), sin (p. 146).

tanh(x)

Returns the hyperbolic tangent of x, defined by (ex–e–x)/(ex+e–x)or sinh(x)/cosh(x). Examples: tanh(0) → 0. tanh(0.5) →0.4621. See also: call tanh (p. 37), cosh (p. 51), sinh (p. 147).

time()

Returns the current time of day as a SAS time (the numberseconds after midnight, 0 ≤ time() < 86400).

Example: time() → 73580.56 (the time is 20:26:21).

See also: datetime (p. 55), dhms (p. 59), hms (p. 82), mdy (p. 101), today (p. 156).

timepart(sas_datetime)

Extracts the time, as a SAS time, from a SAS datetime.

Example: timepart(datetime()) → 80389.44 (now is 02-Sep-2005 22:19:49).

See also: datepart (p. 55), datetime (p. 55).

Page 160: 32883311 Sas Functions Pocketref

tinv

156 | SAS Functions Pocket Reference

tinv(prob,df[,nc])

Returns the prob-th quantile from the Student’s t distributionwith degrees of freedom df (> 0, noninteger allowed) andnoncentrality parameter nc. The probability that an observa-tion from a t distribution is less than or equal to the returnedquantile is prob (0 < x < 1). If nc is omitted or is zero, tinvuses the central t distribution. Large nc values can cause tinvto return a missing value. tinv is the inverse of probt (p. 119).Example: tinv(0.95,2.5,3) → 11.03383362. See also: tnonct(p. 156).

tnonct(x,df,prob)

Returns the nonnegative noncentrality parameter from a non-central Student’s t distribution with the specified parameters.x (≥ 0) is a random variable, df (> 0, noninteger allowed) is thedegrees of freedom, and prob (0 < prob < 1) is a probability.

Examples:

tnonct(2,4,0.05) → 3.8966229298.

tnonct(2,4,probt(2,4,1.5)) → 1.499999999.

See also: cnonct (p. 44), fnonct (p. 75), probt (p. 119), tinv (p. 156).

today()

Returns the current date as a SAS date (the number of dayssince 1-Jan-1960). today is the same as date (p. 55).

Example: today() → 16681 (today is 2-Sep-2005).

See also: datetime (p. 55), dhms (p. 59), hms (p. 82), mdy (p. 101), time (p. 155).

translate(str,to1,from1[,to2,from2,...])

Replaces all occurrences of from characters in str with to char-acters. to and from values correspond on a character-by-char-acter basis; translate changes the first character of from tothe first character of to, and so on. If to has fewer charactersthan from, translate changes the extra from characters tospaces. If to has more characters than from, translateignores the extra to characters. The maximum number of to-

Page 161: 32883311 Sas Functions Pocketref

trigamma

List of Functions | 157

from pairs depends on your OS. Using many pairs of shortarguments is no different from using fewer pairs of long ones.The result’s default length is the length of str.

Example: translate('abcdeb','24,','bde') → ‘a2c4,2’.

See also: tranwrd (p. 157).

trantab(str,translation_table) 9+Transcodes a string by using a translation table to remap thecharacters from one internal representation to another. Theencoding of str must match the encoding of table 1 intranslation_table (see PROC TRANTAB for details). In SAS8.2 and later use the LOCALE= system option instead oftrantab.

Example:

Transcode from Latin2 to uppercase Latin2trantab('polyalloy','lat2_ucs') → ‘POLYALLOY’.

tranwrd(str,substr,replace_with)

Replaces or removes all occurrences of a substring within astring. Trailing spaces in str and replace_with are significant.The result’s default length is 200.

Examples:

tranwrd('Miss J. Kim','Miss','Ms.') → ‘Ms. J. Kim’.

tranwrd('Miss J. Kim','Miss','') → ‘J. Kim’.

tranwrd('abc def ghi',' ',',') → ‘abc,def,ghi’.

See also: translate (p. 156).

trigamma(x)

Returns the value of the trigamma function, defined as thederivative of the function digamma (p. 60) where x is a nonze-ro real number that isn’t a negative integer. If x > 0 thentrigamma is the second derivative of lgamma (p. 98). Examples:trigamma(6) → 0.1813229557. trigamma(-1) → . (missing—bad x). trigamma(-0.5) → 8.9348022005. See also: gamma(p. 80).

Page 162: 32883311 Sas Functions Pocketref

trim

158 | SAS Functions Pocket Reference

trim(str)

Removes trailing spaces from a string, returning one space(‘ ’) if str is blank. Assigning the result to a variable doesn’taffect the variable’s length; if necessary, trim pads the resultwith new trailing spaces to match the target variable’s length.The result’s default length is the length of str. trim is useful forremoving trailing spaces during concatenation. Examples: Seeleft (p. 97). See also: right (p. 133), strip (p. 150), trimn(p. 158).

trimn(str)

Same as trim (p. 158), except that trimn returns an emptystring (‘’) if str is blank. Examples: See left (p. 97).

trunc(x,len)

Shortens a full-length double number x to len bytes (3 ≤ len ≤8), padding truncated bytes with zeros. Use trunc to comparea numerical constant to a SAS numerical value that’s stored infewer than 8 bytes. The maximum integer that you can storein len bytes depends on your operating system.

Examples:

trunc(1,4) → 1.00000000.

trunc(1,8) → 1.00000000.

trunc(1.3,3) → 1.29980469.

trunc(1.3,4) → 1.29999924.

trunc(1.3,8) → 1.30000000.

trunc(1.5,4) → 1.50000000.

See also: round (p. 133).

uniform(seed)

Same as ranuni (p. 132).

upcase(str)

Converts all lowercase letters in a string to uppercase. Theresult’s default length is the length of str.

Page 163: 32883311 Sas Functions Pocketref

uuidgen

List of Functions | 159

Examples:

upcase('Place de l''Étoile') → ‘PLACE DE L'ÉTOILE’.

Convert all character-variable values to uppercasedata mydata;set mydata;array char_vars[*] _CHARACTER_;do i=1 to dim(char_vars);char_vars[i]=upcase(char_vars[i]);

end;drop i;

run;

See also: lowcase (p. 100), propcase (p. 120).

urldecode(url)

Decodes a URL, using escape syntax. A URL escape sequencetakes the form %nn. A plus character is converted to a space.

Example: urldecode('%41+B%43%20%23%31') → ‘A BC #1’.

See also: htmldecode (p. 83), urlencode (p. 159).

urlencode(str)

Encodes a string as a URL, using escape syntax. A URL escapesequence takes the form %nn. A space character is convertedto %20. urlencode doesn’t encode alphanumeric charactersor the symbols $ - _ @ . ! * ( ) , .

Example: urlencode('A BC #1') → ‘A%20BC%20%231’.

See also: htmlencode (p. 83), urldecode (p. 159).

uss(x1[,x2,...])

Returns the uncorrected sum of squares of nonmissing argu-ments. If all the arguments are missing values, the result is amissing value. Example: x1=3; x2=4; x3=5; uss(2,of x1-x3,6,.) → 90. See also: css (p. 53).

uuidgen([max_warnings[,binary_result]]) 9+Generates a universal unique identifier (UUID), returning a36-character string by default. uuidgen logs no more thanmax_warnings (default 1) warnings. Set binary_result to non-

Page 164: 32883311 Sas Functions Pocketref

var

160 | SAS Functions Pocket Reference

zero for a 16-byte (short) binary result. Example: uuidgen() →‘c66a0aca-aa7e-40e3-b6f2-4a835c2c6a7’.

var(x1,x2[,x3,...])

Returns the variance of nonmissing arguments. At least twononmissing arguments are required or the result is a missingvalue. Example: x1=3; x2=4; x3=5; var(2,of x1-x3,6,.)→ 2.5. See also: std (p. 149).

var<attr>(dataset_id,n)

Group of functions that returns attributes of the n-th variablein a SAS dataset. dataset_id is the dataset identifier that open(p. 111) returns. Use varnum (p. 161) or PROC CONTENTSto get n. Replace attr with one of the values listed in the fol-lowing table.

Example:

Create a dataset of attributes of the variables in mydatadata vars;length name label infmt fmt $ 32 type $ 1;drop dataset_id nvars i rc;dataset_id=open('mydata','i');nvars=attrn(dataset_id,'nvars');do i=1 to nvars;name=varname(dataset_id,i);label=varlabel(dataset_id,i);infmt=varinfmt(dataset_id,i);fmt=varfmt(dataset_id,i);type=vartype(dataset_id,i);

attr Returns the variable’sfmt Format (possibly a blank string)

infmt Informat (possibly a blank string)

label Label (possibly a blank string)

len Length (compile-time allocated size)

name Name (≤ 32 characters)

transcode 9+ Transcoding flag (0 = Off; 1 = On)

type Type (C = character; N = numeric)

Page 165: 32883311 Sas Functions Pocketref

v<attr>

List of Functions | 161

length=varlen(dataset_id,i);transcode=vartranscode(dataset_id,i);output;

end;rc=close(dataset_id);

run;

See also: call vnext (p. 37), getvarc (p. 81), getvarn (p. 81), v<attr> (p. 161).

varnum(dataset_id,var_name)

Returns the number of a named variable’s position in a SASdataset (or zero if the variable isn’t in the dataset). dataset_idis the dataset identifier that open (p. 111) returns. var_name isthe variable’s name. PROC CONTENTS produces the samevariable numbers.

Example: varnum(open('mydata.dsn','i'),'x4') → 4.

See also: getvarc (p. 81), getvarn (p. 81), var<attr> (p. 160).

v<attr>(var)v<attr>x(str)

Group of functions that returns attributes of a specified vari-able in a SAS dataset. In the v<attr> functions, var is a vari-able name or an array reference (not an expression or quotedcharacter constant). In the v<attr>x functions, str is a charac-ter expression that evaluates to a variable name (but can’t bean array reference). For example, use vformatd(myvar) orvformatdx('myvar'). Replace attr with one of the values list-ed in the following table.

attr Returns the variable’s

array Array flag (0 = No; 1 = Yes)

format Format name, including the width and dot ($CHAR20., for example)

formatd Decimal argument (d) of the format

formatn Format name, excluding the width and dot ($CHAR, for example)

formatw Width argument (w) of the format

Page 166: 32883311 Sas Functions Pocketref

verify

162 | SAS Functions Pocket Reference

Example:

Prints n=x t=N fn=COMMA w=10 d=2 len=8 v=1,234.00data _null_;x=1234;format x comma10.2;n=vname(x);t=vtype(x);fn=vformatn(x);w=vformatw(x);d=vformatd(x);len=vlength(x);v=vvalue(x);put n= t= fn= w= d= len= v=;

run;

See also: call vnext (p. 37), var<attr> (p. 160).

verify(str,substr1[,substr2,...])

Returns the position of the first character in a string that’s notin any substring, or returns zero if every character in str is inat least one substr. Example: verify('abMyz','abc','xyz')→ 3. See also: countc (p. 52), findc (p. 72).

inarray Array member flag (0 = No; 1 = Yes)

informat Informat name, including the width and dot ($CHAR20., for example)

informatd Decimal argument (d) of the informat

informatn Informat name, excluding the width and dot ($CHAR, for example)

informatw Width argument (w) of the informat

label Label, or the variable name if no label exists

length Length (compile-time allocated size)

name Name (≤ 32 characters)

transcode 9+ Transcoding flag (0 = Off; 1 = On)

type Type (C = character; N = numeric)

value 9+ Formatted value

attr Returns the variable’s

Page 167: 32883311 Sas Functions Pocketref

year

List of Functions | 163

week([sas_date[,descriptor]]) 9+Returns the week (0–53) of the year in which a SAS date falls.If omitted, sas_date defaults to the current date. descriptor isone of the values listed in the following table. If omitted,descriptor defaults to u.

Example: week(today()) → 35 (today is 2-Sep-2005).

See also: day (p. 56), month (p. 104), qtr (p. 126), weekday (p. 163), year (p. 163).

weekday(sas_date)

Returns the day of the week of a SAS date (1 = Sunday, 2 =Monday,..., 7 = Saturday).

Example: weekday(today()) → 6 (today is Friday, 2-Sep-05).

See also: day (p. 56), month (p. 104), qtr (p. 126), week (p. 163), year (p. 163).

year(sas_date)

Extracts the four-digit year from a SAS date.

Example: year(today()) → 2005 (today is 2-Sep-2005).

See also: day (p. 56), month (p. 104), qtr (p. 126), week (p. 163), weekday (p. 163).

descriptor Specifies the SAS date by using theu Week number (0–53) within the year, where Sunday is

the first day of the week (the default).

v Week number (1–53), where Monday is the first day of the week and the first week (week 01) of the year is the week that includes both January 4th and the first Thursday of the year. If the first Monday of January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year.

w Week number (0–53) within the year, where Monday is the first day of the week.

Page 168: 32883311 Sas Functions Pocketref

yieldp

164 | SAS Functions Pocket Reference

yieldp(par,cpn_rate,cpn_freq,remaining_cpns,time_to_next_cpn,price_plus_accrued)

Returns the yield-to-maturity of a bond. par (> 0) is the par(face) value. cpn_rate (0 ≤ cpn_rate < 1) is the nominal per-year coupon rate, expressed as a fraction. cpn_freq (> 0) is theinteger number of coupons per year. remaining_cpns (> 0) isthe integer number of remaining coupons. time_to_next_cpn(> 0 and ≤ 1/cpn_freq) is the time from now until the nextcoupon date, expressed as a fractional number of years.price_plus_accrued (> 0) is the price with accrued interest.

Example: yieldp(1000,0.06,4,14,0.165,800) → 0.13607.

yrdif(start_date,end_date,basis)

Returns the fractional number of years between start_dateand end_date. basis is one of the values listed in the followingtable. See also: datdif (p. 54).

Examples:

sdate='1feb2004'd; edate='1may2007'd;

yrdif(sdate,edate,'30/360') → 3.25.

yrdif(sdate,edate,'act/act') → 3.2440676697.

yrdif(sdate,edate,'act/360') → 3.2916666667.

yrdif(sdate,edate,'act/365') → 3.2465753425.

basis Count days by using'30/360'|'360'

30-day months and 360-day years, regardless of the actual number of days each month or year has

'act/act'|'actual'

The actual number of days between the dates, divided by 365 plus the number of days that fall in 366-day years divided by 366

'act/360' The actual number of days between the dates, divided by 360

'act/365' The actual number of days between the dates, divided by 365

Page 169: 32883311 Sas Functions Pocketref

zipfips

List of Functions | 165

yyq(year,quarter)

Returns a SAS date for the first day of the quarter (1, 2, 3, or 4)of year. year is a two-digit or four-digit integer. The systemoption YEARCUTOFF= defines the year value for two-digitdates. Example: yyq(2005,3) → 16618 (1-Jul-2005). See also:qtr (p. 126), year (p. 163).

zipcity(zip) 9+Converts a zip code to a city name, comma, space, and two-character state postal code (≤ 20 characters). zip is numeric orcharacter, with or without leading zeros (trailing zeros arerequired). zipcity uses the sashelp.zipcode built-in data-set. Examples: See the following table. See also: zipfips (p. 165),zipname (p. 166), zipnamel (p. 166), zipstate (p. 166).

zipfips(zip)

Converts a zip code to a numeric FIPS (Federal InformationProcessing Standards) code. zip is numeric or character, withor without leading zeros (trailing zeros are required). Exam-ples: The following table shows examples of zipfips, zipname(p. 166), zipnamel (p. 166), and zipstate (p. 166).

zip zipcity()‘94123’ ‘San Francisco, CA’

80303 ‘Boulder, CO’

‘04609’ ‘Bar Harbor, ME’

4609 ‘Bar Harbor, ME’

‘637’ ‘Sabana Grande, PR’

‘xxx’ ‘ ’ (missing—unknown zip code)

zip zipfips() zipname() zipnamel() zipstate()‘94123’ 6 CALIFORNIA California CA

80303 8 COLORADO Colorado CO

‘04609’ 23 MAINE Maine ME

4609 23 MAINE Maine ME

Page 170: 32883311 Sas Functions Pocketref

zipname

166 | SAS Functions Pocket Reference

See also: fipname (p. 73), fipnamel (p. 74), fipstate (p. 74), stfips (p. 150), zipcity (p. 165).

zipname(zip)

Converts a zip code to its state or U.S. territory name in upper-case (≤ 20 characters). zip is numeric or character, with orwithout leading zeros (trailing zeros are required). Examples:See zipfips (p. 165). See also: fipname (p. 73), stname (p. 150),zipcity (p. 165), zipnamel (p. 166), zipstate (p. 166).

zipnamel(zip)

Converts a zip code to its state or U.S. territory name in mixedcase (≤ 20 characters). zip is numeric or character, with orwithout leading zeros (trailing zeros are required). Examples:See zipfips (p. 165). See also: fipnamel (p. 74), stnamel(p. 150),zipcity (p. 165),zipname (p. 166),zipstate (p. 166).

zipstate(zip)

Converts a zip code to its two-character state or U.S. territorypostal code or GSA geographic code in uppercase. zip isnumeric or character, with or without leading zeros (trailingzeros are required). Examples: See zipfips (p. 165). See also:fipstate (p. 74), zipcity (p. 165), zipname (p. 166),zipnamel (p. 166).

‘00637’ 72 PUERTO RICO Puerto Rico PR

‘xxx’ . ‘ ’ ‘ ’ ‘ ’

zip zipfips() zipname() zipnamel() zipstate()

Page 171: 32883311 Sas Functions Pocketref

Colophon

The author typeset this book in Adobe FrameMaker 7.2 runningon Microsoft Windows XP. The body typeface is Minion; theheading typeface is Myriad Condensed; and the code typeface isLucida Sans Typewriter. The editor used the commenting andmarkup features in Adobe Acrobat 7.0 to send corrections to theauthor. The cover was created in Adobe InDesign CS2. The ISBNbar code was generated with bookland.py, a free Python programat http://www.cgpp.com/bookland/isbn.html. The final PDFs werecreated with Acrobat Distiller 7.0, using different job optionsdepending on whether the file was headed for the printer or theweb.