IF THEN ELSE SAS statements5 min read

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 on how SAS processes a 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-THEN STATEMENTS IN SAS

Syntax:

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.

Examples:

  1. 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

Example:

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

Example:

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

IF THEN ELSE SAS Statements

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.

Example:

data test;
	input x $ y;

	if x eq "blue" then
		z=1;
	else
		z=0;
	cards;
crimson 1 blue 2
;
run;

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 a variables when first time the variable is assigned a value. For example, consider the below example.

data class(keep=age eligible);
set sashelp.class;
if age < 14 then eligible="NO";
else eligible="YES";
run;

proc print data=class(obs=5);

If then else sas

Note the value of eligible has bee 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 < 14 then eligible="NO";
else eligible="YES";
run;

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.

Example:

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 < 60 THEN Grade='F';

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.

Syntax:

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

Example:

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';

GOTO and RETURN

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<=x then goto OK;
x = 3;
OK: return;
cards;

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 which tells SAS to begin processing a new statement.

STOP

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;
cards;

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

DO-END

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
do;
y = x*x;
z = -x;
end;
else w = x;
cards;

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.

SELECT-OTHERWISE

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;
 end;

In this group of statements, SAS compares an expression to 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;
select(x);
when(1) z = 0;
when(2) z = 0;
when(3) z = 0;
otherwise z = 1;
end;
cards;

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

Leave a Comment

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

Copy link