Tuesday, April 12, 2011

MERGE Command

When you want to match observations from one dataset with observations from another, use the MERGE statement in the DATA step.

Merging SAS data set is a simple process. First, if the data are not already sorted, use the SORT procedure to sort all data sets by the common variables. Then, in the DATA statement, name the new SAS data set to hold the results and follow with a MERGE statement listing the data sets to be combined . use a BY statement to indicate the common variables. For an example,

We have 2 datasets a and b.

First dataset -a. It contains

Name score1

Anu 34

Raju 38

Ravi 56

Anna 21

Second dataset –b it contains

Name score2

Anu 35

Raju 39

Ravi 58

Anna 25



I need a data set with name, score1 and score2 .

data mylib.dataone;

input name$ score1;

datalines;

Anu 34

Raju 38

Anna 56

Ann 21

;

run;

data mylib.datatwo;

input name$ score2;

datalines;

Anu 35

Raju 39

Anna 57

Ann 28

;

run;

proc sort data=mylib.dataone;

by name;

run;

proc sort data=mylib.datatwo;

by name;

run;

data mylib.data;

merge mylib.dataone mylib.datatwo;

by name;

run;

proc print data=mylib.data;

run;

No comments:

Post a Comment