Contributor
Posts: 20

# Need your help in Merging Question

##### Need your help on below question-

There are two datasets TEMP and TEMP2
TEMP

Obs A B C D

1 1 4 3 3
2 2 4 3 2
3 3 3 3 1
4 4 3 3 2
5 5 2 4 2
6 6 2 4 1
7 7 1 4 3
8 8 1 4 1

TEMP2

Obs A B C D

1 9 4 3 1
2 3 6 8 2
3 2 3 3 1
4 1 7 7 2
Question-.Write some data step code to merge the datasets TEMP and TEMP2 by variable A, just keeping the values of A that exist in dataset TEMP2, and keeping the values of B, C and D from dataset TEMP.

Super Contributor
Posts: 334

## Re: Need your help in Merging Question

Posted in reply to Sarvendra

Im starting to think this is someone homework assignment ....:smileyshocked:

Posts: 5,543

## Re: Need your help in Merging Question

Posted in reply to Sarvendra

Here is to you, your classmates and your teacher!

proc sort data=temp; by a; run;
proc sort data=temp2; by a; run;

data want;
merge temp2(in=in2) temp(in=in1);
by a;
if in2 and in1;
run;

proc print data=want noobs; run;

PG

PG
Contributor
Posts: 20

## Re: Need your help in Merging Question

Thanks PG for the reply

but one question that you code will be containing values of A that are in Both Datasets as u mentioned the code line(if in2 and in1;)

But we want only those values of A that exist in dataset TEMP2, and keeping the values of B, C and D from dataset TEMP.

Can you please suggest.

Super Contributor
Posts: 312

## Re: Need your help in Merging Question

Posted in reply to Sarvendra

Hi Savendra,

As far as your first question  you need to use IF IN2 as opposed toIF IN2 AND IN1;.

For your second question, let me put it this way.  If you wanted to KEEP some variables from one dataset and KEEP some other variables from another dataset, what method do you think you would employ to do so?

Regards,

Scott

Contributor
Posts: 20

## Re: Need your help in Merging Question

Posted in reply to Sarvendra

Hi Scott

I am confused in this.

Is it possible to select simultaneously some variables from one dataset and some varaibles from another datase  while merging?

We can fulfill one condition at a time but not sure how to fulfill both conditions

Sarvendra

Super Contributor
Posts: 312

## Re: Need your help in Merging Question

Posted in reply to Sarvendra

You can either KEEP them as part of the sort procedures or alternately as a dataset option as shown below.

DATA WANT;

MERGE TEMP2(IN=IN2 KEEP=A B C D) TEMP(IN=IN1 KEEP= A);

BY A;

IF IN2;

RUN;

Contributor
Posts: 20

## Re: Need your help in Merging Question

Posted in reply to Sarvendra

Got it Scott..

Thanks a lot

Super Contributor
Posts: 312

## Re: Need your help in Merging Question

Posted in reply to Sarvendra

I have seen people just allow one datasets variables write over the other datasets, but I prefer to drop the ones I don't need.

Frequent Contributor
Posts: 81

## Re: Need your help in Merging Question

Posted in reply to Scott_Mitchell

great explanation Scott :-)

thanx

anand

Discussion stats
• 9 replies
• 404 views
• 0 likes
• 5 in conversation