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.
SAS defines [INTERLEAVING](https://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/viewer.htm#a001318366.htm) as using a BY statement with a SET statement. The included link shows two data sets, sorted by the same variable(s), generating one data set using a BY statement with a SET statement.