Combining SAS Data Sets by Adding Observations - SAS Programming

In this example,we show you how to add observations from one SAS data set to another SAS data set.This is a frequently encountered need.For example,you may have a data set for each year of a study and you want to analyze all years together.For this example,suppose you have two permanent SAS data sets, SURVEY 1 and SURVEY2,both stored in a SAS data library in sub directory;C:DATASURVEYS, and both containing thefollowing variables:ID, GENDER, HEIGHT, WEIGHT, YEAR .The contents of these two data sets are as follows:

Data set SURVEY1

ID GENDER HEIGHT WEIGHT YEAR
1 1 68 155 91
7 2 72 205 90
9 1 66 120 93

Data set SURVEY2

ID GENDER HEIGHT WEIGHT YEAR
5 2 63 102 92
8 1 70 250 91

You want a single data set SURV1_2 which contains all the observations from SURVEY 1 and SURVEY2,and this time you want to store it in the SAS data library SURVEYS along with SURVEY1 and SURVEY2.Here is the code:

Example

LIBNAME GEORGE'C:DATASURVEYS';
DATA GEORGE.SURV1_2;
SET GEORGE.SURVEY1 GEORGE.SORVEY2;
RON;
PROC PRINT DATA=GEORGE.SURV1_2;
TITLE 'Combined Data Sets 1 and 2';
RUN;

Note that the libref GEORGE is used to refer to all three permanent SAS data sets.Results of the PROC PRINT are as follows:

Output from Example-Combining SAS Data Sets by Adding Observations

Output from Example-Combining SAS Data Sets by Adding Observations

As you can see,the effect of including multiple data sets in a SET state ment is to combine the data sets in the order in which they are listed in the SET statement.In this case,the observations of SURVEY 1 are followed by those of SURVEY2.

You will find other, more efficient,methods of combining data.Now let's see what happens when the two data sets do not contain identical sets ofvariables. Suppose you have a third data set SURVEY3 which contains ID,GENDER, HEIGHT, YEAR, and IQ (WEIGHT is not included,but a new variable, IQ is in the data set).What happens when you SET this new data set with SURVEY1? First, here are the observations from SURVEY3:

Data set SURVEY3

ID GENDER HEIGHT YEAR IQ
52 63 92 1 20
81 70 91 1 10

The program to combine SURVEY 1 and SURVEYS is almost identical to the previous program, with minor changes:

Example

LIBNAME GEORGE 'C:DATASURVEYS';
DATA GEORGE.SURV1_3;
SET GEORGE,SURVEY1 GEORGE,SURVEY3;
RUN;
PROC PRINT DATA*GEORGE.SURVI_3;
TITLE 'Combined Data Sets 1 and 3';
RUN;
Here is the output:

Output from Example-Combining SAS Data Sets by Adding Observations

Output from Example-Combining SAS Data Sets by Adding Observations

The program performs exactly as you might have expected; it contains all the variables that are contained in SURVEY1 and SURVEYS, with missing values for IQ in the observations that come from SURVEY 1 and missing values for WEIGHT in the observations that come from SURVEYS. If this is not what you expected, it's time to read this section again.


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

SAS Programming Topics