SAS Expressions
In SAS, an expression is a sequence of operands and operators that produces a value.
In any SAS syntax context that accepts an expression, the following are valid.
Contents
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;
The not keyword inverts the expression.
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:
% is 0+ character wildcard
_ is 1 character wildcard
use only single-quotemarks for PATTERN, due to grammatical ambiguity with macros.
For case-insensitive matching, try: upcase(VAR) like 'PATTERN'.
Assignment Operators
Precisely what you expect: +, -, *, /, and **.
Common Functions
min(VARLIST) (max(VARLIST)) returns the lowest (highest) value of any variable in VARLIST
upcase(VAR), lowcase(VAR), and propcase(VAR) push string variables to specific casing
cats(VARLIST) concatenates string variables
substr(VAR, POSITION, LENGTH) substrings a string variable
month(VAR), year(VAR), and day(VAR) extract numeric date artifacts from datetime variables
today() returns today's datetime
mdy(MONTH, DAY, YEAR) returns a specified datetime
Obviously False
If a condition is obviously false, it is substituted with 0. SAS will log a note like:
NOTE: WHERE 0 /* an obviously FALSE WHERE clause */ ;
An example of such a condition is VAR='123' when VAR is 1-wide.