Merging SAS Data Sets |
Performing a Basic
Match-Merge |
After all input data sets are sorted or indexed by the value of a BY variable, you can merge the data sets using a DATA step that contains MERGE and BY statements. |
General form, basic DATA step for
match-merging:
DATA output-SAS-data-set; where
|
For example, suppose you have sorted the data sets
Clinic.Demog and Clinic.Visit as
follows.
proc sort data=clinic.demog; by id; run;
proc sort data=clinic.visit; by id; run;
data clinic.combined; merge clinic.demog clinic.visit; by id; run; proc print data=clinic.combined; run; Notice that all observations, including unmatched observations and observations with missing data, are written to the output data set. |
Obs | ID | Age | Sex | Date | Visit | SysBP | DiasBP | Weight |
1 | A001 | 21 | m | 11/05/98 | 1 | 140 | 85 | 195 |
2 | A001 | 21 | m | 10/13/98 | 2 | 138 | 90 | 198 |
3 | A001 | 21 | m | 07/04/98 | 3 | 145 | 95 | 200 |
4 | A002 | 32 | m | 04/14/98 | 1 | 121 | 75 | 168 |
5 | A003 | 24 | f | 08/12/98 | 1 | 118 | 68 | 125 |
6 | A003 | 24 | f | 08/21/98 | 2 | 112 | 65 | 123 |
7 | A004 | . | 03/30/98 | 1 | 143 | 86 | 204 | |
8 | A005 | 44 | f | 02/27/98 | 1 | 132 | 76 | 174 |
9 | A005 | 44 | f | 07/11/98 | 2 | 132 | 78 | 175 |
10 | A005 | 44 | f | 04/16/98 | 3 | 134 | 78 | 176 |
11 | A007 | 39 | m | 11/11/57 | . | . | . | |
12 | A008 | . | 05/22/98 | 1 | 126 | 80 | 182 |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.