If statement <executable Statement>
Data B; Set sashelp. class; if _ n _ le 4; * If the IF is true, continue to execute the statement after the if statement, and finally output the observation that meets the if condition, if false, the system immediately returns to the beginning of the Data step and continues executing the next set statement; y = 'now ';
/*
Y = 'now ';
If _ n _ le 4; the same result can also be obtained, but the efficiency is relatively low, because the value assignment statement of Y must be repeated.
*/Run;
The other two formats of if
If X = 3 then Y = 4; then is used to express only one piece of data.
If X = 3 then do y = 4; Z = 5; end; use then do end for multiple statements to be expressed;
Where Statement (where = option) <unexecutable Statement>
In the systemObserve before reading PDVA condition that data must meet.
Where where-expression-1 <logical-operator where-expression-N>;
Logical-OperatorCan be and, and not, or, or not
Where expression OPERATOR: between and, is missing (is null), contain (?) , Like, same and, in
TIPS:
1: The where statement cannot be used with automatic variables because the where statement is executed before PDV.
2: when using the where statement, you must ensure the integrity of the data set to be read. You cannot use firstobs = 2 or other options that cannot be fully read into the data set.
3: When the where option and where statement act on a dataset at the same time, the system only considers the where Option
4: when the data step contains the where statement and by statement, the where statement is executed before the by statement, and the by group re-defines the first/last variable for the dataset after the execution.
5: The like can be used in places where contains can be used. Therefore, we should first consider using like. Where X like a_ B %; '_' indicates that there is exactly one matching character, '%' indicates that any number of characters can be replaced.
Data A; input x [email protected] @; cards; 1 10 1 20 0 200 2 30 2 40 3 60 4 70 3 80 4 400; run; proc sort data = A; by X; run; Data B; set a; * Where X; * The following conditions are not added: to filter numeric data whose X is not 0 or not missing, only applicable to numeric data; where X is not missing; * filtering data that is not missing X includes 0 values, applicable to both numeric and numeric values; run; proc print data = B Noobs;
The most important differences between where and if
1: Where is not executable, if is executable
2: Where has its own specific expression. If is a general expression, for example, where X is missing;
3: Where can only be selected from the existing SAS data set. If statements can also be selected from observations generated by input statements. * Generally, commercial applications use existing SAS data sets;
4: the efficiency of Where is higher than that of if.
5: when to use if and when to use where? If you need to process PDV observations to determine which observations, you can only use if. Others can use where