16.5 - Selecting Variables
It is often the case that input data sets contain many more variables than you want in your final merged data set. Just as you can use the DROP= and KEEP= DATA step options to drop or keep a subset of variables when reading SAS data sets, you can use the DROP= and KEEP= options to drop or keep a subset of variables when merging SAS data sets. Let's take a look at an example!
In each case, the PRINT procedure's OBS=5 option tells SAS to print just the first five observations of each data set. The second PRINT procedure contains an extra direction to SAS to print only those observations for which the v_type variable equals 0. Before you can run the program, you'll need to right-click the data set links... back, purg, and fhx... so that you can save the data sets to a convenient location on your computer. After doing so, launch the SAS program, and edit the LIBNAME statement so that it reflects the location in which you saved the data sets. Then, run the program, and review the output so that you can familiarize yourself with each data set.
As you can see, the data sets share three variables — subj, v_type, and v_date, and then each data set has its own set of unique variables. Our interest in this example is to merge the three data sets into a new data set called icdbdata that contains the common subj variable, the b_date and sex variables from the back data set, the purg_1 and purg_2 variables from the purg data set, and the fhx_1 and fhx_2 variables from the fhx data set. That is, when all is said and done, this is what the first ten observations of the icdbdata set should look like:
The following code does the trick for us:
At a global level, the DATA step tells SAS to merge the (permanent) back, purg, and fhx data sets by the subj variable. The data set options give SAS more specific directions about what to do while match-merging the data sets. Specifically:
- The KEEP= option attached to the icdb.back data set tells SAS to keep just three variables — subj, b_date, and sex — from the icdb.back data set.
- The WHERE= option attached to the icdb.purg data set tells SAS to select the observations for which the v_type variable equals 0.
- The KEEP= option attached to the icdb.purg data set tells SAS to keep four variables — subj, v_type, purg_1, and purg_2 — from the icdb.purg data set. Although we don't want the v_type variable in the final icdbdata data set, we still need to tell SAS to keep the variable just so we can use it to select the observations for which v_type equals 0.
- The KEEP= option attached to the icdb.fhx data set tells SAS to keep just three variables — subj, fhx_1, and fhx_2 — from the icdb.purg data set.
- Finally, the DROP= option attached to the icdbdata output data set tells SAS to drop the v_type variable from the output data set.
Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that the icdbdata data set contains only the variables and observations that we desired.