To combine datasets vertically in SAS, the easiest way is to “set” the datasets in a SAS data step.

data new;
    set dataset1 dataset2;
run;

You can also use PROC APPEND to append a dataset at the end of another dataset.

proc append base=dataset1 data=dataset2;
run;

When working with data in SAS, the ability to create new datasets is valuable. Various operations, such as merging and appending, allow us to create new datasets from existing datasets.

The easiest way to combine datasets vertically is by “setting” datasets with the set statement.

“Setting” datasets stacks the given datasets vertically and allows you to create a new dataset.

With this method, you can stack as many datasets as you want and append many datasets on top of one another.

Below is a simple example of how you can append two SAS datasets in a SAS Data Step.

data dataset1;
	input num;
	datalines;
4
1
5
;
run;

data dataset2;
	input num;
	datalines;
1
6
3
;
run;

data new;
    set dataset1 dataset2;
run;

The resulting dataset “new” is shown below.

num
  4	
  1	
  5	
  1	
  6	
  3

Appending SAS Datasets in Data Step with Different Variables and Data Types

When you go to combine multiple SAS datasets in a SAS Data Step and you have different variables, there are a few different things to understand.

First, if you have different columns, then in the newly created dataset you will have missing values for the records where the column didn’t exist in the input dataset.

Second, if you have columns with the same name and different data types, then you will get an error.

Below is an example of the output in SAS of combining datasets with “set” when you have different variables.

data dataset1;
	input num1;
	datalines;
4
1
5
;
run;

data dataset2;
	input num2;
	datalines;
1
6
3
;
run;

data new;
    set dataset1 dataset2;
run;

The resulting dataset is shown below.

num1  num2
   4     .	
   1     .	
   5     .	
   .     1	
   .     6	
   .     3

Using PROC APPEND to Combine Datasets Vertically in SAS

You can also use PROC APPEND to combine two datasets and append one dataset to another.

To use PROC APPEND, you pass two arguments. The first argument is the dataset you want to append to, and the second argument is the dataset you want to append.

With PROC APPEND, you don’t create a new dataset. Instead, you are appending the second dataset to the base dataset.

Below is an example of how you can append datasets with PROC APPEND in SAS using the same datasets as the first example from above.

proc append base=dataset1 data=dataset2;
run;

The resulting dataset “dataset1” is shown below.

num
  4	
  1	
  5	
  1	
  6	
  3

One other consideration is that with PROC APPEND, by default, the variables MUST be the same. If the variables in the two datasets aren’t the same, you can pass FORCE to force PROC APPEND to append the datasets.

proc append base=dataset1 data=dataset2 force;
run;

Hopefully this article has been useful for you to learn how to combine multiple datasets vertically in SAS.

Categorized in:

SAS,

Last Update: March 13, 2024