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  

这个下回学习,太多了

SAS 中,`WHERE` 语句是一种非常高效的工具,用于在数据处理之前对数据集进行筛选。它可以在 `DATA` 步或 `PROC` 步中使用,主要作用是选择满足特定条件的数据观测行[^2]。 ### 基本语法 `WHERE` 语句的基本格式如下: ```sas WHERE logical-expression; ``` 其中 `logical-expression` 是一个逻辑表达式,用于指定筛选条件。例如,从数据集 `test1` 中筛选出性别为女性 (`sex='F'`) 且年龄大于 20 (`age>20`) 的观测: ```sas data test2; set test1; where sex='F' and age > 20; run; ``` 该语句创建了一个新数据集 `test2`,其中只包含满足条件的观测[^2]。 ### 多条件筛选 可以使用逻辑运算符(如 `AND`、`OR`、`NOT`)来组合多个条件,以实现更复杂的筛选逻辑。例如,筛选出年龄在 18 到 40 岁之间的男性: ```sas data young_male; set population; where sex='M' and age >= 18 and age <= 40; run; ``` ### 使用 WHERE 选项 除了在 `DATA` 步中直接使用 `WHERE` 语句,还可以在 `SET`、`MERGE` 或 `UPDATE` 语句中使用 `WHERE=` 数据集选项来筛选输入数据。例如: ```sas data filtered_data; set original_data(where=(age > 30)); run; ``` 此方法在处理大型数据集时特别有用,因为它可以在读取数据之前就进行筛选,从而减少内存使用和提高效率[^5]。 ### 与 IF 语句的对比 虽然 `WHERE` 语句和 `IF` 语句都能用于数据筛选,但它们的执行机制不同。`WHERE` 语句是在数据进入程序数据向量(PDV)之前进行筛选,而 `IF` 语句是在数据已经进入 PDV 后才进行判断。因此,`WHERE` 通常比 `IF` 更高效,尤其是在处理大型数据集或索引数据集时。 ### 注意事项 - `WHERE` 语句只能作用于已存在的变量,不能用于在 `DATA` 步中创建的新变量。 - 如果数据集已经建立了索引,`WHERE` 语句可以利用索引来加速数据访问。 - 在使用 `WHERE` 语句时,逻辑表达式的书写要准确,避免出现语法错误。 ### 示例代码 以下是一个完整的示例,展示了如何使用 `WHERE` 语句筛选出年龄在 41 到 64 岁之间的女性: ```sas data middle_age_female; set patient_data; where sex = 'F' and age >= 41 and age <= 64; run; ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值