A Brief Introduction to SAS Operators and Functions

By Curtis Nichols,2014-06-22 09:17
7 views 0
A Brief Introduction to SAS Operators and Functions

    A Brief Introduction to SAS Operators

    and Functions


This chapter introduces SAS arithmetic, comparison, and Boolean operators, and SAS

    mathematical and statistical functions and provides some basic rules for using them. SAS operators and functions are used as part of SAS programming statements,

    including if…then statements and assignment statements used in the data step to create

    new variables and carry out other tasks, and where statements used in proc steps to

    select cases. Examples using operators and functions appear throughout this workbook. You can get more information on SAS operators and functions in the SAS online documentation and in the Language Reference.

SAS Arithmetic Operators:

    The symbols for SAS arithmetic operators are given below, along their definitions and examples of how they could be used in an expression to set up a new variable.

Symbol Definition Example

    ** Exponentiation y = x**2;

     z = x**y;

    * Multiplication r = x*y;

    / Division r = x/y;

    + Addition s = x+y;

    - Subtraction t = x-y;

    Note that an asterisk (*) must always be used to indicate multiplication e.g. y=2*x, not y=2x, or 2(x). If one of the arguments for an arithmetic operator is missing, the result is missing.

SAS Comparison Operators:

    SAS comparison operators can be written as symbols or as their mnemonic equivalents, as shown below:

    Symbol Mnemonic Definition

    < Lt Less than

    <= Le Less than or equal to

    > Gt Greater than

    >= Ge Greater than or equal to

    = Eq Equal to

    ~= Ne Not equal to.

    Intro to SAS operators and functions. 1

    If the symbol for a comparison operator is used, it is not necessary to have blank spaces around it, but if the mnemonic is used, it must be set off by spaces, as illustrated in the examples below:

if x

is equivalent to:

if x < y then group = 1;

This statement can also be written using mnemonic equivalents:

if x lt y then group eq 1;

    The mnemonics used for comparison operators are not case-sensitive and may be given in upper case, lower case, or a mixture.

Logical (Boolean) Operators:

    Logical or Boolean operators are used as links between parts of a SAS programming statement. The table below lists the logical operators and their mnemonic equivalents.

Symbol Mnemonic Equivalent

    & AND

     OR |

    ~ NOT*

    *Note that the symbol for NOT depends on the operating system you are using; it is probably safer to use the mnemonic equivalent, rather than the symbol for NOT.

    An example of a SAS statement using a logical operator is shown below:

if age < 25 and sex = "F" then youngfem = 1;

    Parentheses can be used in SAS expressions to group parts of the expression and control the order in which operations are processed. Expressions within parentheses are evaluated before anything outside parentheses. Be sure that each left parenthesis is followed by a matching right parenthesis.

if (age < 25) and (sex = "F") then youngfem = 1;

Order of Precedence of Operations:

Intro to SAS operators and functions. 2

    The order of precedence of the arithmetic operators and Boolean operators is shown below. Items in a higher row are evaluated before those in a lower row, and items in the same row are evaluated generally from left to right.


    * and /

    + and -

    < < + = ~= >= > ~< ~>

    & (AND) | (OR)

SAS Functions:

    A SAS function performs a computation or system manipulation on argument(s) and returns a value. For example, the log function returns the natural log of an argument. The

    argument(s) to a function are contained in parentheses immediately following the function name; argument(s) may be either variable names, constants such as numbers, or SAS expressions (e.g. other SAS functions or mathematical expressions). If a function requires more than one argument, they are separated by commas.

    There are many types of SAS functions, including arithmetic, array, truncation, mathematical, trigonometric, probability, quantile, sample statistics, random number, financial, character, date and time, state and ZIP code functions. For a complete list of SAS functions by category, see the SAS Language Reference.

SAS Mathematical Functions:

    A short list of some of some commonly used mathematical functions is given below:

    Selected Mathematical Functions

    Function Name Definition Assignment Statement Example

    Y1 = abs(x); Abs Absolute value

    Integer (takes the integer part of the Y2 = int(x); Int argument)

    Y3=log(x); Log Natural log

    Y4=log10(x); Log10 Log base 10

    y5=round(x,.01); Rounds the argument to the nearest Round specified level (e.g., hundredths)

    Y6=sqrt(x); Sqrt Square root

If the value of the argument is invalid (such as using the sqrt function with a negative

    value as the argument), SAS will return a missing result, and display an error message in the SAS Log. However this error will not prevent the program from executing.

Intro to SAS operators and functions. 3

Example using SAS mathematical functions to transform variables and

    create new variables:

The example below shows how to use SAS mathematical functions and arithmetic

    operators to transform variables and create new variables in a data step. Each new variable is created using an assignment statement, in which the new variable is named on

    the left hand side of the equals sign, and the function or expression is on the right hand

    side. These new variables must be created between the data statement and the run statement of a data step to be valid.

data math;

     input x y;

/*mathematical functions*/

     absx = abs(x);

     sqrtx = sqrt(x);

     log10y = log10(y);

     lny = log(y);

     int_y = int(y);

     roundy = round(y,.1);

/*arithmetic operators*/

     mult = x*y;

     divide = x/y;

     expon = x**y;

     tot = x + y;

     diff = x - y;


     4 5.23

     -15 22.0

     . 18.51

     -1 3

     6 0

     5 5.035


    proc print data=math;


