The IF-THEN-ELSE statement is used to conditionally process statement(s) when certain condition(s) are met.
Let's look at some examples.

IF-THEN Statement
The IF-THEN statement tells SAS to execute a statement if the condition specified is true.
data students2;
set students;
if results > 50 then exam = "Pass";
run;
- Exam = "Pass";

The ELSE statement is optional. It can be used to execute a statement if the condition is not true.
data students2;
set students;
if results > 50 then exam = "Pass";
else exam = "Fail";
run;

DO Group
In the example above, the following statement is executed when the result is greater than 50:
- Exam = "Pass";
Sometimes, we might need to execute more than one statement when the condition is met. You can use the DO group to execute more than one statement in the IF-THEN statement.
data students2;
set students;
if results <= 50 then do;
exam = "Fail";
Retake = "Yes";
end;
run;
- Exam = "Fail";
Retake = "Yes";
The DO group ends with the END statement.
When the result is not greater than 50, the character values "Fail" and "Yes" are assigned to the EXAM and RETAKE variables, respectively.

Do you have a hard time learning SAS?
Take our Practical SAS Training Course for Absolute Beginners and learn how to write your first SAS program!
IF Statement
The IF statement can also be used to subset a data set.
data students2;
set students;
if results > 70;
run;
This tells SAS to subset the data set and keep only the students whose result is greater than 70.
