In SAS, you can set multiple datasets in a single data step. If you have multiple datasets in a set statement, you will combine the datasets vertically.
data new; set dataset1 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.
In SAS, you can “set” multiple datasets in a single data step. If you have multiple datasets in a set statement, you will combine the datasets vertically.
“Setting” datasets stacks the given datasets vertically and creates 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 “set” 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
Using SET with Mutiple 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
Hopefully this article has been useful for you to learn what happens if you use set with multiple datasets in SAS.