Select Page

IF-THEN, ELSE, and ELSE IF statements in SAS can be used to regulate the flow of your programmer based on defined criteria. Here’s a quick rundown of these conditional statements:

1. IF-THEN Condition:

The IF-THEN statement is used to run a piece of SAS code conditionally based on a defined condition.

The fundamental syntax is as follows:

IF condition THEN statement;

The statement(s) after the THEN keyword are performed if the condition is true. The code after the THEN is skipped if the condition is false.

Example-

IF Age > 20 THEN Status = ‘Adult’;

2. IF-THEN-ELSE Condition:

The IF-THEN-ELSE statement is an extension of the IF-THEN statement that specifies an alternate action if the condition is not satisfied.

Basic Syntax

IF condition THEN statement1;

ELSE statement2;

Statement1 is executed if the condition is true; statement2 is executed if the condition is false.

Example

IF Score >= 50 THEN Result = ‘Pass’;

ELSE Result = ‘Fail’;

3. IF-THEN-ELSE (Multiple Conditions) Statement:

The IF-THEN-ELSE IF statement allows you to progressively assess several conditions and run distinct code blocks depending on which condition is true.

Basic Syntax

IF condition1 THEN statement1;

ELSE IF condition2 THEN statement2;

ELSE statement3;

SAS examines conditions in the order they are encountered and runs the code block corresponding with the first true condition it encounters. If none of the requirements are met, the ELSE statement’s code block is performed.

Example

IF Score >= 80 THEN Grade = ‘A’;

ELSE IF Score >= 70 THEN Grade = ‘B’;

ELSE IF Score >= 60 THEN Grade = ‘C’;

ELSE Grade = ‘D’;

These conditional statements let you to make judgements in your SAS programmers based on variable values or calculation outcomes. They are required for data manipulation, the creation of derived variables, and the generation of conditional formatting reports.