Solved
Contributor
Posts: 27

# Merging datasets with unequal observations

[ Edited ]

Hi,

I'm struggling combining datasets with unequal observations in SAS. I have dataset A with 8000 observations (containing variables X and Y), and dataset B (containing variable Z) with 9000 observations. Both datasets have a common identifier.

I want to have dataset A, and pull variable Z from dataset B to dataset A (so the dataset will have 8000 obs, with the added variable from dataset B). Does anyone know how to do so?

Accepted Solutions
Solution
‎05-07-2018 06:51 AM
Valued Guide
Posts: 592

## Re: Merging datasets with unequal observations

To merge data sets so that only common id values in both data sets are included in the output you can use the in= data set option which has the value of 1 if the data set contributes to the current observation or 0 if it doesn't. Here is an example

``````data dataa;
length id \$1 x \$5 y \$6;
infile datalines dlm=",";
input id x y;
datalines;
A,CAT,BLUE
B,DOG,GREEN
C,BIRD,RED
D,FISH,YELLOW
;
run;

data datab;
length id \$1 z \$7;
infile datalines dlm=",";
input id z;
datalines;
A,USA
B,UK
C,GERMANY
E,SPAIN
F,ITALY
;
run;

data datac;
merge dataa(in=a) datab(in=b);
by id;
if a and b;
run;``````

All Replies
Solution
‎05-07-2018 06:51 AM
Valued Guide
Posts: 592

## Re: Merging datasets with unequal observations

To merge data sets so that only common id values in both data sets are included in the output you can use the in= data set option which has the value of 1 if the data set contributes to the current observation or 0 if it doesn't. Here is an example

``````data dataa;
length id \$1 x \$5 y \$6;
infile datalines dlm=",";
input id x y;
datalines;
A,CAT,BLUE
B,DOG,GREEN
C,BIRD,RED
D,FISH,YELLOW
;
run;

data datab;
length id \$1 z \$7;
infile datalines dlm=",";
input id z;
datalines;
A,USA
B,UK
C,GERMANY
E,SPAIN
F,ITALY
;
run;

data datac;
merge dataa(in=a) datab(in=b);
by id;
if a and b;
run;``````
Contributor
Posts: 27