SAS中文论坛

标题: 请教:关于sas hash的一个问题 [打印本页]

作者: shiyiming    时间: 2009-9-25 19:08
标题: 请教:关于sas hash的一个问题
data aa;
input n1 id1 f1;
cards;
1        1        0.279103244
1        2        0.423331188
1        3        0.468192815
1        4        0.424473077
2        5        0.298561304
2        6        0.313567808
2        7        0.527609991
2        8        0.317669888
3        9        0.342632588
3        10        0.498541723
3        11        0.375768989
3        12        0.377146096

;
data bb;
input n2 id2 f2;
cards;
1        13        0.356242929
1        14        0.44764805
1        15        0.48368379
1        16        0.441579713
1        17        0.322713805
1        18        0.490257199
1        19        0.701382686
1        20        0.421768935
2        21        0.409891761
2        22        0.336426959
2        23        0.564979922
2        24        0.340961356
2        25        0.435482576
2        26        0.423822678
2        27        0.600047163
2        28        0.320887932
3        29        0.391996276
3        30        0.537861812
3        31        0.408102109
3        32        0.378189334
3        33        0.411606792
3        34        0.477971239
3        35        0.289566708
3        36        0.427040361
;
data cc;
input n3 id3 f3;
cards;
1        37        0.329143126
1        38        0.400794463
1        39        0.432646156
1        40        0.392089002
1        41        0.290059121
1        42        0.451998602
1        43        0.513093355
1        44        0.451948013
2        45        0.39674351
2        46        0.292887287
2        47        0.518306132
2        48        0.297867081
2        49        0.409592958
2        50        0.372189187
2        51        0.546214109
2        52        0.276230669
3        53        0.360550965
3        54        0.43107428
3        55        0.251990394
3        56        0.387600035
3        57        0.400069633
3        58        0.552321202
3        59        0.464109421
3        60        0.307850897
;
data dd(keep=distance idnew1 idnew2 idnew3 n1 n2 n3);
length id2 f2 id3 f3 8;
if _N_=1 then do;
declare hash h(dataset: 'bb',ordered: 'no');
declare hiter iter('h');
h.defineKey('id2');
h.defineData('id2','f2','n2');
h.defineDone();
declare hash y(dataset: 'cc',ordered: 'no');
declare hiter ite('y');
y.defineKey('id3');
y.defineData('id3','f3','n3');
y.defineDone();
call missing(id2,f2,n2,id3,f3,n3);
end;
set aa;
retain distance 2;
rc=iter.first();
rc=ite.first();
if (rc=0) then distance=1.5;
do while (rc=0);
scoredistance=f1+f2+f3;
if scoredistance<distance then do;
/*if n1=n2 then do;*/
distance=scoredistance;
idnew1=id1;
idnew2=id2;
idnew3=id3;
end;
/*end;*/
rc=iter.next();
rc=ite.next();
if (rc^=0) then do;
output;
rc1=h.remove(key: idnew2);
rc2=y.remove(key: idnew3);
end;
end;
run;
用hash来整合数据,要求结果数据集dd的一个条件是n1=n2=n3,如果只要求n1=n2或者n1=n3,注释语句可以实现,但是n1=n2=n3实现不了,请高手帮忙!
作者: shiyiming    时间: 2009-9-27 17:47
标题: Re: 请教:关于sas hash的一个问题
你的rc这个变量有问题,你两个hash对象查找的返回值公用了一个变量,这是不行的,你用两个变量试一下。




欢迎光临 SAS中文论坛 (https://mysas.net/forum/) Powered by Discuz! X3.2