Published on :
Data Manipulation CREATION_INTERNE

Examples: Interleaving Data

This code is also available in: Deutsch Español Français
Awaiting validation
Interleaving SAS© datasets is a technique that allows combining observations from multiple datasets into a single one, respecting the order defined by one or more common variables specified in the BY statement. To ensure correct interleaving, it is imperative that all input datasets are pre-sorted (using PROC SORT) or indexed according to the same BY variable or variables. The process copies observations from the original datasets into the output dataset, maintaining the sequential order of the BY variable values. If BY variable values are duplicated between input datasets, the order in which the datasets are listed in the SET statement will determine the order of the corresponding observations in the output. If an input dataset contains variables not present in the others, the corresponding values in the output dataset will be null. The total number of observations in the output dataset corresponds to the sum of observations from all input datasets.
Data Analysis

Type : CREATION_INTERNE


The examples use generated data (datalines) to create the SAS datasets necessary to demonstrate interleaving.

1 Code Block
DATA STEP / PROC SORT Data
Explanation :
This example creates two datasets, 'animal' and 'plant', and sorts them by the common variable 'common'. Then, the DATA step interleaves these datasets using the BY statement. The output dataset 'interleave' contains observations from both datasets, ordered by 'common'.
Copied!
1DATA animal;
2 INPUT common $ animal $;
3 DATALINES;
4a Ant
5b Bird
6c Cat
7d Dog
8e Eagle
9f Frog
10;
11RUN;
12 
13DATA plant;
14 INPUT common $ plant $;
15 DATALINES;
16a Apple
17b Banana
18c Coconut
19d Dewberry
20e Eggplant
21f Fig
22;
23RUN;
24 
25PROC SORT DATA=animal; BY common; RUN;
26PROC SORT DATA=plant; BY common; RUN;
27 
28DATA interleave;
29 SET animal plant;
30 BY common;
31RUN;
32PROC PRINT DATA=interleave; RUN;
2 Code Block
DATA STEP / PROC SORT Data
Explanation :
This example demonstrates interleaving datasets ('animalDupes' and 'plantDupes') that contain duplicate values for the BY variable 'common'. The datasets are sorted before interleaving. The order of the datasets in the SET statement influences the order of observations with the same 'common' values in the output dataset. An additional example with the order 'plantDupes animalDupes' is provided to illustrate this impact.
Copied!
1DATA animalDupes;
2 INPUT common $ animal $;
3 DATALINES;
4a Ant
5a Ape
6b Bird
7c Cat
8d Dog
9e Eagle
10;
11RUN;
12 
13DATA plantDupes;
14 INPUT common $ plant $;
15 DATALINES;
16a Apple
17b Banana
18c Coconut
19c Celery
20d Dewberry
21e Eggplant
22;
23RUN;
24 
25PROC SORT DATA=animalDupes; BY common; RUN;
26PROC SORT DATA=plantDupes; BY common; RUN;
27 
28DATA interleave;
29 SET animalDupes plantDupes;
30 BY common;
31RUN;
32 
33PROC PRINT DATA=interleave; RUN;
3 Code Block
DATA STEP / PROC SORT Data
Explanation :
This example illustrates interleaving datasets ('animalDupes' and 'plantMissing2') where the BY variable 'common' contains values present in one dataset but not in the other (e.g., 'd' in 'animalDupes' and 'f' in 'plantMissing2'). After sorting, the DATA step interleaves the datasets. Variables not present in a specific observation of an input dataset receive missing values in the output dataset.
Copied!
1DATA animalDupes;
2 INPUT common $ animal $;
3 DATALINES;
4a Ant
5a Ape
6b Bird
7c Cat
8d Dog
9e Eagle
10;
11RUN;
12 
13DATA plantMissing2;
14 INPUT common $ plant $;
15 DATALINES;
16a Apple
17b Banana
18c Coconut
19e Eggplant
20f Fig
21;
22RUN;
23 
24PROC SORT DATA=animalDupes; BY common; RUN;
25PROC SORT DATA=plantMissing2; BY common; RUN;
26 
27DATA interleave;
28 SET animalDupes plantMissing2;
29 BY common;
30RUN;
31 
32PROC PRINT DATA=interleave; RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved