How to Concatenate Datasets in SAS (With Example)


You can use the following basic syntax to concatenate datasets in SAS:

/*concatenate two datasets into one*/
data data3;
	set data1 data2;
run;

The following example shows how to use this syntax in practice.

Example: Concatenate Datasets in SAS

Suppose we have the following two datasets in SAS:

/*create first dataset*/
data data1;
    input firstName $ lastName $ points;
    datalines;
Austin Smith 15
Brad Stevens 31
Chad Miller 22
;
run;

/*create second dataset*/
data data2;
    input firstName $ lastName $ points;
    datalines;
Dave Michaelson 19
Eric Schmidt 29
Frank Wright 20
Greg Gunner 40
Harold Anderson 35
;
run;

/*view datasets*/
proc print data=data1;
proc print data=data2;

SAS concatenate datasets

We can use the following code to concatenate these two datasets into one dataset:

/*concatenate two datasets into one*/
data data3;
	set data1 data2;
run;

/*view new dataset*/
proc print data=data3;

The resulting dataset contains all of the observations from the first two datasets.

Note: In this example we concatenated only two datasets into one. However, we can use similar syntax to concatenate as many datasets as we’d like. The only requirement is that each dataset contains the same variable names.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Normalize Data in SAS
How to Remove Duplicates in SAS
How to Concatenate Strings in SAS
How to Replace Missing Values with Zero in SAS

Leave a Reply

Your email address will not be published.