# Different Cases of SAS Concatenate Data Sets with SET Statement

Interactive Online Courses: Elevate Skills & Succeed Enroll Now!

Previously, we studied SAS Functions, now we will move on to a very easy and interesting method to combine two data sets in SAS programming i.e. SAS concatenate data sets.

After the end of this tutorial, you will be able to understand SAS concatenate datasets and different cases of concatenating data sets.

### What is SAS Concatenate Data Sets?

We come across different types of SAS datasets and many times we would want to combine two data sets to have a clear idea about our analysis. The SET statement is used to concatenate two datasets in SAS.

If there are two data sets, say one has 2 observations and the other has 3 observations, then, our SAS concatenate dataset will have 5 observations. The order of observations is sequential. All observations from the first data set are followed by all observations from the second data set, and so on.

Below is the syntax of a SET statement in SAS

`SET data-set 1 data-set 2 data-set 3.....;`

Data-set 1, data-set 2 and data-set 3 are names of the data sets we wish to combine.

For example – suppose the data setÂ store1Â contains three variables,Â storeÂ (number),Â dayÂ (of the week), andÂ salesÂ (in dollars):

StoreÂ  Â  Â  Â  DayÂ  Â  Â  Â  Â  Â Sales
1Â  Â  Â  Â  Â  Â  Â  Â MÂ  Â  Â  Â  Â  Â  Â 1200
1Â  Â  Â  Â  Â  Â  Â  Â TÂ  Â  Â  Â  Â  Â  Â  1435
1Â  Â  Â  Â  Â  Â  Â  Â WÂ  Â  Â  Â  Â  Â  Â 1712
1Â  Â  Â  Â  Â  Â  Â  Â RÂ  Â  Â  Â  Â  Â  Â  1529
1Â  Â  Â  Â  Â  Â  Â  Â FÂ  Â  Â  Â  Â  Â  Â  1920
1Â  Â  Â  Â  Â  Â  Â  Â SÂ  Â  Â  Â  Â  Â  Â  2325

and the data setÂ store2Â contains the same three variables:

StoreÂ  Â  Â  Â  Â DayÂ  Â  Â  Â  Â  Â Sales
2Â  Â  Â  Â  Â  Â  Â  Â  MÂ  Â  Â  Â  Â  Â  2215
2Â  Â  Â  Â  Â  Â  Â  Â  TÂ  Â  Â  Â  Â  Â  Â 2458
2Â  Â  Â  Â  Â  Â  Â  Â  WÂ  Â  Â  Â  Â  Â  1798
2Â  Â  Â  Â  Â  Â  Â  Â  RÂ  Â  Â  Â  Â  Â  Â 1692
2Â  Â  Â  Â  Â  Â  Â  Â  FÂ  Â  Â  Â  Â  Â  Â 2105
2Â  Â  Â  Â  Â  Â  Â  Â  SÂ  Â  Â  Â  Â  Â  Â 2847

For SAS concatenate data sets, you simply specify a list of data set names inÂ oneÂ SET statement.

```DATA store1;
Â Â Â Â Â  input Store Day \$ Sales;
Â Â Â Â Â  DATALINES;
1Â    MÂ    1200
1Â    TÂ Â   1435
1Â    W  Â  1712
1Â Â   RÂ    1529
1Â Â   FÂ Â   1920
1Â Â   SÂ Â   2325
;
RUN;
DATA store2;
Â Â Â Â Â  input Store Day \$ Sales;
Â Â Â Â Â  DATALINES;
2Â    MÂ   2215
2Â    TÂ Â  2458
2Â    WÂ   1798
2Â Â   RÂ   1692
2Â Â   FÂ Â  2105
2Â Â   SÂ Â  2847
;
RUN;
DATA bothstores;
Â Â Â Â  Â set store1 store2;
RUN;
PROC PRINT data = bothstores;
Â Â Â Â Â Â  title â€˜ The bothstores datasetâ€™;
RUN;```

The output of the above program will look like:
StoreÂ  Â  Â  DayÂ  Â  Â  Â  Sales
1Â  Â  Â  Â  Â  Â  Â MÂ  Â  Â  Â  Â  Â 1200
1Â  Â  Â  Â  Â  Â  Â TÂ  Â  Â  Â  Â  Â  Â 1435
1Â  Â  Â  Â  Â  Â  Â WÂ  Â  Â  Â  Â  Â  1712
1Â  Â  Â  Â  Â  Â  Â RÂ  Â  Â  Â  Â  Â  Â 1529
1Â  Â  Â  Â  Â  Â  Â FÂ  Â  Â  Â  Â  Â  Â 1920
1Â  Â  Â  Â  Â  Â  Â SÂ  Â  Â  Â  Â  Â  Â 2325
2Â  Â  Â  Â  Â  Â  Â MÂ  Â  Â  Â  Â  Â  Â 2215
2Â  Â  Â  Â  Â  Â  Â TÂ  Â  Â  Â  Â  Â  Â 2458
2Â  Â  Â  Â  Â  Â  Â WÂ  Â  Â  Â  Â  Â  1798
2Â  Â  Â  Â  Â  Â  Â RÂ  Â  Â  Â  Â  Â  Â 1692
2Â  Â  Â  Â  Â  Â  Â FÂ  Â  Â  Â  Â  Â  Â  2105
2Â  Â  Â  Â  Â  Â  Â SÂ  Â  Â  Â  Â  Â  Â 2847

## Different Cases of Concatenating Data Sets

In the above example, we saw that both datasets had three variables so it was easy to concatenate them. But, what happens if the variables are different, or of different lengths or of different names? Let us understand this from SAS Concatenate Datasets in differentÂ cases.

### 1. Different Names of Variables

In this case, both the data sets have different names for their variables. So, when we try and concatenate, an error might get displayed.

In order to overcome this, when we concatenate the datasets, we will rename the variables so that their name appears as one in the concatenated data set. Let us look at how we can do this.

Example:

In the below example data set x1 has the variable name x1inc whereas the data set x2 has the variable name x2inc. But both of these variables represent the same type(numeric). We apply the RENAME function in the SET statement as shown below:

```DATA x1;
INPUT empid name \$ x1inc ;
DATALINES;
2 ArtÂ  22000
1 Bill 30000
3 Paul 25000
;
RUN;Â
DATA x2;
INPUT empid name \$ x2inc ;
DATALINES;
1 Bess 15000
3 PatÂ  50000
2 AmyÂ  18000
;
RUN;Â
DATA x3;Â
SET x1(RENAME=(x1inc=inc))x2(RENAME=(x2inc=inc));
RUN;Â
PROC PRINT DATA=x3;
RUN;```

Output-

OBSÂ  Â  Â  Â  Â  Â EMPIDÂ  Â  Â  Â  Â  NAMEÂ  Â  Â  Â  Â  Â  Â INC
1Â  Â  Â  Â  Â  Â  Â  Â  Â 2Â  Â  Â  Â  Â  Â  Â  Â  Â  ArtÂ  Â  Â  Â  Â  Â  Â  22000
2Â  Â  Â  Â  Â  Â  Â  Â  Â 1Â  Â  Â  Â  Â  Â  Â  Â  Â  BillÂ  Â  Â  Â  Â  Â  Â  30000
3Â  Â  Â  Â  Â  Â  Â  Â  Â 3Â  Â  Â  Â  Â  Â  Â  Â  Â  PaulÂ  Â  Â  Â  Â  Â  Â 25000
4Â  Â  Â  Â  Â  Â  Â  Â  Â 1Â  Â  Â  Â  Â  Â  Â  Â  Â  BessÂ  Â  Â  Â  Â  Â  15000
5Â  Â  Â  Â  Â  Â  Â  Â  Â 3Â  Â  Â  Â  Â  Â  Â  Â  Â  PatÂ  Â  Â  Â  Â  Â  Â  50000
6Â  Â  Â  Â  Â  Â  Â  Â  Â 2Â  Â  Â  Â  Â  Â  Â  Â  Â  AmyÂ  Â  Â  Â  Â  Â  18000

### 2. Different Variable Attributes

We talked about variable attributes in earlier classes which are a type, length, label, etc. Now, what happens if these differ. Again, while combining datasets, we can specify the length in our SET statement, that we want to keep or the data type of the variables.

Let us look at an example:

In the below example, the variable name is of length 3 in the first data set and 4 in the second. When concatenating, we apply the LENGTH statement inÂ SAS concatenate data set to set the name length to 7 or the sum of two lengths.

```DATA x1; Â Â
INPUT empid name \$3. inc;
DATALINES;Â  Â
2 ArtÂ  22000 Â
1 BobÂ  30000 Â
3 TomÂ  25000 Â
RUN;
DATA x2;Â Â
INPUT empid name \$4. inc;
DATALINES;Â  Â
1 Bess 15000Â  Â
3 Rory 50000Â  Â
2 Jane 18000Â
RUN;Â  Â
DATA x3; Â
SET x1 x2;
RUN;
PROC PRINT DATA=x1andx2;
RUN;```

Output-
OBSÂ  Â  Â  Â  empIDÂ  Â  Â  Â  Â NAMEÂ  Â  Â  Â  Â  Â INC
1Â  Â  Â  Â  Â  Â  Â  2Â  Â  Â  Â  Â  Â  Â  Â  ArtÂ  Â  Â  Â  Â  Â  Â 22000
2Â  Â  Â  Â  Â  Â  Â  1Â  Â  Â  Â  Â  Â  Â  Â  BobÂ  Â  Â  Â  Â  Â  30000
3Â  Â  Â  Â  Â  Â  Â  3Â  Â  Â  Â  Â  Â  Â  Â  TomÂ  Â  Â  Â  Â  Â  25000
4Â  Â  Â  Â  Â  Â  Â  1Â  Â  Â  Â  Â  Â  Â  Â  BesÂ  Â  Â  Â  Â  Â  15000
5Â  Â  Â  Â  Â  Â  Â  3Â  Â  Â  Â  Â  Â  Â  Â  RorÂ  Â  Â  Â  Â  Â  50000
6Â  Â  Â  Â  Â  Â  Â  2Â  Â  Â  Â  Â  Â  Â  Â  JanÂ  Â  Â  Â  Â  Â  18000

### 3. Different Number of Variables

If one of the two datasets has more number of variables than the other, then the data set which has less number of variables shows missing values in place of that extra variable.

## Summary

Today we learned what is concatenation in SAS, how SAS concatenate data sets in SAS programming. We also discussed different cases of concatenating data sets in SAS.

Hope you find the explanation helpful. If you have any queries, feel free to ask in the comment section.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google