Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
Advanced Data Manipulation [2-6]


In the data manipulation module, we have learned how to concatenate and merge data sets.

​​There is also another way to combine data sets.

​Let's look at an example.

Interleaving Data Sets
Picture

When interleaving data sets, you are inserting the observations from each data set in the order of the by-group variable.

Let's look at an example.

The code above creates two data sets:
  • INDS1
  • INDS2

Each data set contains two columns and three observations:
​
The INDS1 data set contains ID 101, 103 and 105:
Picture

The INDS2 data set contains ID 102, 104 and 106:
Picture

You can interleave these two data sets by using the SET statement and the BY statement.

Example
data inds;
set inds1 inds2;
by ID;
run;

This is a very simple data step with a SET statement on the INDS1 and INDS2 data sets.

There is also a BY statement on the ID variable:
Picture

When executing the step, SAS inserts the observations into the output data set based on the order of the ID column.
​
In the first data step iteration, SAS inserts the observation with ID 101:
Picture

In the second data step iteration, SAS inserts the observation from the next by-group (i.e. ID 102).
Picture

SAS continues until all of the observations are inserted into the output data set:
Picture

Programming Tips:
  1. Please note that you can achieve the exact same results by concatenating (SET Statement) and sorting the data sets (Proc Sort). However, doing so is less efficient, especially when you are working with some large data sets.
  2. Since this technique involves a BY statement in the data step, the input data sets must be sorted prior to interleaving them.

Exercise

Copy and run the code from the yellow box below:

The code above creates two data sets:
  • MALE and
  • FEMALE

Each data set contains a list of students.

Interleave these two data sets using the NAME variable as the by-group.
Next

Need some help? 


HINT:
You must first sort the data sets prior to interleaving them.


SOLUTION:

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

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


Fill out my online form.
Already a member? Go to member's area.