Signpost with conditional instructions

Conditional statements in SAS Base

After getting familiar with the rudiments of Base SAS, in this article we will deal with how to handle conditional statements.

Introduction

This type of construct allows you to execute a certain portion of code rather than another, once a condition has been evaluated.
To better understand the concept set out above, which could be new for those who are not accustomed to programming, let’s start with a simple example. This is a classic case where conditional statements enter our daily life. Suppose we have to cook some pasta, we know that we can throw the pasta into the pot with the water only if it reaches boiling temperature, i.e. 100°C.

butta la pasta
If water boils… it’s time to drop the pasta!

So, let’s try to write the conditional statement in pseudocode that formalizes the case outlined above:

IF WaterTemperature >= 100 THEN
	Butta la pasta nella pentola
ENDIF

This is therefore the simplest possible case: an if with a single statement and no else branch. In this case, in fact, it is necessary to carry out an action, to throw the pasta into the pot, only and only when the water has reached boiling temperature. Since there is no else branch in the construct, if the condition doesn’t occur we won’t have to do anything, or in our case wait for the water to reach boiling temperature, also because I’m starting to get hungry!

Conditional statements in SAS

So let’s go on to formalize and generalize concepts introduced through the practical example in SAS Base. In SAS, there are several ways to express a conditional statement, depending on the particular need:

  • If with single statement , with and without branch else: There
    is only one statement to be executed if the condition is evaluated as true. The case in which the condition is evaluated as false need not necessarily be contemplated.
    Without else branch:
if condizione then
	istruzione-se-condizione-vera;
  • Con ramo else:
if condizione then
	istruzione-se-condizione-vera;
else
	istruzione-se-condizione-falsa;
  • If with multiple statements, with and without branch else: There is only one statement to be executed if the condition is evaluated as true. The case in which the condition is evaluated as false need not necessarily be contemplated.
    With else branch:
if condizione then do;
	istruzione-1-se-condizione-vera;
	istruzione-2-se-condizione-vera;
	...
	istruzione-n-se-condizione-vera;
end;
else do;
	istruzione-1-se-condizione-falsa;
	istruzione-2-se-condizione-falsa;
	...
	istruzione-n-se-condizione-falsa;
end;
  • Without branch else:
if condizione then do;
	istruzione-1-se-condizione-vera;
	istruzione-2-se-condizione-vera;
	...
	istruzione-n-se-condizione-vera;
end;

Conditional statements: A practical example

Now let’s see an example of how to use conditional statements. Let’s start from the example introduced in the previous article and create a dataset again by running the following code

data people_registry;
	input id name $ surname $ sex $ age weight height;
	datalines;
1 Julia Smith F 29 56 172
2 Mark Ronson M 47 78 182
3 Patrick Lane M 33 69 177
4 Allie White F 45 62 189
5 Owen Peterson M 39 71 176
;
run;

We now use conditional statements to create a binary variable that indicates whether the subject is male (assuming the value 1) or female (assuming the value 0). These variables are also referred to as dummies.

data new_people_registry;
	set people_registry;
	if sex = 'M' then is_male = 1;
	else is_male = 0;
run;

Thanks to this data step we are able to create a new dataset called new_people_registry containing the new variable is_male.

Let’s now print the content of the dataset thus obtained on the screen using the proc print function.

This function allows you to print the contents of the dataset on the screen. Here is the code needed to generate the output:

proc print data=new_people_registry;
run;

This is the output generated on the screen:

The SAS System
Ossidnamesurnamesexageweightheightis_male
11JuliaSmithF29561720
22MarkRonsonM47781821
33PatrickLaneM33691771
44AllieWhiteF45621890
55OwenPetersonM39711761

The output confirms that the created variable behaves as expected.

Index

To make it easier for you to read this guide, at the end of each article there is an index including all the topics covered.

  1. Introduction to Base SAS
  2. Conditional statements in Base SAS

Posted

in

by