标题: 如何以分组、组合的方式来求得结果 [打印本页] 作者: shiyiming 时间: 2011-12-7 10:34 标题: 如何以分组、组合的方式来求得结果 [code:3kalp0zg]data test;
input num & $2. group & $8. compare & $20.;
cards;
01 merry A B C D
02 merry A B E G H
03 merry B E
04 merry AA R
05 agit W X Y
06 agit G HH
07 wang A C DD W Z
08 wang B C DD Z
09 wang C W
10 yang A S
;
[/code:3kalp0zg]
需要得到的结果如下:
num------group------compare--------count--------_num_
01--------merry------A B C D------------2-------------02
02--------merry------A B E G H---------2--------------01,03
03--------merry------B E----------------2--------------02
04--------merry------AA R--------------0--------------空
05--------agit--------W X Y-------------0--------------空
06--------agit--------G HH--------------0--------------空
07--------wang-------A C DD W Z-------3--------------08
08--------wang-------B C DD Z-----------3-------------07
09--------wang-------C W----------------2--------------07
10--------yang--------A S-----------------0--------------空
*:第一条观测的count=2,_num_=02
说明:count值为编号为01的成员merry与其他编号为02,03,04的同名组成员(merry)的compare值中以空格分隔的词相同数的最大值,而_num_则对应与该成员的compare中词相同最多的同名组成员编号(num)...
感觉有点复杂,希望有大侠能解决上边的这种以分组、组合的方式来的求的结果----作者: shiyiming 时间: 2011-12-10 11:57 标题: Re: 如何以分组、组合的方式来求得结果 额,自己顶一下多!!作者: shiyiming 时间: 2011-12-12 14:46 标题: Re: 如何以分组、组合的方式来求得结果 [code:3fv0pgh8]data test;
input num $2. group $8. compare & $20.;
cards;
01 merry A B C D
02 merry A B E G H
03 merry B E
04 merry AA R
05 agit W X Y
06 agit G HH
07 wang A C DD W Z
08 wang B C DD Z
09 wang C W
10 yang A S
;
data htest;
set test;
rename num=hnum group=hgroup compare=hcompare;
run;
data out;
length hnum $2 hgroup $8 hcompare _num_ $20;
if _n_=1 then do;
declare hash h(dataset:'htest',hashexp:6,ordered:'y');
declare hiter hi('h');
rc=h.defineKey('hnum','hgroup');
rc=h.defineData('hnum','hgroup','hcompare');
rc=h.defineDone();
call missing(hnum,hgroup,hcompare);
end;
set test;
count=0; _num_='';
do while(hi.next()=0);
if hgroup=group and hnum ne num then do;
n=0;
do i=1 to count(compbl(hcompare),' ');
n=n+(indexw(compare,scan(hcompare,i)) gt 0);
end;
if count=n and n>0 then _num_=catx(',',_num_,hnum);
else if n>count then do;
_num_=hnum;
count=n;
end;
end;
end;
keep num group compare count _num_;
run;[/code:3fv0pgh8]作者: shiyiming 时间: 2011-12-12 23:55 标题: Re: 如何以分组、组合的方式来求得结果 [code:2i5fhxne]data test;
input num $ group $ compare &:$20.;
cards;
01 merry A B C D
02 merry A B E G H
03 merry B E
04 merry AA R
05 agit W X Y
06 agit G HH
07 wang A C DD W Z
08 wang B C DD Z
09 wang C W
10 yang A S
;
data out;
length num group $8 compare _num_ $20;
if _n_=1 then do;
declare hash h(dataset:'test',multidata:'y',hashexp:6,ordered:'y');
h.definekey('group');
h.definedata('num','compare');
h.definedone();
call missing (group,num,compare);
end;
set test;
rawgroup=group; rawnum=num; rawcompare=compare;
count=0; _num_='';
if (h.find()=0) then do;
if num ne rawnum then do;
do i=1 to count(compbl(compare),' ');
if indexw(rawcompare,scan(compare,i,' ')) gt 0 then count+1;
end;
if count gt 0 then _num_=num;
end;
do while(h.find_next()=0);
n=0;
if num ne rawnum then do;
do i=1 to count(compbl(compare),' ');
if indexw(rawcompare,scan(compare,i,' ')) gt 0 then n+1;
end;
if count=n and n>0 then _num_=catx(',',_num_,num);
else if n>count then do;
_num_=num;
count=n;
end;
end;
end;
end;
group=rawgroup; num=rawnum; compare=rawcompare;
keep _num_ num group compare;
run;[/code:2i5fhxne]作者: shiyiming 时间: 2011-12-13 22:12 标题: Re: 如何以分组、组合的方式来求得结果 要多谢hopewell,都解答两次了,我才知道...版主就是帅,呵呵!作者: shiyiming 时间: 2011-12-13 22:26 标题: Re: 如何以分组、组合的方式来求得结果 用我的测试数据说明第二段代码效率比第一段代码至少提高了上千或上万倍,版主真是太帅了....