我正在尝试合并以下两个数据集:
data testA;
input categorical $3. value;
*order = _n_;
datalines;
Dog.
M 7
F 5
Cat.
M 4
F 2
;
run;
data testA;
set testA;
order=_n_;
run;
data testB;
input categorical $2. value;
datalines;
Dog.
F 3
Cat.
M 1
F 2
;
run;
proc sql;
create table final as
select a.*,b.* from testA a left join testB b on
a.categorical=b.categorical
order by order;
quit;
我想要的输出如下:
data testA;
input categorical $ value value2;
datalines;
Dog . .
M 7 .
F 5 3
Cat . .
M 4 1
F 2 2
;
run;
我遇到的问题是:1)“类别” ID没有按字母顺序排序,并且我不想更改其顺序2)由于有两个M和F,我不知道如何重命名MF,使其具有唯一性3)可能是内部联接,因为可能价值不存在于价值2
答案 0 :(得分:1)
如果您的数据的类别值是散列的行,则当您通过数据集时被发现时,您将需要创建第三列来保存这些值。为讨论起见,此新列为group
-它也将是分类的,并且在层次上在另一个类别列之上。这是执行复杂连接所需的“合成”类别,将从最终结果中删除。
want
连接将是一个简单的“黑匣子”,涉及分组,合并,偷偷摸摸的数学运算和行总和的分组总和。
该示例代码创建了一个表fulljoin_peek
,该表不是结果所必需的,但将提供对流经黑盒的数据的了解。该代码还可以处理在一组中重复出现的类别的“现实世界数据”情况。
样本数据:
data testA;
input categorical $3. value;
datalines;
Dog . * missing means categorical is really group
M 7
F 5
Cat .
M 4
F 2
Rat . * B does not have rat
T 5
Bat . * Bat has two M (repeated category) need to be summed
M 7
M 3
Fly .
M 5
F 6
;
run;
data testB;
input categorical $3. value;
datalines;
Dog . * only one category
F 3
Cat .
M 1
F 2
Cow . * A does not have cow
X 7
Bat . * Bat has two F (repeated category) need to be summed
F 7
F 13
Fly . * F M order different than A
F 16
M 20
;
run;
扩展数据具有一个组列和有关原始排序的信息:
data A2;
set testA;
if value = . then do;
* presume missing is the 'discovery' of when the
* group value has to be assigned;
group = categorical; retain group;
group_order + 1;
value_order = 0;
end;
value_order + 1;
format group_order value_order 4.;
run;
data B2;
set testB;
if value = . then do;
* presume missing is the 'discovery' of when the
* group value has to be assigned;
group = categorical; retain group;
group_order + 1;
value_order = 0;
end;
value_order + 1;
format group_order value_order 4.;
run;
加入操作(数据浏览)
* this full join shows how data matches up for the answer
* the answer will use grouping, coalescing, summing and adding;
proc sql;
create table fulljoin_peek as
select
coalesce (A.categorical, B.categorical) as want_categorical
, sum(A.value,B.value) as want_value format=4.
, A.group as A_group
, B.group as B_group
, A.group_order as A_group_order
, B.group_order as B_group_order
, A.categorical as A_cat
, B.categorical as B_cat
, A.value as A_value
, B.value as B_value
, A.value_order as A_value_order
, B.value_order as B_value_order
from
A2 as A
full join
B2 as B
on
A.group = B.group
and A.categorical = B.categorical
;
想要加入(答案)
proc sql;
create table
want (drop=group_order value_order) as
select
coalesce (A.categorical, B.categorical) as want_categorical
, min (coalesce (A.group_order-1e6,B.group_order)) as group_order
, min (coalesce (A.value_order-1e6,B.value_order)) as value_order %* -1e6 forces A order to have precedence ;
, sum ( sum (A.value,B.value) ) as value
from
A2 as A
full join
B2 as B
on
A.group = B.group
and A.categorical = B.categorical
group by
A.group, want_categorical
order by
group_order, value_order
;