这个问题是我之前question的后续问题。除了使用哈希对象进行合并之外,当我在同一个数据库中进行查找时,我正在努力处理哈希对象。我有这个数据库,其中持续更新客户端的order_number
:
Client Order_number New_number
XYZ 1000 1001
1001 1002
ABC 1006 1009
1009 1017
SST 1010 1011
1017 1020
1020 1030
1011 1050
与我之前的问题类似,我需要以下内容:
Client Order_number New_number
XYZ 1000 1001
XYZ 1001 1002
ABC 1006 1009
ABC 1009 1017
SST 1010 1011
ABC 1017 1020
ABC 1020 1030
SST 1011 1050
换句话说,当缺少客户端名称时,我使用order_number与先前的new_number匹配以查找客户端。
订单首先按order_number
排序,然后按new_number
排序。
我正在尝试通过对前一个问题中发布的代码进行一些更改来实现代码,但没有成功。
答案 0 :(得分:3)
如果您的数据是SAS数据集,这应该有效。此示例将重写为现有数据集。什么是过滤所有客户端已知的观察结果,然后在客户端为空时使用哈希循环遍历订单数量链。
data orders (keep=c o n rename=(c=client o=order_number n=new_number));
length client $8 order_number 8 new_number 8;
* declare hash object;
if _n_ = 1 then do;
declare hash h(dataset:'orders');
h.definekey('order_number');
h.definedata('client','new_number');
h.definedone();
call missing(client, order_number, new_number);
end;
* set statement with rename of original column names;
set orders (rename=(order_number=o new_number=n client=c) where=(c ne ''));
* find in hash ;
rc = h.find(key:n);
* write first observation;
output;
* do loop through chain of order numbers while client is empty;
do while (rc = 0 and client = '');
* update values of output dataset;
o = n;
n = new_number;
rc = h.find(key:n);
* write current observation;
output;
end;
run;