BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Marjolein
Obsidian | Level 7

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?

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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
D,CANADA
E,SPAIN
F,ITALY
;
run;

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

View solution in original post

2 REPLIES 2
ChrisBrooks
Ammonite | Level 13

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
D,CANADA
E,SPAIN
F,ITALY
;
run;

data datac;
	merge dataa(in=a) datab(in=b);
	by id;
	if a and b;
run;
Marjolein
Obsidian | Level 7

Fantastic, it works! Thanks a lot for your reply!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 3192 views
  • 1 like
  • 2 in conversation