Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2019-12-03 06:00:53
Size: 2859
Comment: Initial commit
Revision 5 as of 2023-01-14 03:58:58
Size: 3992
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Logic Operators = ## page was renamed from SASExpressions
= SAS Expressions =

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

<<TableOfContents>>

----



== Logic Operators ==
Line 13: Line 24:
Conditional processing is written as:

=== Conditional Processing ===

To process a statement conditionally, use the `if` statement.
Line 19: Line 34:
After an `if` statement, an `else` (or `else if`) statement is also valid.
Line 20: Line 36:
{{{
if EXPR then ASSIGNMENT;
else if EXPR then ASSIGNMENT;
else ASSIGNMENT;
}}}
Line 21: Line 42:
== Multi-statement conditional processing == Note that this syntax structure does not take a keyword to close the `if`.
Line 23: Line 44:
`do` blocks enable a single EXPR to apply to multiple statements. Often a single statement is not sufficient for processing. To use multiple statements, use `do` blocks.
Line 27: Line 48:
    ASSIGNMENT1;
    ASSIGNMENT2;
    ASSIGNMENT;
    ASSIGNMENT;
Line 34: Line 55:
== Compound Logic == === Compound Logic ===
Line 36: Line 57:
`and` and `or` operators chain conditions. (`&` and `|`, as well as several more obscure platform-dependent characters, are also options.) Parentheses group conditions as expected.

SAS supports a full `if ... elif/else` system.
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.
Line 41: Line 60:
if EXPR then ASSIGNMENT;
else if EXPR then do;
    ASSIGNMENT1;
    ASSIGNMENT2;
end;
else
ASSIGNMENT;
if EXPR and EXPR then ASSIGNMENT;
else if EXPR or EXPR then ASSIGNMENT;
Line 49: Line 64:
Finally, SAS supports a case matching system. 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.
Line 55: Line 76:
        ASSIGNMENT1;
        ASSIGNMENT2;
        ASSIGNMENT;
        ASSIGNMENT;
Line 62: Line 83:


== Membership test ==

Membership tests can be written as `VAR in VALUELIST`, where `VALUELIST` is a list of space and/or comma delimited values surrounded by parentheses. The test can be negated using `not in`.
Note that, unlike the `if`/`else` statements, the case matching syntax requires and `end` statement to close.
Line 70: Line 87:
== Range test == === 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 ===
Line 76: Line 105:
== Missing value test == === Missing Values ===
Line 78: Line 107:
Missing values are `.` for numeric variables and `""` (empty string) for string variables. These can be tested literally, or using `VAR is missing`. The test can be negated using `is not`. 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;
}}}
Line 82: Line 116:
== String matching test == === String Matching ===
Line 84: Line 118:
String matching is written as `VAR like PATTERN` or `VAR =* PATTERN`. The rules for `PATTERN` are: 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:
Line 89: Line 130:
For case-insensitive matching, instead test `upcase(VAR)` and an all-uppercase `PATTERN`. For case-insensitive matching, try: `upcase(VAR) like 'PATTERN'`.

----
Line 93: Line 136:
= Assignment Operators = == Assignment Operators ==
Line 99: Line 142:
== Common Functions == === Common Functions ===
Line 109: Line 152:
----



== 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.

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;

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.


CategoryRicottone

SAS/Expressions (last edited 2023-01-14 04:00:37 by DominicRicottone)