SCL Fundamentals |
Operators are symbols that request an arithmetic calculation, a comparison, or a logical operation. SCL includes the same operators that are provided in the Base SAS language. The only restrictions on operators in SCL are for the minimum and maximum value operators. For these SAS operators, you must use the operator symbols (> < and < >, respectively) rather than the mnemonic equivalents (MIN and MAX, respectively).
Arithmetic Operators |
The arithmetic operators, which designate that an arithmetic calculation is performed, are shown here:
Symbol | Definition |
---|---|
+ | addition |
/ | division |
** | exponentiation |
* | multiplication |
- | subtraction |
Comparison Operators |
Comparison operators propose a relationship between two quantities and ask whether that relationship is true. Comparison operators can be expressed as symbols or written with letters. An operator that is written with letters, such as EQ for =, is called a mnemonic operator. The symbols for comparison operators and their mnemonic equivalents are shown in the following table:
You can add a colon (:) modifier after any operator to compare only a specified prefix of a character string. For example, the following code produces the message pen found , because the string pen occurs at the beginning (as a prefix) of pencil :
var='pen'; if var =: 'pencil' then put var 'found'; else put var 'not found';
The following code produces the message phone not found because phone occurs at the end (as a suffix) of telephone :
var='phone'; if var =: 'telephone'; then put var 'found'; else put var 'not found';
The code produces these messages:
pen found phone not found
The IN operator compares a value produced by an expression on the left side of the operator to a list of values on the right. For example:
if age in (16, 21, 25);
If the IN operator returns 0 if the value on the left does not match a value in the list. The result is 1 if the value on the left matches a value in the list. In the case of arrays, the IN operator returns the index of the element if it finds a match.
The form of the comparison is
expression IN <value-1<, . . . ,value-n>) |
can be any valid SAS expression, but it is usually a variable name when used with the IN operator.
Suppose you have the following program section:
init: declare a[5] = (2 4 6 8 10); b = 6; if b in a then put 'B is in array A'; c=b in a; put c=; return;
This code produces the following output:
B is in array A c=3
Logical (Boolean) Operators |
Logical operators (also called Boolean operators) are usually used in expressions to link sequences of comparisons. The logical operators are shown in the following table:
If both conditions compared by an AND operator are true, then the result of the AND operation is true. Two comparisons with a common variable linked by AND can be condensed with an implied AND. For example, the following two subsetting IF statements produce the same result:
if 16<=age and age<=65; if 16<=age<=65;
If either condition compared by an OR operator is true, then the result of the OR operation is true.
Be careful when using the OR operator with a series of comparisons (in an IF, SELECT, or WHERE statement, for example). Remember that only one comparison in a series of OR comparisons needs to be true in order to make a condition true. Also, any nonzero, nonmissing constant is always evaluated as true. Therefore, the following subsetting IF statement is always true:
if x=1 or 2;
Although X=1 may be either true or false, the 2 is evaluated as nonzero and nonmissing, so the entire expression is true. In the following statement, however, the condition is not necessarily true, because either comparison can evaluate as true or false:
if x=1 or x=2;
You can also use the IN operator with a series of comparisons. The following statements are equivalent:
if x in (2, 4, 6); if x=2 or x=4 or x=6;
Putting NOT in front of a quantity whose value is false makes that condition true. That is, negating a false statement makes the statement true. Putting NOT in front of a quantity whose value is missing is also true. Putting NOT in front of a quantity that has a nonzero, nonmissing value produces a false condition. That is, the result of negating a true statement is false.
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.