In this blog, we will discuss some of the SAS control statements with examples.
IF or WHERE statement is applied to select observations. WHERE condition is applied while reading observations from Input dataset where as IF condition is applied at Program Data Vector (PDV).
- Using the Where statement may improve the efficiency of SAS programs because SAS is not required to read all observations from the input dataset.
- The Where statement cannot be executed conditionally, i.e., it cannot be a part of an IF-THEN statement
- Where statements can contain multiple WHERE expressions that are joined by logical operators.
Scenario: For understanding sales performance of “Sandal” product , we need to pull data. Considering a long list of product and volume of data, it is better to filter out other products and select only “Sandal” data.
Scenario: Out of curiosity and understanding data anomaly, we want to find the athletes which have high weight and low height.
%* Athletes with low height and high weight;
where Weight >100 and Height_cm <=150;
IF statement causes the DATA step to process only those observations from a SAS data set (or raw data records) that meet the condition of the expression specified in the IF statement
If the expression is true, SAS executes the statements in the DATA step and includes the current observation in the dataset, resulting SAS dataset contains a subset of the original SAS dataset (or external file).
If the expression is false, no further statements are executed for that observation (or record), the current observation is not written to the SAS dataset & the remaining statements in the DATA step are not executed, SAS immediately returns to beginning of the DATA step because the subsetting IF statement does not require additional statements to stop processing observations.
Scenario: An organisation has 10M customer base. A Marketing Managed who has recently joined the organisation is looking to understand the customers. He requested for Profiling Dashboard. Before proceeding further analyst wants to create a small sample of data and then run the process on all the customers.
Hint: IF condition and ranuni function.
IF THEN ELSE Statement
SAS evaluates the expression in an IF-THEN statement and check whether a condition is true or false. If the conditions that are specified in the IF clause are met, the IF-THEN statement executes a SAS statement for observations that are read from a SAS dataset (or for computed values).
An optional ELSE statement gives an alternative action if the THEN clause is not executed. The ELSE statement if used must follow the IF-THEN statement.
%* IF THEN ELSE;
input product $ sales;
DATA D2 D3;
IF product = 'LOREAL' then output D2;
ELSE IF product = 'PONDS' then output D3;
In the above example, based on condition (IF /ELSE) input observations are put into the output dataset. If "IF condition" is true then in the dataset D2 and when "ELSE IF condition " is true then output into D3.
Next Blog on Do Loops