SAS Expressions

In any SAS syntax context that accepts an expression, the following are valid.


Logic Operators

The basic operators are:

Operation

Syntax

Equality

=, eq

Inequality

~=, ^=, ne

Less than

<, lt

Less than or equality

<=, le

Greater than

>, gt

Greater than or equality

>=, ge

Conditional Processing

To process a statement conditionally, use the if statement.

if EXPR then ASSIGNMENT;

After an if statement, an else (or else if) statement is also valid.

if EXPR then ASSIGNMENT;
else if EXPR then ASSIGNMENT;
else ASSIGNMENT;

Note that this syntax structure does not take a keyword to close the if.

Often a single statement is not sufficient for processing. To use multiple statements, use do blocks.

if EXPR then do;
    ASSIGNMENT;
    ASSIGNMENT;
end;

Compound Logic

The and and or keywords chain conditions. (& and |, as well as several more obscure platform-dependent characters, are also options.) Parentheses group conditions as expected.

if EXPR and EXPR then ASSIGNMENT;
else if EXPR or EXPR then ASSIGNMENT;

Case Matching

SAS offers case matching syntax. The select statement identifies the variable that is matched against, while subsequent when statements give values to match against.

select(VAR);
    when(VAL) ASSIGNMENT;
    when(VAL) do;
        ASSIGNMENT;
        ASSIGNMENT;
    end;
    otherwise ASSIGNMENT;
end;

Note that, unlike the if/else statements, the case matching syntax requires and end statement to close.

Membership

Membership tests can be written using the in keyword.

if (VAR in ( 1 2 3 ))       then ASSIGNMENT;
if (VAR not in ( 1, 2, 3 )) then ASSIGNMENT; /* accepted delimiters are spaces, commas, or both */

Value Ranges

Range tests can be written as MIN < VAR < MAX or VAR between MIN and MAX.

Missing Values

Missing values are . for numeric variables and "" (empty string) for string variables. These can be tested literally, or the missing keyword.

if (VAR is missing)     then ASSIGNMENT;
if (VAR is not missing) then ASSIGNMENT;

String Matching

String matching is using the like keyword or the =* operator.

if (VAR like 'PATTERN') then ASSIGNMENT;
if (VAR =* 'PATTERN')   then ASSIGNMENT;

The rules for PATTERN are:

For case-insensitive matching, try: upcase(VAR) like 'PATTERN'.


Assignment Operators

Precisely what you expect: +, -, *, /, and **.

Common Functions


CategoryRicottone