BookmarkSubscribeRSS Feed
Trus
Calcite | Level 5

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.

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just drop the if statement?

data datac;
merge dataa datab;
by id date;
run;
Trus
Calcite | Level 5

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.

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1037 views
  • 0 likes
  • 2 in conversation