Site LogoIntroduction to SAS: IF, IF-THEN and IF-THEN-ELSE Statements

Download the SAS Tutorial in a single PDF file from the SAS Tutorial page. The online tutorial will be eliminated in August 2017.

Contents Index

Conditional statements (if-then statements, if-then-else statements) are often used to assign values to variables. Example:

data new;
input prefix age sex $;
if prefix=821 then city='auburn';
if age <30 then young=1;
else young=0;
if age<30 and sex='f' then yw=1;
if age>80 or prefix='749' then nogood=1;

The logical connectives AND and OR are used to create compound tests, as illustrated above.

A counterless do-loop can be used to have more complicated processing after an if-branch. Example:

if age<30 then do;
	if income<20000 then cat=1;
	if income>20000 and income <30000 then cat=2;

The return statement can be used to branch to the bottom of the data step program.

Do-loops and if-then statements can be nested.

© Copyright 2016 Jerry Alan Veeh. All rights reserved.