sas hash2 多对多的情况

来源:互联网 发布:windows 直接登录 编辑:程序博客网 时间:2024/06/10 00:21
data testdup(index=(key));
length key data 8;
input key data;
datalines;
1 10
2 11
1 15
3 20
2 16
2 9
3 100
5 5
1 5
4 6
5 99
;
run;
data a;
input key data1;
cards;
1 8
1 11
2 14
2 3
5 6
5 8
;
run;

data RESULT(KEEP=KEY DATA RESULT);
length r 8;
IF _N_=0 THEN SET A;
IF _N_=1 THEN DO;
dcl hash h(dataset:'A', multidata: 'y', ordered: 'y');/*multidata:'y'允许重复的KEY在hash中出现,默认情况不允许*/
h.definekey('key');
h.definedata('key', 'data1');
h.definedone();
call missing (key, data1);
END;
SET testdup;
by key;
result=0;

rc = h.find();
if (rc = 0) then do;
if data>data1 then result=1;
put key= data= data1= result=;
h.has_next(result: r);/*h.has_next():在允许出现相同关键词的前提下,判断是否存在下一条相同关键字的观测*/
do while(r ne 0);/*不等于0表示存在下一条相同关键字的观测*/
rc = h.find_next();/*h.find_next():在允许出现相同关键字的前提下,寻找下一条相同关键字的观测*/
put key= data= data1= result;
if data>data1 then result=2 ;
else result = 3;
put 'before replace ' key= data= data1= result=;
rc = h.replacedup();/*h.replacedup():在允许出现相同关键词的前提下,使用新的数据代替当前关键字对应的数据等*/
put 'after replace ' key= data= data1= result=;
h.has_next(result: r);
end;
end;
RUN;

这种处理,可以用于进行set数据集 取hash表中时间最新数据,或者按key值排序,进行于hash表中最大值或最小值进行对比等,
下面是一个关于hash 很不错的链接
https://wenku.baidu.com/view/2cc9b821a1c7aa00b42acba0.html


原创粉丝点击