如何提取具有列的最大值的SAS记录

Vic*_*tor 0 sql sas

我有一个这样的SAS数据集:

col1   col2   col3   col4    col5  col6
A1     B1     C1     D1      E1    $100
A1     B1     C1     D2      E2    $200
A2     B2     C2     D3      E3    $500
Run Code Online (Sandbox Code Playgroud)

前3列是我的关键列.我需要提取具有最高值的行col6.

所以我可以这样做:

proc sql;
   create table temp as 
   select col1,col2,col3,max(col6) as col6 
   from dataset 
   group by 1,2,3;
   select * from dataset t1 
   inner join temp t2 
   where t1.col1 = t2.col1 and t1.col2 = t2.col2 
     and t1.col3 = t2.col3 and t1.col6 = t2.col6;
quit;
Run Code Online (Sandbox Code Playgroud)

但是如何通过一次传递数据来实现同样的目标呢?有办法吗?

Jef*_*eff 5

对于许多用途,您的方法非常好.如果只使用一次传递实际上是必不可少的,则可以使用数据步骤和哈希对象.这会读取每个记录一次并在每次找到col6高于尚未看到的行时更新散列对象中的单行.

data _null_;
    if 0 then set have; /*Make sure all vars used in the hash object are set up with the correct types*/
    retain highest_so_far;
    if _n_ = 1 then do;
        highest_so_far = col6;
        declare hash hi_row();
        hi_row.definekey(co1,col2,col3,col4,col5,col6);
        hi_row.definedone();
    end;

    set have end=eof;

    if col6 > highest_so_far then do;
        hi_row.clear();
        hi_row.add();
        highest_so_far = col6;
    end;

    if (eof) then hi_row.output(want);
run;
Run Code Online (Sandbox Code Playgroud)

如果有一个最高的平局,这个程序将返回第一个,但它可以修改为返回一个arbirary数量的关系.