Sample Topic 5: SET Statement

h

SET Statement

The SET statement is used to read data from an existing SAS data set.

Example

The A data set is a very simple data set. It contains two variables: VAR1 and VAR2.

Now, we are going to use the SET statement to read the data from the A data set and write them to another data set.

Example

Data B;
Set A;
Run;

The B data set now contains the identical set of data as A:


Processing the SET statement

Before we learn more complex applications of the SET statement, it is important to understand how the SET statement is processed.

In our example, the SET statement reads the first observation from the A data set into the PDV during the first data step iteration:

There is an implied OUTPUT statement before the RUN statement:

The implied OUTPUT statement writes the data to the B data set:

This concludes the first data step iteration.

SAS continues to read the data from the A data set and write them to the B data set until all of the observations are read.

At the end, the B data set contains the same set of data as A.


Concatenating Data Sets

The SET statement can also be used to concatenate data sets.

Copy and run the code from the yellow box below:

The DEPT1 and DEPT2 data sets each contain a list of jobcodes:

DEPT1
DEPT2

​The two data sets can be concatenated into one:

Data Accounting;
Set dept1 dept2;
Run;

The lists of jobcodes are now combined into one data set:

Note: the JOBCODE variable has different lengths in the DEPT1 and DEPT2 data sets.

The length of JOBCODE is 5 in the DEPT1 data set and 7 in the DEPT2 data set.

In the output data set, the length of JOBCODE will follow the first data set listed in the SET statement. 

In our example, it is the DEPT1 data set.

As a result, the length of JOBCODE in the ACCOUNTING data set is set to 5:

✍️ Exam Tips

When concatenating data sets that have the same variables, the length of the variable will follow the first data set listed in the SET statement.

? Sample Exam Question(s):
Question 1

Get Solution