如何选择每个dicipline最高分的学生

时间:2017-07-24 05:53:37

标签: sql sas

假设我有类似下面的数据集。我需要输出哪个学生在每个科目中得到高分并且学生姓名应该在输出中?

input student$ phy math chem eng;
datalines;
raju 89 56 37 99
raki 80 90 76 45
rani 56 65 88 43
ramya 67 98 70 67
;

使用任何方法(datasep,proc sql或其他过程)

1 个答案:

答案 0 :(得分:0)

使用临时数组自动保留的事实:

data chapions (keep=student high_score);
    set scores end=last;
    array score {} phy math chem eng;
    array high {4} _temporary_ ;
    array champ {4} $ _temporary_ ;
    if _N_ eq 1 then do dicipline = 1 to 4;
        high(dicipline) = score(dicipline);
        champ(dicipline) = student;
    end;
    else  do dicipline = 1 to 4;
        if high(dicipline) lt score(dicipline) then do;
            high(dicipline) = score(dicipline);
            champ(dicipline) = student;
        end;
    end;
    if last then do dicipline = 1 to 4;
        high_score = high(dicipline);
        student = champ(dicipline) ;
        output;
    end;
run; 

未经测试的草稿,在通过正确描述所需输出来改进问题时进行改进