DATA Step, Macro, Functions and more

Merging Data Set based on specified condition

Reply
Occasional Contributor
Posts: 7

Merging Data Set based on specified condition

Hi all,

 

I'm trying to merging two data sets based on some field in common and different observations.

I have something like that:

 

TABLE 1                                                                                TABLE 2

iddateKPI_1KPI_2x iddateKPI_1KPI_2
A01JAN171023GREEN A01JAN1713
B01-feb-17123RED B01-mar-1715
C01-mar-171456BMW F01JUN1756
D01-apr-171567OK     

 

I want a third table with all obs from TABLE_1 and all obs from TABLE_2 (if id + date are not in TABLE_1). 

Below the wanted output:

 

iddateKPI_1KPI_2x
A01JAN171023GREEN
B01-feb-17123RED
C01-mar-171456BMW
D01-apr-171567OK
B01-mar-1715 
F01JUN1756 

 

I wrote the below code, but i'm missing something:

 

data dataa;
	length id $1 date $7 KPI_1 $6 KPI_2 $6 x $6;
	infile datalines dlm=",";
	input id date KPI_1 KPI_2 x;
	datalines;
A,01JAN17,10,23,GREEN
B,01FEB17,12,3,RED
C,01MAR17,14,56,BMW
D,01APR17,15,67,OK
;
run;

data datab;
	length id $1 date $7 KPI_1 $6 KPI_2 $6;
	infile datalines dlm=",";
	input id date KPI_1 KPI_2;
	datalines;
A,01JAN17,1,3
B,01MAR17,1,5
F,01JUN17,5,6
;
run;

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

 

Could anyone help me?

Thanks for your assistance.

Super User
Super User
Posts: 9,599

Re: Merging Data Set based on specified condition

[ Edited ]

Just drop the if statement?

data datac;
merge dataa datab;
by id date;
run;
Occasional Contributor
Posts: 7

Re: Merging Data Set based on specified condition

[ Edited ]

Thanks for your reply RW9, but actually your solution is not correct for my use case.

Because in this way the KPI_1 and KPI_2  fields should have the values from table 1 for ID A.

Below the wanted Output & Output wthout if condition:

 

A 01JAN17 10 23 GREEN
B 01FEB17 12 3 RED
B 01MAR17 1 5 
C 01MAR17 14 56 BMW
D 01APR17 15 67 OK
F 01JUN17 5 6 

 

A 01JAN17 1 3 GREEN
B 01FEB17 12 3 RED
B 01MAR17 1 5 
C 01MAR17 14 56 BMW
D 01APR17 15 67 OK
F 01JUN17 5 6 

 

Thanks.

 

 

Super User
Super User
Posts: 9,599

Re: Merging Data Set based on specified condition

Ok, so keep in a if it exists:

data datac (drop=tmp:);
merge dataa
datab (rename=(kpi_1=tmp1 kpi_2=tmp2 x=tmp3);
by id date;
if kpi_1=. then kpi_1=tmp1;
if kpi_2=. then kpi_2=tmp2;
if x="" then x=tmp3;
run;

I.e. rename the variables coming in from datab, then check if the variables after merge are missing, if so use b's version. 

Note not tested, post test data in the form of a datastep. 

Ask a Question
Discussion stats
  • 3 replies
  • 85 views
  • 0 likes
  • 2 in conversation