The output from these commands is shown below. Notice the missing values as the result

    of applying arithmetic or mathematical operators to a missing or illegal argument.

     OBS X Y ABSX SQRTX LOG10Y LNY INT_Y ROUNDY 1 4 5.230 4 2.00000 0.71850 1.65441 5 5.2 2 -15 22.000 15 . 1.34242 3.09104 22 22.0 3 . 18.510 . . 1.26741 2.91831 18 18.5 4 -1 3.000 1 . 0.47712 1.09861 3 3.0 5 6 0.000 6 2.44949 . . 0 0.0 6 5 5.035 5 2.23607 0.70200 1.61641 5 5.0 OBS MULT DIVIDE EXPON TOT DIFF 1 20.920 0.76482 1408.55 9.230 -1.230 2 -330.000 -0.68182 7.48183E25 7.000 -37.000 3 . . . . . 4 -3.000 -0.33333 -1.00 2.000 -4.000 5 0.000 . 1.00 6.000 6.000 6 25.175 0.99305 3306.08 10.035 -0.035

    Intro to SAS operators and functions. 4

SAS Statistical Functions:

Statistical functions can be used to generate such values as the mean, sum, and standard

    deviation of values within a case. Statistical functions operate on at least 2 arguments. The arguments can be listed separated by commas, or lists of variables can be used if the

    keyword of precedes the list. The result of a statistical function is based on the non-

    missing values of the arguments.

    Selected Statistical Functions

    Assignment Statement Function Name Definition Example

    Mean Mean of non-missing values y1 = mean (x1,x2,x3);

    Min Minimum of non-missing values y2 = min (of x1-x3);

    Max Maximum of non-missing values y3 = max (cash, credit);

    N The number of non-missing values y4 = n (of age--weight);

    Nmiss The number of missing values y5 = nmiss (of wt1-wt3);

    Std Standard deviation of non-missing values y6 = std (5,6,7,9);

    Stderr Standard error of the mean of non-y7 = stderr(of x1-x20);

    missing values

    Sum Sum of non-missing values y8 = sum(of x1-x20);

The example below shows the use of selected statistical functions on a hypothetical data

    set with three variables containing the salary for each person in the years 2001, 2002, and

    2003 (SAL01, SAL02, and SAL03, respectively). The format statement is used to

    display the values of the salary variables and some of the result variables in dollar form,

    with 9 places (dollar9.). You can change the way these values are displayed by modifying the format used. For example, you could display these variables with dollars

    and cents by using the dollar12.2 format.

data salary;

     input SAL01 SAL02 SAL03;

     AvgSalary = mean(SAL01,SAL02,SAL03);

     StdSalary = std(SAL01,SAL02,SAL03);

     MaxSalary = max(SAL01,SAL02,SAL03);

     YrsSalary = n(SAL01,SAL02,SAL03);

     TotSalary = sum(SAL01,SAL02,SAL03);

     format SAL01 SAL02 SAL03 AvgSalary MaxSalary TotSalary dollar9.;


    50000 55000 60000

     . 65000 70000

     . . 52000

    50000 . .


    Intro to SAS operators and functions. 5

title "Salary Example";

    proc print data=salary;


    The output from these commands is shown below:

     Salary Example Std Yrs Obs SAL01 SAL02 SAL03 AvgSalary Salary MaxSalary Salary TotSalary 1 $50,000 $55,000 $60,000 $55,000 5000.00 $60,000 3 $165,000 2 . $65,000 $70,000 $67,500 3535.53 $70,000 2 $135,000 3 . . $52,000 $52,000 . $52,000 1 $52,000 4 $50,000 . . $50,000 . $50,000 1 $50,000

To restrict the calculation of the mean salary to cases that have at least two valid values

    in the arguments, you could use the n function in combination with the mean function, as shown below for the variable AVGSAL2:

data salary2;

     input SAL01 SAL02 SAL03;

     AvgSalary = mean(SAL01,SAL02,SAL03);

     StdSalary = std(SAL01,SAL02,SAL03);

     MaxSalary = max(SAL01,SAL02,SAL03);

     YrsSalary = n(SAL01,SAL02,SAL03);

     TotSalary = sum(SAL01,SAL02,SAL03);

     if n(SAL01,SAL02,SAL03)>=2 then AvgSalary2 = mean(SAL01,SAL02,SAL03);

     format SAL01 SAL02 SAL03 AvgSalary AvgSalary2 MaxSalary

    TotSalary dollar9.;


    50000 55000 60000

     . 65000 70000

     . . 52000

    50000 . .


title "Salary Example, with Two Ways of Calculating Mean";

    proc print data=salary2;

     var SAL01 SAL02 SAL03 AvgSalary AvgSalary2; run;

     Salary Example, with Two Ways of Calculating Mean Avg Obs SAL01 SAL02 SAL03 AvgSalary Salary2 1 $50,000 $55,000 $60,000 $55,000 $55,000 2 . $65,000 $70,000 $67,500 $67,500 3 . . $52,000 $52,000 . 4 $50,000 . . $50,000 .

Intro to SAS operators and functions. 6

Report this document

For any questions or suggestions please email