SAS Tutorials

Also... New
Quick Reference Guide for SAS

BeSmartNotes (tm)

Click for more info

New -- Kwikstat
Data Generator

Generate data sets with continuous, categorical, grouped and correlated data. Great for creating data sets for examples and for quality assurance testing. Data sets are created in standard .CSV format so they can be opened in most statistical programs such as SAS, SPSS, WINKS and into spreadsheet programs such as Microsoft Excel. Order now.




 SAS Functions
Tutorial and Reference, Part 1

See for files mentioned in this tutorial © TexaSoft, 2008


How to use SAS Functions

SAS functions can be used in a DATA statement to calculate new variables or as a part of a conditional statement. (See also part 2)

Not all SAS Functions are described in these tutorials and reference -- the most commonly used functions are discussed  — check SAS manuals for complete list.

For example, here is an example SAS program that illustrates how some simple functions might be used:






Notice that in these cases the function on the right side of the “=“ sign does something to the parameter(s) in the parenthesis and returns some value, which is then assigned to the name on the left side of the “=“ sign.

The SUM function returns the total of the number A, B and C. The SQRT function returns the square root of the AGE variable, UPCASE function makes all character values in the SEX variable upper case. For example, if the SEX variable contains the value “f” then the GENDER variable will contain the value “F.”

The syntax for a function is

FUNCTIONNAME(argument-1, argument-2...argument-k)

Where arguments may be constants, variables, expressions or other functions.

Arithmetic/Mathematical Functions
(Note: Arguments for functions can be variables, expressions or other functions. Most of the examples below use constants.)

  • ABS() - Absolute Value, Ex: X=ABS(-9) returns the value 9
  • EXP() - Exponentiation, Ex: X=EXP(100) returns the value 2.688
  • LOG() - Natural Log, Ex: X=LOG(10) returns the value 2.3026
  • LOG10() - Log Base 10, Ex: X=LOG10 returns the value 1.0
  • FACT() - Factorial, Ex: FACT(7) returns the value 5040, which is (7*6*5*4*3*2*1)
  • MAX(x1,x2,x3...) maximum in list
  • MEAN() Mean of non-missing values in list. Ex: MEAN(2,4,.,6) returns the value 4
  • MEDIAN() Median of non-missing values in list
  • MIN(x1,x2,x3 ...) minimum in list
  • MOD(x1, modulo) remainder of x1/modulo Ex: MOD(5,3)=2, MOD(6,2)=0
  • N() - Number of non-missing values, Ex: N(1,2,.,4) returns the value 3.
  • NMISS() - Number missing, Ex: NMISS(1,2,.,4) returns the value 1.
  • RANUNI(0) - A different random number (uniform distribution) each time
  • RANUNI(X) - Where X is a “seed” value you enter returns the same random sequence (uniform) each time for that particular seed.
  • RANNOR() - A random number from a normal distribution with mean 0 and standard deviation of 1 (See seed info for RANUNI)
  • SIGN(x) - The sign of x
  • SUM() - Sum of non-missing values in list
  • SQRT() - Square Root, Ex: X=SQRT(9) returns the value 3
  • Trignometric Functions

    • ARCOS(argument) -arccosine
    • ARSIN(argument) - arcsine
    • ATAN(argument) - arctangent
    • COS(argument) - cosine
    • COSH(argument) - hyperbolic cosine
    • SIN(argument)- sine
    • SINH(argument) - hyperbolic sine
    • TAN(argument) - tangent
    • TANH(argument) - hyperbolic tangent

    Special Use Functions

    Variable conversion/re-read functions
    EX: s="1,212";x=input(s,comma5.); Returns x=1212 (a numeric value.)

    (Note: INPUTC(string1,char-informat) and INPUTN(string1,num-informat) are similar to INPUT(), but you can assign the format within the code.)

    PUT (X1,format) where X1 is string or numeric, returns a character value. EX: x=PUT(1234,comma6.); returns the string value “1,234.”

    (Note: PUTC(x1,char-informat) & PUTN(x1,num-informat) are similar to PUT(), but you can assign format within the code.)

    Working with previous observations

    LAGN() returns the value of the nth previous observation. Ex: Your data has values x=1,2,3. Then LAG2(x) for the third item would be 1. LAG() is the same as LAG1()

    DIFN() returns the difference in the current value and the nth previous value. Ex: Your data has values x=1,2,3. DIF2(x) of third item would be 2 (3-1=2). DIF() is the same as DIF1().

    Tutorial continues -- part 2





    Get the SAS BeSmartNotes Quick Reference Guide


    | Send comments | Back to Tutorial Menu | TexaSoft |

    © Copyright TexaSoft, 1996-2007

    This site is not affiliated with SAS(r) or SAS Institute