Differences between revisions 1 and 2
Revision 1 as of 2019-12-03 06:00:53
Size: 2859
Comment: Initial commit
Revision 2 as of 2020-02-10 14:24:37
Size: 3639
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Logic Operators = = SAS Expressions =

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

<<TableOfContents>>

----



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

=== Conditional Processing ===

To process a statement conditionally, use the `if` statement.
Line 19: Line 33:
After an `if` statement, an `else` (or `else if`) statement is also valid.
Line 20: Line 35:
{{{
if EXPR then ASSIGNMENT;
else if EXPR then ASSIGNMENT;
else ASSIGNMENT;
}}}
Line 21: Line 41:
== Multi-statement conditional processing == Note that this syntax structure does not take a keyword to close the `if`.
Line 23: Line 43:
`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 47:
    ASSIGNMENT1;
    ASSIGNMENT2;
    ASSIGNMENT;
    ASSIGNMENT;
Line 34: Line 54:
== Compound Logic == === Compound Logic ===
Line 36: Line 56:
`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 59:
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 63:
Finally, SAS supports a case matching system.

=== 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 73:
        ASSIGNMENT1;
        ASSIGNMENT2;
        ASSIGNMENT;
        ASSIGNMENT;
Line 62: Line 80:


== 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 84:
== 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 102:
== Missing value test == === Missing Values ===
Line 78: Line 104:
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 113:
== String matching test == === String Matching ===
Line 84: Line 115:
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 127:
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 133:
= Assignment Operators = == Assignment Operators ==
Line 99: Line 139:
== Common Functions == === Common Functions ===

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:

  • % 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


CategoryRicottone

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