SAS 编程中的 If-Then-Else 语句
使用条件语句时使用的比较运算符。
Symbol | Mnemonic | Meaning |
---|---|---|
= | EQ | equals |
^= or ~= | NE | not equal |
> | GT | greater than |
< | LT | less than |
>= | GE | greater than or equals |
<= | LE | less than or equals |
in | IN | selecting multiple values |
- IF 语句
句法:
IF (condition is true) => It means subsetting a dataset.
例子:
Data readin; Input R_Num Subj1-Subj3; cards; 112 21 22 23 105 23 24 26 95 25 25 26 125 26 26 24 122 15 25 16 86 26 16 15 ; Data readin1; Set readin; IF R_Num GE 100; run;
输出:
IF R_Num GE 100 => 这将告诉 SAS 仅保留值大于或等于 100 的卷号。换句话说,您正在删除值小于或等于 100 的卷号。
也可以使用 IF-THEN DELETE 语句来完成。
- 如果-则删除
句法:
IF (condition is true) THEN (delete the given statements);
例子:
Data readin; Input R_Num Subj1-Subj3; cards; 112 21 22 23 105 23 24 26 95 25 25 26 125 26 26 24 122 15 25 16 86 26 16 15 ; Data readin1; Set readin; IF R_Num LT 100 THEN DELETE; run;
输出:
IF R_Num LT 100 THEN DELETE => 这将告诉 SAS 删除所有值小于 100 的卷号。
- IF-THEN-ELSE 语句
任务2:假设您要在所有R_Num 上设置一个标签。条件是:
If the value of R_Num is less than or equal to 100 sets "Old" tag otherwise set "New" tag.
IF (condition is true) THEN (perform this action); ELSE (perform the set of statements that is set when condition is false);
Data readin; Input R_Num Subj1-Subj3; cards; 112 21 22 23 105 23 24 26 95 25 25 26 125 26 26 24 122 15 25 16 86 26 16 15 ; Data readin1; Set readin; IF R_Num LE 100 THEN TAG ="Old"; ELSE TAG ="New"; run;
输出:
- IF-THEN-ELSE IF 语句
任务 3:假设您被要求更新 TAG 列。
标记条件如下:
- 如果 R_Num 的值小于 75,则 TAG = “Old”
- 如果 R_Num 的值大于或等于 75 且小于 100,则 TAG = “New”
- 如果 R_Num 的值大于或等于 100,则 TAG = “未选中”
IF (condition is true) THEN (perform this action); ELSE IF (perform the set of statements when second condition is true); ELSE IF (perform the set of statements when third condition is true);
Data readin; Input R_Num Subj1-Subj3; cards; 112 21 22 23 105 23 24 26 95 25 25 26 125 26 26 24 122 15 25 16 86 26 16 15 ; Data readin1; Set readin; length TAG $20; IF R_Num < 75 THEN TAG ="Old"; ELSE IF 75 <= R_Num < 100 THEN TAG = "New"; ELSE IF R_Num >= 100 THEN TAG ="Unchecked"; run;
输出: