proc sql将列添加到订单获胜者

时间:2018-12-10 00:50:33

标签: sql sas proc

我需要您的帮助,我有一个练习,要求按奖牌总数排序优胜者,如果两个国家的奖牌数相同,那么我需要按顺序比较3列(这是奖牌的类型) )。

所以我有

sum(3columns) as total
order total desc, col1 desc , col2 desc , col3 desc

然后我将其保存为table,并添加monotonic()作为获胜者的位置(1 2 3 4 ...) 但是我得到了3个在3列中具有相同总结果和相同价值的团队 所以他们必须全部放在25位,但是我在努力做到这一点。

预先感谢

2 个答案:

答案 0 :(得分:0)

正确排序数据,然后使用FIRST / LAST / BY组处理。

proc sort data = have;
    by descending Total_Medals descending Gold descending Silver descending Bronze;
run;

data want; 
 set have;

 by descending Total_Medals descending Gold descending Silver descending Bronze;

 Rank = _n_;
run;

答案 1 :(得分:0)

使用first.组中最后一个变量的by评估来确定何时增加整体排名值。

* sample data;
data have (keep=eventid teamid medal);
  length eventid teamid 8 medal $8;
  do eventid = 1 to 75;
    g = ceil(100 * ranuni(123));
    do until (s ne g);
      s = ceil(100 * ranuni(123));
    end;
    do until (b ne g and b ne s);
      b = ceil(100 * ranuni(123));
    end;

    teamid = g; medal = 'gold'; output;
    teamid = s; medal = 'silver'; output;
    teamid = b; medal = 'bronze'; output;
  end;
run;

* compute medal count for each team;    
proc summary noprint data=have ;
  class teamid medal;
  ways 2;
  output out=stage2(drop=_type_);
run;

* pivot categorical medal counts into wide form;
proc transpose data=stage2 out=stage3(drop=_name_);
  by teamid;
  var _freq_;
  id medal;
run;

* compute each medaling teams total medal count;
data stage4;
  retain teamid total gold silver bronze; * sets order of variables in pdv;
  set stage3;
  total = sum (gold, silver, bronze);
run;

* sort descendingly by total and medal quality in preparation of rank assignment;
proc sort data=stage4;
  by descending total descending gold descending silver descending bronze;
run;

* assign dense rank;
data want;
  set stage4;
  by descending total descending gold descending silver descending bronze;

  if first.bronze then /* new combination detected */
    overall_rank + 1;
run;