SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

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;
      MERGE SAS-data-set-1 SAS-data-set-2;
      BY variable(s);
RUN;

where

  • output-SAS-data-set names the data set to be created
  • SAS-data-set-1 and SAS-data-set-2 are input data sets
  • variable(s) in the BY statement specifies the variable(s) whose values are used to match observations.
NOTE: You can specify any number of input data sets in the MERGE statement.


For example, suppose you have sorted the data sets Clinic.Demog and Clinic.Visit as follows.
     proc sort data=clinic.demog;
        by id;
     run;


Obs ID Age Sex Date
1 A001 21 m 05/22/75
2 A002 32 m 06/15/63
3 A003 24 f 08/17/72
4 A004 .   03/27/69
5 A005 44 f 02/24/52
6 A007 39 m 11/11/57


     proc sort data=clinic.visit;
        by id;
     run;

Obs ID Visit SysBP DiasBP Weight Date
1 A001 1 140 85 195 11/05/98
2 A001 2 138 90 198 10/13/98
3 A001 3 145 95 200 07/04/98
4 A002 1 121 75 168 04/14/98
5 A003 1 118 68 125 08/12/98
6 A003 2 112 65 123 08/21/98
7 A004 1 143 86 204 03/30/98
8 A005 1 132 76 174 02/27/98
9 A005 2 132 78 175 07/11/98
10 A005 3 134 78 176 04/16/98
11 A008 1 126 80 182 05/22/98


You can then submit this DATA step to create Clinic.Combined by merging Clinic.Demog and Clinic.Visit according to values of the variable ID.

     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



back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement