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

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

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.

Condition - Decision Making in SAS

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:Example - Decision Making in SAS

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:

  • Exam = “Pass”;

SAS Decision Making Statement - SAS IF-THEN statement

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

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.

SAS Decision Making Statement - SAS IF-Else statement

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;

SAS Decision Making Statement - SAS IF-THEN-ELSE IF Statement

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.

  • Use the DELETE statement when it is easier to specify a condition that excludes observations from the data set or when there is no need to continue processing the DATA step statements for the current observation.
  • Do not confuse the DROP statement with the DELETE statement. The DROP statement excludes variables from an output data set; the DELETE statement excludes observations.
  • When DELETE executes, the current observation is not written to a data set, and SAS returns immediately to the beginning of the DATA step for the next iteration.

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.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

1 Response

  1. BEERESREEKANTH says:

    GOOD

Leave a Reply

Your email address will not be published. Required fields are marked *