Wednesday, November 9, 2011

SAS Operators

1 Arithmetic Operators


Symbol
Definition
Example
Result
**
exponentiation
a**3
raise A to the third power
*
multiplication1
2*y
multiply 2 by the value of Y
/
division
var/5
divide the value of VAR by 5
+
addition
num+3
add 3 to the value of NUM
-
subtraction
sale-discount
subtract the value of DISCOUNT from the value of SALE
The asterisk (*) is always necessary to indicate multiplication; 2Y and 2(Y) are not valid expressions.
Note: If a missing value is an operand for an arithmetic operator, the result is a missing value. You can use parentheses ( ) to control the order in which the expression is evaluated.

2 Comparison Operators

Symbol
Mnemonic Equivalent
Definition
Example
=
EQ
equal to
a=3
^=
NE
not equal to
a ne 3
¬=
NE
not equal to

~=
NE
not equal to

>
GT
greater than
num>5
<
LT
less than
num<8
>=
GE
greater than or equal to2
sales>=300
<=
LE
less than or equal to3
sales<=100

IN
equal to one of a list
num in (3, 4, 5)
1 The symbol that you use for NE depends on your personal computer.
2 The symbol => is also accepted for compatibility with previous releases of SAS. It is not supported in WHERE clauses or in PROC SQL.
3 The symbol =< is also accepted for compatibility with previous releases of SAS. It is not supported in WHERE clauses or in PROC SQL.
Note: You can add a colon (:) modifier to any of the operators to compare only a specified prefix of a character string (name =: ‘St’ would give you all the values of the name variable that start with a capital S followed by a lower case t).
In addition to the above comparison operators, there is the MIN (><) operator which returns the lesser of the two values. The MAX (<>) operator returns the greater of the two values. For example, if A is less than B, then A><B returns the value of A (the minimum or lesser value).

3 Logical Operators

Symbol
Mnemonic Equivalent
Example
&
AND
(a>b & c>d)
|
OR1
(a>b or c>d)
!
OR

¦
OR

¬
NOT2
not(a>b)
NOT

~
NOT

1 The symbol that you use for OR depends on your operating environment.
2 The symbol that you use for NOT depends on your operating environment.

Note: Logical operators are also called Boolean operators. For an AND expression to be true, both sides of the expression must be true (a must be greater than b as well as c must be greater than d, above). For an OR expression to be true, only one side of the expression must be true (a must be greater than b or c must be greater than d, above). The NOT logical operator is my favorite. (Just listen to the name: The not logical operator! It’s what I tell my husband I’m being when I’m not following his rules of logic.) Anyway, the NOT logical operator reverses the logic of the AND or OR expression (a cannot be greater than b, above).

No comments:

Post a Comment

Blog Archive