I know I have done this before and it shouldn't be difficult . . . but I am drawing a blank as to how to do this efficiently.
I have two tables:
Table 1
primaryID | TypeID |
---|---|
1001 | A |
1001 | B |
1001 | C |
1002 | B |
1003 | C |
1004 | A |
1004 | C |
1005 | B |
Table 2
primaryID | valueA | valueB | valueC |
---|---|---|---|
1001 | 12 | 0.22 | 0.042 |
1002 | 10 | 0.19 | 0.039 |
1003 | 9 | 0.18 | 0.036 |
1004 | 14 | 0.3 | 0.062 |
1005 | 5 | 0.12 | 0.028 |
I want to create a new field (value) in table 1 from the value in table 2 that corresponds to TypeID from the row that matches primaryID
Want
primaryID | TypeID | Value |
---|---|---|
1001 | A | 12 |
1001 | B | 0.22 |
1001 | C | 0.042 |
1002 | B | 0.19 |
1003 | C | 0.036 |
1004 | A | 14 |
1004 | C | 0.062 |
1005 | B | 0.12 |
Thanks in advance
Just fix table two so that it is in a useful form.
First let's convert your listings into actual data.
data table1 ;
input primaryID $ TypeID $;
cards;
1001 A
1001 B
1001 C
1002 B
1003 C
1004 A
1004 C
1005 B
;
data table2;
input primaryID $ valueA valueB valueC ;
cards;
1001 12 0.22 0.042
1002 10 0.19 0.039
1003 9 0.18 0.036
1004 14 0.3 0.062
1005 5 0.12 0.028
;
You could use PROC TRANSPOSE to convert those multiple variables into multiple observations.
proc transpose data=table2 out=tall;
by primaryid;
var value: ;
run;
Then use a simple data step to tease the type out of the name.
data tall ;
set tall;
length typeid $8;
typeid = substr(_name_,6);
rename col1=value;
run;
Now you can merge or join since the type is available to compare.
proc sql;
create table want as
select a.primaryid, a.typeid, b.value
from table1 a left join tall b
on a.primaryid = b.primaryid and a.typeid = b.typeid
order by 1,2
;
quit;
If you don't mind losing some precision on the values by converting them into strings you could use the VVALUEX() function to do it with a single data step (assuming datasets are sorted).
data want;
merge table1(in=in1) table2(in=in2);
by primaryid;
if in1;
if in2 then value = input(vvaluex(cats('value',typeid)),32.);
keep primaryid typeid value;
run;