【发布时间】:2014-09-06 02:32:36
【问题描述】:
我是散列对象的新手,但我想了解更多关于它们的信息。我正在尝试寻找方法来尽可能地用散列替换所有可能的 proc sql 和常规合并。在使用 SASHELP 数据集时,我遇到了以下问题:
假设我有一个包含 10 个独特观察值(汽车制造商)的数据集,我想将它与包含这些汽车的各种型号的另一个表进行匹配,因此汽车在该表中重复出现。另一个需要注意的重要方面是,并非所有汽车制造商都出现在我正在查找的表格中,但我仍然希望将它们保留在我的表格中。
考虑下面的代码:
proc sql noprint;
create table x as select distinct make
from sashelp.cars;
quit;
data x;
set x (obs = 10);
if make = "GMC" then make = "XYZ";
run;
data hx (drop = rc);
if 0 then set sashelp.cars(keep = make model);
if _n_ = 1 then do;
declare hash hhh(dataset: 'sashelp.cars(keep = make model)', multidata:'y');
hhh.DefineKey('make');
hhh.DefineData('model');
hhh.DefineDone();
end;
set x;
rc = hhh.find();
do while(rc = 0);
output;
rc = hhh.find_next();
end;
if rc ne 0 then do;
call missing(model);
output;
end;
run;
如果表 X 中的所有品牌也都在表车中,那么在 call missing(model) 之后删除 output 命令将完全符合我的要求。但我也想确保 make "XYZ" 将保留在表格中。
然而,现有代码在找到所有匹配模型后会产生一个空白,如下所示:
make model
==========
Acura MDX
Acura RSX Type S 2dr
Acura TSX 4dr
... (skipping a few rows)
Acura NSX coupe 2dr manual S
Acura
Audi A4 1.8T 4dr
如您所见,在上表中,倒数第二行缺少模型。这种模式出现在每个品牌的末尾。
任何有关如何解决此问题的建议将不胜感激!
非常感谢
【问题讨论】: