Question: I cannot understand the difference between `interleaving` and `concatenation`

Interleaving 

```
proc sort data=ds1 
           out=ds1;
   by var1;
run;
proc sort data=ds2 
           out=ds2;
   by var1;
run;
 data testInterleaving ; 
 set ds1 ds2 ;
 run ;

```

Concatenation 

```
data testConcatenation; 
   set ds1 ds2;
run;

```

I tested these and the resulting datasets were exactly the same except for the order of observations which I think does not really matter. The two resulting datasets contain exactly the same observations. So, what is the difference, except for order?

Answer: 
Interleaving, as CarolinaJay notes, is combining `SET` with `BY`. It is not merging, and it is not just sorting prior to setting.

For example, let's create a pair of datasets, the female and the male members of `sashelp.class`.

```
data male female;
set sashelp.class;
if sex='F' then output female;
else output male;
run;

proc sort data=male;
by name;
run;
proc sort data=female;
by name;
run;

data concatenated;
set male female;
run;

data interleaved;
set male female;
by name;
run;

```

Now, look at the datasets. `Concatenated` is all of the males, then all of the females - it processes the `set` statements in order, exhausting the first before moving onto the second.

`Interleaved` is in name order, not in order by sex. That's because it traverses the two (in this case) `set` datasets by name, keeping track of where it is in the `name` ordering. You can add debugging statements (Either use the data step debugger, or add a `put _all_;` to the datastep) to see how it works.
The data steps at the end are the exact same. You are performing the same code, it doesn't matter if you sort before hand.

What I think you mean in the interleaving is

```
data testInterleaving ;
MERGE ds1 ds2;
by var1;
run;

```

The `set` statement reads sequentially through the data sets in the order you list them. The `merge` statement compares records between the sets and puts them into the output in the order of the variable(s) in the `by` statement. I recommend looking at the SAS documentation on the `merge` statement as this is a very simplistic explanation for a very powerful tool.