SAS中的运算符

summarize from SAS help :SAS operators in Expressions 

1 arithmetic operator(算术运算符)

operatormeaning/definitionExampleResult
+additionnum+3add 3 to the value of num
-subtractionsale-discountsubtract the value of discount from the value of sale
*multiplication2*ymultiply 2 by the value of y
/divisionvar/5divide the value of var by 5
**exponentiationa**3raise A to the third power

2 comparison operator  (这里数值型变量和字符型变量都可以用哦)

operatormeaning/definitionEquivalentexample
=equal toeqa=3
less thanlta<3
<=less than or equal tolea le 3
>=greater than or equal togea ge 3
^=not equal tonea ne 3

in

equal to one of a lista 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:

  • y = x in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

  • y = x in (1 2 3 4 5 6 7 8 9 10);

  • y = x in (1:10);

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:

  • if state in ('NY','NJ','PA') then region+1;

  • if state in ('NY' 'NJ' 'PA') then region+1;

  • if state='NY' or state='NJ' or state='PA' then region+1;

2.3 Numeric Comparisons and Character Comparisons

A blank, or missing character value, is smaller than any other printable character value. For example, because . is less than h, this expression is true:

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 S:

if lastname=:'S';

举例如下

/*创建stuinfo表*/
data stuinfo;
input ID name $ age;  /*$默认是8位*/ /*注意数值型缺失值用.表示*/
cards;  
1 Tom    2
2 marry 6
3 louis .  
4 joy 1
5 Tob 1
; run;
data www; set stuinfo ; where name=:'T';  run;   /*来一招这里,可以通过name=:‘T’筛出首字母是T的名字*/
proc print;/*proc print后面如果不指定数据集就是默认当前数据集*/
run;

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

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  

这个下回学习,太多了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值