SAS Statements

IF-THEN/ELSE Statement in SAS

In SAS you can use the IF-THEN/ELSE statement to execute other statements that meet a specific condition. Also, you can use this statement to subset a data set. This article discusses the syntax of the IF-THEN/ELSE statement and its different applications. We use examples to explain these applications.

The IF-THEN/ELSE Syntax

IF expression THEN statement;
<ELSE statement;>

The IF-THEN/ELSE statement always contains the IF-THEN part. The ELSE part is optional. The expression is a condition that SAS can evaluate and is either True or False. The statement is another SAS statement or a group of SAS statements within a DO group. The statement will be executed if the forgoing expression is True.

IF-THEN/ELSE Examples

For all the examples below, we use a data set with tree columns:

  • City: A city somewhere in the world
  • Beach: “Yes” or “No”
  • Temperature: Temperature in degrees Celsius

We created the data set with the datalines statement and the delimiter option.

data work.ds;
	infile datalines dlm=',';
	length city $ 25;
	input city $ beach $ temperature;
	datalines;
Amsterdam, No, 30
Barcelona, Yes, 31
Buenos Aires, Yes, 15
Detroit, No, 31
Lima, Yes, 14
Los Angeles, Yes, 25
Madrid, No, 35
Moscow, No, 10
Paris, No, 20
Sydney, Yes, 10
;
run;
IF-THEN/ELSE data set

In the first example, we use only the IF-THEN statement. Rows 1, 2, 4 and 7 meet the expression “temperature >= 30”. So, for these rows, the column “Warm” is filled with “Yes”. However, for the other rows, the column “Warm” is empty. This is because we didn’t use the ELSE part.

data work.warm_1;
	set work.ds;
	
	if temperature >= 30 then warm = 'Yes';
run;
IF-THEN statement

If we add the ELSE statement, then the “Warm” column will be filled.

data work.warm_2;
	set work.ds;
	
	if temperature >= 30 then warm = 'Yes';
	else warm = 'No';
run;
IF-THEN/ELSE statement

Besides a single IF-THEN/ELSE statement, you can also create multiple ELSE statements. The example below creates the column “Weather” based on three different conditions.

data work.weather;
	set work.ds;
	
	if temperature >= 30 then weather = 'Warm';
	else if 15 <= temperature < 30 then weather = 'Nice';
	else weather = 'Cold';
run;
IF-THEN/ELSE statement with multiple conditions.

Until now, we have seen that one simple line of code was executed when the IF or ELSE statement was met. However, it is also possible to execute multiple lines of code if the IF or ELSE statement is satisfied. In this case, the lines of code you want to execute need to be within a DO block.

data work.action;
	set work.ds;
	
	if temperature >= 30 and beach = 'Yes' then do;
		warm = 'Yes';
		action = 'Go to the beach';
	end;
	else if temperature >= 25 and beach = 'No' then do;
		warm = 'Yes';
		action = 'Eat Icecream';
	end;
	else do;
		warm = 'No';
		action = 'Something else';
	end;
run;
IF-THEN/ELSE statement with DO block.

Finally, the IF-THEN statement can be used to subset a data set. For example, the code below shows how the cities with a beach are stored in one data set while the cities without a beach are stored in another.

data work.beach work.no_beach;
	set work.ds;
	
	if beach = 'Yes' then output work.beach;
	else output work.no_beach;
run;
IF-THEN statement to subset data.
IF-THEN statement to subset data.

The official SAS documentation of the IF-THEN/ELSE statement can be found here.