IF Statement, Subsetting

Continues processing only those observations that meet the condition of the specified expression.
Valid in: DATA step
Category: Action
Type: Executable

Syntax

Arguments

expression
is any SAS expression.

Details

The subsetting IF statement causes the DATA step to continue processing only those raw data records or those observations from a SAS data set that meet the condition of the expression that is specified in the IF statement. That is, if the expression is true for the observation or record (its value is neither 0 nor missing), SAS continues to execute statements in the DATA step and includes the current observation in the data set. The resulting SAS data set or data sets contain a subset of the original external file or SAS data set.
If the expression is false (its value is 0 or missing), no further statements are processed for that observation or record, the current observation is not written to the data set, and the remaining program statements in the DATA step are not executed. SAS immediately returns to the beginning of the DATA step because the subsetting IF statement does not require additional statements to stop processing observations.

Comparisons

  • The subsetting IF statement is equivalent to this IF-THEN statement:
    if not (expression)
       then delete;
  • When you create SAS data sets, use the subsetting IF statement when it is easier to specify a condition for including observations. When it is easier to specify a condition for excluding observations, use the DELETE statement.
  • The subsetting IF and the WHERE statements are not equivalent. The two statements work differently and produce different output data sets in some cases. The most important differences are summarized as follows:
    • The subsetting IF statement selects observations that have been read into the program data vector. The WHERE statement selects observations before they are brought into the program data vector. The subsetting IF might be less efficient than the WHERE statement because it must read each observation from the input data set into the program data vector.
    • The subsetting IF statement and WHERE statement can produce different results in DATA steps that interleave, merge, or update SAS data sets.
    • When the subsetting IF statement is used with the MERGE statement, the SAS System selects observations after the current observations are combined. When the WHERE statement is used with the MERGE statement, the SAS System applies the selection criteria to each input data set before combining the current observations.
    • The subsetting IF statement can select observations from an existing SAS data set or from raw data that are read with the INPUT statement. The WHERE statement can select observations only from existing SAS data sets.
    • The subsetting IF statement is executable; the WHERE statement is not.

Example: Limiting Observations

  • This example results in a data set that contains only those observations with the value F for the variable SEX:
    if sex='F';
  • This example results in a data set that contains all observations for which the value of the variable AGE is not missing or 0:
    if age;

See Also

WHERE-Expression Processing in SAS Language Reference: Concepts
Data Set Options:
WHERE= Data Set Option in SAS Data Set Options: Reference