You can use the following basic syntax to include multiple datasets in the set statement in SAS:
data new_data; set data1 data2 data3; run;
The following example shows how to use this syntax in practice.
Example: Use SET Statement with Multiple Datasets in SAS
Suppose we have the following dataset in SAS that shows the points scored by various basketball players on a team called A:
/*create first dataset*/ data data1; input team $ points; datalines; A 12 A 15 A 16 A 21 A 22 ; run; /*view dataset*/ proc print data=data1;
And suppose we have another dataset that shows the points scored by various basketball players on a team called B:
/*create second dataset*/ data data2; input team $ points; datalines; B 16 B 22 B 25 B 29 B 30 ; run; /*view dataset*/ proc print data=data2;
We can use the set statement with multiple datasets to combine these two datasets into one:
/*create new dataset that combines two datasets*/ data data3; set data1 data2; run; /*view new dataset*/ proc print data=data3;
The result is a third dataset called data3 that combines the rows from data1 and data2.
Note: Even if the two datasets didn’t share the same column names, the set statement would still combine the datasets into one and simply leave empty spaces in the cells where the columns don’t match.
The following tutorials explain how to perform other common tasks in SAS:
How to Delete Datasets in SAS
How to Add Row Numbers in SAS
How to Select the First N Rows of a Dataset in SAS