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
id | date | KPI_1 | KPI_2 | x | id | date | KPI_1 | KPI_2 | |
A | 01JAN17 | 10 | 23 | GREEN | A | 01JAN17 | 1 | 3 | |
B | 01-feb-17 | 12 | 3 | RED | B | 01-mar-17 | 1 | 5 | |
C | 01-mar-17 | 14 | 56 | BMW | F | 01JUN17 | 5 | 6 | |
D | 01-apr-17 | 15 | 67 | OK |
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:
id | date | KPI_1 | KPI_2 | x |
A | 01JAN17 | 10 | 23 | GREEN |
B | 01-feb-17 | 12 | 3 | RED |
C | 01-mar-17 | 14 | 56 | BMW |
D | 01-apr-17 | 15 | 67 | OK |
B | 01-mar-17 | 1 | 5 | |
F | 01JUN17 | 5 | 6 |
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.
Just drop the if statement?
data datac;
merge dataa datab;
by id date;
run;
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.