You already know how to vertically combine two SAS data sets using PROC APPEND.
In this video, you’ll learn how to vertically combine two or more SAS data sets by concatenating them in a DATA step.
Concatenating copies all the observations from the first data set,
and then copies all the observations from one or more additional data sets into a new data set.
The original data sets are unchanged.
Here’s a situation in which you would concatenate data sets.
The Trial_CHI data set contains data about participants recruited at the Chicago site.
Another data set named Trial_FW contains data about all participants recruited at the Fort Worth site.
You want to create a new data set called Trial_Multi that contains a copy of the observations in
Trial_CHI followed by a copy of the observations in Trial_FW.
To create this new data set, you concatenate the two original data sets.
To Concatenate two data sets, you will use a DATA step, which is already familiar to you, with a small change to the SET statement.
Remember that the SET statement tells SAS where to get the input data.
Up to now we’ve only ever invluded one input data set in our SET statement, but we can include multiple data sets in the SET statement.
When we do, SAS concatenates them into a single new data set.
The order in which they appear in the new data set will be the same as the order in which you have them listed in the SET statement.
Here’s a quick demonstration of concatenating data sets in SAS.
Here you are creating two SAS data set with identical variables.
The first data set contains your advisees that are in the MPH program, and the second data set contains your advisees in the DrPH program.
Below is the DATA step you will use to concatenate the two data sets.
What do you think will be the value for first name in the first observation of the data set advisees?
You know that SAS will add observations to in the new data set advisees in the same order that they occur in the SET statement.
Therefore, Alison will be the value for the variable first name in the first observation of advisees.
When you check the log you see that 3 observations were read from advisees_MPH
and 2 observations were read from advisees_DrPH,
and the data set advisees was created with 5 observations and 3 variables.
The list report confirms that the concatenation occurred as you intended.
The three MPH students are list first, followed by the two students in the DrPH program.
Now look at this next example.
Do you notice anything different about the data set advisees_MHA?
Advisees_MHA still has three variables, but the third variable is now called degree instead of program.
However, you know that both variables capture the same information and have the same attributes.
In the DATA step below you want to concatenate advisees_MPH and advisees_MHA to make a new data set named advisees_Masters.
Just like before you will type the name of both data sets in the SET statement.
How many variables do you think will be in the data set advisees_Masters after you submit this program?
This time, when you view the list report you see that advisees_Masters has 4 variables:
first, gender, program, and degree.
Alison, Ming, and Brian all have missing values for the variable degree, and Jessica and Ryan have missing values for the variable program.
Is this what you intended the data set to look like?
No, of course not, so why did this happen?
During compilation, SAS identifies three variables in the first data set – advisees_MPH.
SAS then creates the PDV with slots for the three variables: first, gender, and program.
In the second data set, advisees_MHA, SAS finds a new variable, degree, and adds it to the PDV as well.
Then SAS creates the descriptor portion of the new data set advisees_Masters, and starts the execution phase.
At execution, SAS starts by reading the first observation in advisee_MPH.
SAS reads the values of first, gender and program into the PDV.
But, the observations for advisees_MPH have no value for degree, so SAS assigns them a value of missing.
Then SAS writes the contents of the PDV out to the new data set.
SAS repeats this process once for each observation in the two data sets one at a time until all observations are read in,
and the output data set is complete.
So, how would you create the output data set with only the fist three variables (first, gender, and program),
and the values for degree in the last two observations moved over to the program variable?
To do this you start by using the RENAME data set option shown here.
Notice that you enclose the rename equals option within an outer set of parentheses.
Then inside an inner set of parentheses you specify one or more variables that you want to rename.
If the RENAME equals option is associated with an input data set in the SET statement,
as it is here, the rename applies to the data that is being read.
In your program, you want to rename a variable in the advisees_MHA data set from degree to program.
The RENAME equals option does not rename variables in the input data sets.
Instead, the RENAME equals option tells SAS which slot in the PDV to use when building observations.
And now you see that the data set advisees_Masters includes just three variables,
and the values of degree for Jessica and Ryan are now in the program variable.
Now you can vertically combine SAS data sets using PROC APPEND.
You can also concatenate two or more dYou can also concatenate two or more data sets in a DATA step.
In the next video, you'll learn one final method for vertically combining SAS data sets.