IF THEN ELSE SAS statements

IF THEN ELSE SAS control statements produce a result that is either non-zero, 0 or missing.

The expression is true if a non-zero or non-missing result is generated. 0 or non-missing results are to be false.   

For maximum performance, use the IF-THEN-ELSE conditional statements instead of multiple IF-THEN statements.

The control returns to the top of the block and slips the ELSE clause when one of the conditions in the IF-THEN-ELSE block is satisfied.

When multiple IF-THEN blocks are used, each condition is checked sequentially. You can read in detail how SAS processes an –  if then else statements in the Michael Leitson paper.

Use IF-THEN/ELSE statements with conditions of decreasing probability to increase efficiency.

There are various statements that control the flow of execution of statements within the data step.



 IF condition THEN action

SAS evaluates the condition following the IF statement to determine whether it is true or false. If the condition is true, SAS takes the action that follows the keyword THEN.

If the condition is false, SAS ignores the THEN clause and proceeds to the next statement in the DATA step.


if x then delete

Using Comparison Operators in IF-THEN Statements

You can comparison operator to specify the conditions in an IF-THEN statement.

SAS uses the following syntax for the standard comparison operators. You can use either the symbol or the mnemonic code when specifying the conditions.

If the staments


if age lt 18 then delete;

Using Logical Operators in IF-THEN Statements

In addition to comparison operators, you can also use logical operators when specifying multiple conditions for an IF-THEN statement.

Logical Operators


if status='OK' and type=3 then count+1;


If-then-else statements are used to conditionally execute a SAS statement. The statements following the IF statements are executed if the expression evaluates to true.

The else part is optional, and if it is omitted, control passes to the first statement after the if-then statement when the expression is false.


data test;
	input x $ y;

	if x eq "blue" then
crimson 1 blue 2

In the above example, x is compared to blue after input. A new variable, z, is set equal to 1 when x equals blue, otherwise, z is set equal to 0.

How to avoid values getting truncated when creating a new calculated column?

SAS sets the length of variables when the first time the variable is assigned a value. For example, consider the below example.

data class(keep=age eligible);
set sashelp.class;
if age lt 14 then eligible="NO";
else eligible="YES";
proc print data=class(obs=5);

If then else sas

Note the value of eligible has been truncated to YE. 

In order to avoid these truncations after the SET statement, you need to specify a LENGTH statement to the maximum length of the variable.

If you don’t it sets the length by the length of the first time the variable is assigned a value. 

The modified SAS code is as below.

data class(keep=age eligible);
set sashelp.class;
if age lt 14 then eligible="NO";
else eligible="YES";
 proc print data=class(obs=5);

ELSE IF Statements

A more efficient way is to use IF-THEN-ELSE statements when comparing for multiple conditions. The general form is as follows:

IF condition THEN action 
ELSE IF condition THEN action 
ELSE IF condition THEN action

In this method once a condition is met, SAS stops checking the other IF-THEN statements that follow ELSE.


IF marks = . THEN Grade='Incomplete'
ELSE IF marks >= 90 THEN Grade='A'
ELSE IF marks >= 80 THEN Grade='B'
ELSE IF marks >= 7o THEN Grade='C'
ELSE IF marks >= 60 THEN Grade='D'
ELSE IF marks 

You can also use the ELSE statement in a series which would specify an action that will be executed when the previous IF-THEN statements fail.


IF condition THEN action 
IF condition THEN action
ELSE IF condition THEN action
ELSE action


IF marks = . THEN Grade='Incomplete';
ELSE IF marks >= 90 THEN Grade='A';
ELSE IF marks >= 80 THEN Grade='B';
ELSE IF marks >= 7o THEN Grade='C';
ELSE IF marks >= 60 THEN Grade='D';
ELSE  Grade='F';


A goto statement tells SAS to jump immediately to another statement in the same data step and begin executing statements from that point.

data test;
input x y;
if 1 

The if statement checks to see if the input value of x is greater than or equal to 1; if it is not, then x is set equal to 3; if it is then the SAS program jumps to the statement labelled OK.

This is a return statement that tells SAS to begin processing a new statement.


The stop statement stops processing a SAS data step. The observation being processed when the stop statement is encountered just isn't added to the data set and processing resumes with the first statement after this data step.

data example;
input x y z;
if x = 2 then stop;

When a value of x = 2 is encountered SAS stops building the data set.


The do statement designates a group of statements to be executed as a unit until a matching end statement is encountered. A number of do statements can be nested within do groups.

A simple do is often used within if-then-else statements to designate a group of statements to be executed relying on whether or not if the condition is true or false.

data example;
input x;
if x gt 0 then
y = x*x;
z = -x;
else w = x;

The statements inside the do block create two new variables y and z when x > 0 and one new variable w when x ≤ 0.

Note that these variables are equal to. when they aren't assigned anything.


The select-otherwise statement replaces a sequence of if-then-else statements. The select statement takes the form:

select (expression);
 when (expression1 ) statement1 ;
 when (expression2 ) statement2 ;
 otherwise statement;

In this group of statements, SAS compares an expression to an expression. If they are equal, then the statement is executed.

If none are equal, then the statement is executed. In otherwise statement is optional. An end statement ends a choose group. For instance, the program

data example;
input x;
when(1) z = 0;
when(2) z = 0;
when(3) z = 0;
otherwise z = 1;

A variable z is added to the data set, which takes the value 0 when x = 1, 2, or 3 and the value 1 when x takes another value.

Also Read, SAS Loops

Every week we'll send you SAS tips and in-depth tutorials


Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap