Combining SAS Data Sets by Adding Variables - SAS Programming

In the previous examples you combined two data sets by adding the obser vations of one to the observations of the other,typically with both data sets having the same set of variables to deal with,or at least partially the same. It's sort of like visually stacking one data set on top of another and lining up the columns.Now let's change the scenario a bit.Let's combine two data sets by adding the variables of one to the variables of the other, typically with both data sets having the same set of observations,or once again,at least partially the same.

This time it's like placing one data set next to the other.Think of it like adding the variables (columns) from one data set on to the variables of the other.We start out with perhaps the simplest merge of all.You have two data sets LEFT and RIGHT.Data set LEFT contains variables ID, HEIGHT,and WEIGHT. Data set RIGHT contains variables GENDER and RACE.

Combining SAS Data Sets by Adding Variables

Both data sets contain three observations, as follows: You can combine the variables from these two data sets into a single data set simply by using the MERGE statement as follows:

Example

DATA AMBIDEX;
MERGE LEFT RIGHT;
RUN;
PROC PRINT DATA*AMBIDEX;
TITLE 'Simple One-to-one Merge';
RUN;

This code merges the variables from data set LEFT with those from data set RIGHT,one observation at a time,in the order that the observations occur in the two data sets.The resultant data set,AMBIDEX, is a "one-to-one" merge of LEFT and RIGHT.The output,as you might have guessed, looks like this:

Output from Example- Combining SAS Data Sets by Adding Variables

Output from Example- Combining SAS Data Sets by Adding Variables

This type of merge (one-to-one)can get you into big trouble and is rarely used (notice we did not say "never" — one should never say never!) It is just too "dumb" to be of much use in most real world situations.

The typical need when merging data sets together is to align or match the records or observations on a matching variable,such as ID number.The obvious problem with a one-to-one match is that the process will merge records side by side,regardless if they match up or not on any matching variable.

If the two data sets do not have the exact same set of observations,in the exact same order, the final merged data set will contain mismatched obser vations.Of course,if the data sets are properly aligned,the final merged data set will be accurate.This may well be the case, but it's a bad habit to rely on it being that way.So what's the answer? Read on


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics