Home » SAS Decision Making | If-Then-Else | If-Then-Delete

SAS Decision Making | If-Then-Else | If-Then-Delete

by Online Tutorials Library

SAS- Decision Making

Decision Making is the idea of choosing a logical option from the available options. In SAS, Decision making helps a programmer to applya specific condition on a single statement or set of statements.

The decision is dependent on the condition, i.e., if the conditionis true, then the program falls on another set of conditions or another statement, but if the condition is false, then it goes back on the beginning statement of the code.

Consider the following image:

SAS- Decision Making

There are three types of Decision Making Statements in SAS:

  • If -Then Statement
  • If-Then-Else – If Statement
  • If-Then-Delete Statement

If?Then Statement

The If-Then statement instructs SAS to execute a statement if the specified condition is true.Let’s understand it through an example.

Example:

In the following code we provided two conditions. The first condition is, if the result of a student is more than 50, then add that student to the “Pass” group.

The second condition is, if the result of a student is less than 50, then add that student to the “Fail” group.

Execute the above code in SAS Studio:

SAS- Decision Making

Output:

SAS- Decision Making

As per the above output, when the condition “result>50” is true or the result is greater than 50, it has been added to the “Pass” group. And when the condition “result<50” is true or the result is less than 50, it has been added to the “Fail” group.

If-Then-Else-If Statement

The If-Then-Else-If statement instructs SAS to execute a statement if the specified condition is trueand execute the else part of statement if the condition is false.

In else part, we can give another condition by using If. Hence, we can give multiple conditions by using If-Then-Else-If statement.Let’s understand it through an example.

Example:

In the following code we provided two conditions. If the first condition is true, then it will execute ifthen part, but if the condition is false, then it will execute elseif part of the code.

Execute the above code in SAS Studio:

SAS- Decision Making

Output:

SAS- Decision Making

As per the above output, the result which is greater than 50 has been added to the “Pass” group as per the first condition, and the result which is less than 50 has been added to the “Fail” groupas per the second condition.

IF-THEN-DELETE Statement

IF-THEN-DELETE Statementinstructs SAS to execute a statement if the specified condition is true, and if the condition is false, then deleteall the observations after the condition is being false. Let’s understand it through an example.

Example:

In the following code we haveprovided a condition. If the condition is true, then it will execute if part, and if it is false, then it will delete all the observations after the condition is being false.

Execute the above code in SAS Studio:

SAS- Decision Making

Output:

SAS- Decision Making

As per the above output, SAS is displaying only the results that are more than 50. All the results which are less than 50 have been deleted.


Next TopicSAS Functions

You may also like