SAS中文论坛

标题: 怎么用一步hash实现重复值的计数 [打印本页]

作者: shiyiming    时间: 2010-8-10 22:30
标题: 怎么用一步hash实现重复值的计数
要求用hash实现下面data步实现的功能。

[code:2xhffdk2]data ex;
input num1    num2;
cards;
2    1
2    1
2    1
2    1
5    1
5    2
5    1
5    2
6    1
6    5
6    2
6    5
6    5
6    1
6    5
6    2
;
proc sort;
by num1 num2;
run;
data ex;
    set ex;
          by  num1 num2;
           if first.num1 then k=0;
              k+1;        
           if last.num1  then  output;
run;

proc print;
run;[/code:2xhffdk2]
作者: shiyiming    时间: 2010-8-11 09:09
标题: Re: 怎么用一步hash实现重复值的计数
[code:2k7xb4cw]data ex;
        input num1 num2;
cards;
2    1
2    1
2    1
2    1
5    1
5    2
5    1
5    2
6    1
6    5
6    2
6    5
6    5
6    1
6    5
6    2
;
data _null_;
        if _n_=1 then
                do;
                        declare hash h(hashexp:16,ordered:'yes');
                        h.definekey('num1');
                        h.definedata('num1','num2','k');
                        h.definedone();
                        call missing(num1,num2,k);
                end;
        set ex(rename=(num2=temp)) end=last;
        if h.find()=0 then do;
                        k+1;
                        if temp>num2 then num2=temp;
                        rc=h.replace(key:num1,data:num1,data:num2,data:k);
                end;
        else rc=h.add(key:num1,data:num1,data:num2,data:1);
        if last then h.output(dataset:'out');
run;
proc print;
run;[/code:2k7xb4cw]
作者: shiyiming    时间: 2010-8-11 11:56
标题: Re: 怎么用一步hash实现重复值的计数
还缺一口气,.......

[code:17vzy9bc]data ex;
input num1    num2;
cards;
6    5
2    1
2    1
2    1
6    3
2    1
2    1
5    1
5    2
5    2
6    1
6    5
6    2
6    5
6    1
6    5
6    2
;
data ex1 ;
if _n_=1 then do;
      declare hash h(dataset:'ex', multidata: 'y');
           h.defineKey('num1','num2');
           h.defineData('num1', 'num2');
           h.defineDone();
      /* avoid uninitialized variable notes */
      call missing(num1,num2);
end;

   set ex end=end;  
          rc = h.find();
          if (rc = 0) then
               do;
                    count=1;
                    rc = h.find_next();       
                    do while(rc = 0);
                           count+1;       
                           rc = h.find_next();
                                        end;       
               end;       
drop rc;
run;
proc print;
run;[/code:17vzy9bc]




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