Interleaving SAS datasets (Section 5.3)

In some situations, you may want to combine datasets and, at the same time, preserve some sort of order. This is often called interleaving. Often this is the case when the datasets you want to combine are already sorted by one or more important variables.

To interleave observations of more than one datasets, first make sure that the datasets are sorted by the same variables. Then use a BY statement along with the SET statement to combine the datasets.

Syntax

DATA combined_dataset;
  SET data1 data2 data3 ... ;
  BY  byvar1 ...;

RUN;
Example:
data1:	
Class	Name	Score
 A	John	 75  
 A	Jill	 80 
 B	Paul 	 98
 C	Andrea	 86

data2:	
Class	Name	Score
 A	Chris	 58  
 B	Cathy	 92 
 B	Amy	 88
 C	Don	 68 
 
DATA combine;
  SET data1 data2;
  BY class;
RUN;

combine:	
Class	Name	Score
 A	John	 75  
 A	Jill	 80 
 A	Chris	 58  
 B	Paul	 98
 B	Cathy	 92
 B	Amy	 88
 C	Andrea	 86
 C	Don	 68 

Note that the new dataset combine is still sorted by Class. But within each unique Class value, the 2nd dataset is again simply stacked beneath the first.
When there are different variables between the datasets, missing values occur like in the preceeding section on stacking datasets. But all datasets to be combined MUST contain the same BYVARS as listed on the BY statement.