summarize from SAS help :SAS operators in Expressions
1 arithmetic operator(算术运算符)
operator | meaning/definition | Example | Result |
+ | addition | num+3 | add 3 to the value of num |
- | subtraction | sale-discount | subtract the value of discount from the value of sale |
* | multiplication | 2*y | multiply 2 by the value of y |
/ | division | var/5 | divide the value of var by 5 |
** | exponentiation | a**3 | raise A to the third power |
2 comparison operator (这里数值型变量和字符型变量都可以用哦)
operator | meaning/definition | Equivalent | example |
= | equal to | eq | a=3 |
< | less than | lt | a<3 |
<= | less than or equal to | le | a le 3 |
>= | greater than or equal to | ge | a ge 3 |
^= | not equal to | ne | a ne 3 |
in | equal to one of a list | a in (3, 4, 5) |
The symbol that you use for NE depends on your personal computer.
2.1 The IN Operator -numeric comparison 关于in 这里 ()变量之间 , 和空格 是等效的,Individual values can be separated by commas or spaces,You can use a colon to specify a range of sequential integers. The three forms of the IN comparison are equivalent:
You can use multiple ranges in the same IN list, and you can use ranges with other constants in an IN list. The following example shows a range that is used with other constants to test if X is 0, 1, 2, 3, 4, 5, or 9. if x in (0,9,1:5); expression can be any valid SAS expression, but is usually a variable name when it is used with the IN operator. value must be a constant. 2.2 The IN Operator in Character Comparisons You can use the IN operator with character strings to determine whether a variable's value is among a list of character values. The following statements produce the same results:
2.3 Numeric Comparisons and Character Comparisons A blank, or missing character value, is smaller than any other printable character value. For example, because You can compare only a specified prefix of a character expression by using a colon (:) after the comparison operator. SAS truncates the longer value to the length of the shorter value during the comparison. In the following example, the colon modifier after the equal sign tells SAS to look at only the first character of values of the variable LastName and to select the observations with names beginning with the letter if lastname=:'S'; 举例如下 /*创建stuinfo表*/ |
3 Logical Operators
Symbol | Mnemonic Equivalent | Example |
---|---|---|
& | AND |
|
| | OR1 |
|
! | OR | |
¦ | OR | |
¬ | NOT2 |
|
∘ | NOT | |
~ | NOT | |
1The symbol that you use for OR depends on your operating environment. | ||
2The symbol that you use for NOT depends on your operating environment. |
3.1 and
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;
3.2 The not operator
For example, the following two expressions are equivalent:
-
not(name='SMITH')
-
name ne 'SMITH'
Furthermore, NOT(A&B) is equivalent to NOT A|NOT B, and NOT(A|B) is the same as NOT A & NOT B. For example, the following two expressions are equivalent:
-
not(a=b & c>d)
-
a ne b | c le d
4 Boolean Numeric Expressions
0 | . = False 1 = True
For example, suppose that you want to fill in variable Remarks depending on whether the value of Cost is present for a given observation. You can write the IF-THEN statement as follows:
if cost then remarks='Ready to budget';
This statement is equivalent to:
if cost ne . and cost ne 0 then remarks='Ready to budget';
5 The MIN and MAX Operators-目前工作中还没用到呢,,,,
6Order of Evaluation in Compound Expressions
这个下回学习,太多了