Site icon DataFlair

Decision Making in SAS | Learn IF-THEN & IF-ELSE Statement with Syntax

Decision making in SAS

Decision making in SAS

FREE Online Courses: Enroll Now, Thank us Later!

Previously, we discussed Subsetting Datasets in SAS, now, we will learn Decision Making in SAS. We have come across with statements like IF, THEN and IF ELSE in programming languages like C, C++ or JAVA. SAS language also makes use of these decision making statements to test for conditions within a code.

Let us see how these Statements of Decision Making work in SAS.

Decision Making in SAS

SAS Decision making helps a programmer apply specific conditions on a statement or set of statements and then evaluate it. If the criteria meet the requirements, the program then falls to another set of conditions, if it fails it goes back to the beginning.

Decision Making Statements in SAS

Decision making in SAS can be done through statements, let’s discuss this statement with the help of an example:

1. IF-THEN and IF-ELSE Statement

SAS IF-THEN statement informs SAS to execute a statement if the condition specified is true.

data students1;
set students;
if result>50 then exam = “pass”;
run;

The IF-THEN statement above executes the following statement when the result is greater than 50:

SAS ELSE statement is optional. It can be used to execute a statement if the condition is not true.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Example:

data students2;
set students;
if results>50 then exam=”pass”;
else exam=”fail”
run;
;

The above ELSE statement tells SAS to assign the value “Fail” to the EXAM variable if the result is NOT greater than 50.

2. IF-THEN-ELSE IF Statement

Let’s discuss syntax and example of SAS IF-THEN-ELSE-IF Statement

Syntax:

IF (condition1) THEN result1;
ELSE IF (condition2) THEN result2;
ELSE IF (condition3) THEN result3;

Example:

Suppose we have a dataset containing a variable PLANET_NAME which contains the names of planets. Now, we wish to derive a new variable PLANET_ORDER which contains the corresponding position of each planet (e.g. Mercury = 1, Venus = 2, etc.).

This can easily be accomplished using an IF…THEN…ELSEIF construction as follows:

if upcase(planet_name) = 'MERCURY' then planet_order=1;
else if upcase(planet_name) = 'VENUS' then planet_order=2;
else if upcase(planet_name) = 'EARTH' then planet_order=3;
else if upcase(planet_name) = 'MARS' then planet_order=4;
else if upcase(planet_name) = 'JUPITER' then planet_order=5;
else if upcase(planet_name) = 'SATURN' then planet_order=6;
else if upcase(planet_name) = 'URANUS' then planet_order=7;
else if upcase(planet_name) = 'NEPTUNE' then planet_order=8;

3. IF-THEN-DELETE Statement

The DELETE statement is often used in a THEN clause of an IF-THEN statement or as part of a conditionally executed DO group.

Example:

IF (condition ) THEN DELETE;
data abc;
infile datalines;
input a b c;
d= c - b;
if d <= 0 then delete;
datalines;
1 5 10
0 8 7
1 4 6
;
run;

The output of the above statement will only show two observations because in the second observation d will become less than 0.

Summary

We learned decision making in SAS and decision making statements in SAS Programming Language; IF-THEN, IF-ELSE, and IF-THEN-ELSE IF Statement. It is almost similar to all the IF statements we see in other languages.

If you have any queries related to the article, feel free to ask in the comment section.

Exit mobile version