WHERE condition wins against IF condition in the following cases :
1. The WHERE statement can be used in procedures to subset data while IF statement cannot be used in procedures.
Look at the log of WHERE and IF statements shown below :
2. WHERE can be used as a data set option while IF cannot be used as a data set option.
Look at the log of WHERE and IF conditions shown below :
3. The WHERE statement is more efficient than IF statement. It tells SAS not to read all observations from the data set.
Look at the LOG (shown below) after using WHERE statement, you only see a count of the number of observations that meet the criteria in the WHERE statement.
Only 6 observations were read from the dataset READIN. In actual, the dataset READIN contains 14 observations.
All 14 observations are read and the 6 that meet the IF criteria are placed in the new data set.
NOTE : Both statements produced the same result.
4. The WHERE statement can be used to search for all similar character values that sound alike while IF statement cannot be used.
For example, you want to filter out all the names that sound alike 'Sam'.
IF condition wins against WHERE condition in the following cases :
1. When reading data using INPUT statement.
IF Statement
WHERE Statement
WHERE statement can not be used when specifying an input statement.
2. When it is required to execute multiple conditional statements
Suppose, you have data for college students’ mathematics scores. You want to rate them on the basis of their scores.
Conditions :
1. If a score is less than 40, create a new variable named “Rating” and give “Poor” rating to these students.
2. If a score is greater than or equal to 40 but less than 75, give “Average” rating to these students.
3. If a score is greater than or equal to 75 but less than or equal to 100, give “Excellent” rating to these students.
This can be easily done using IF-THEN-ELSE IF statements. However, WHERE statement requires variables to exist in the data set.
3. When it is required to use newly created variables.
IF statement doesn't require variables to exist in the READIN data set while WHERE statement requires variable to exist in the data set.

SOURCE
No comments:
Post a Comment