IF THEN ELSE SAS statements

IF THEN ELSE SAS statements

  • Post author:
  • Post category:Base SAS
  • Post comments:0 Comments
  • Reading time:12 mins read

IF THEN ELSE SAS control statements produce either non-zero, 0 or missing results.

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.

Various statements 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 following statement in the DATA step.Examples:

if x then delete

Using Comparison Operators in IF-THEN Statements

You can compare operators to specify the conditions in an IF-THEN statement.

SAS uses the following syntax for the standard comparison operators. You can use 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 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 execute a SAS statement conditionally. The statements following the IF statements are executed if the expression evaluates to true.

The else part is optional; 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 do 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";
run;
proc print data=class(obs=5);
IF THEN ELSE SAS statements

Note the value of eligible has been truncated to YE. 

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";
run;
 proc print data=class(obs=5);

ELSE IF Statements

A more efficient way is to use IF-THEN-ELSE statements when comparing 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 

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

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.

There is a return statement that 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. Several 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, depending on whether 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 an expression. If they are equal, then the statement is executed.

If none are equal, then the statement is executed. The “Otherwise” statement is optional. An end statement ends a chosen 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

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

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

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