|
SAS官方论坛上看到的:
data have;
input id $ score @@;
datalines; /*临时弄几个分数测试*/
S12 -20 S12 20 S12 10
S12 10 S12 20 S12 -50
S13 -40 S13 60 S13 -100
S14 10 S14 20 S14 0
S14 -30 S14 -20
;
/* change as needed to bring back desired ranking */
%let start= 1;
%let end = 3;
/* create table containing distinct id and value -- needed b/c more than one id can have the same score */
proc sql;
create table dst_id_score as
select distinct id, score from have
order by id, score desc; /* keep this sort order for ranking */
* NOTE: Table WORK.DST_ID_SCORE created, with 12 rows and 2 columns. ;
/* assign ranking by id by score */
data temp;
set dst_id_score ;
by id;
if first.id then rnk=0;
rnk + 1;
run;
* NOTE: The data set WORK.TEMP has 12 observations and 3 variables. ;
/* go back to original file and bring back all data meeting desired ranking */
proc sql;
create table final_top_ranked as
select
a.id
, a.score
, b.rnk as rank
from have a inner join temp b on a.id = b.id and a.score = b.score
where rnk between &start and &end
order by id, rnk;
* NOTE: Table WORK.FINAL_TOP_RANKED created, with 11 rows and 3 columns. ;
这一段里只要调start和end,那么不止前三名,原则上可以查询任何范围内的数据。
|