SAS笔记3:分组求 top 5 值,最大值,第二大值,第N大值 |
您所在的位置:网站首页 › sas取数据集前30数据 › SAS笔记3:分组求 top 5 值,最大值,第二大值,第N大值 |
为简明起见,仍以 sashelp.class 数据集为例,要求以性别分组,求各组 top 5 ,最大,第2大,第N大 height 值。
sashelp.class 原表:
Obs Name Sex Age Height Weight 1 Joyce F 11 51.3 50.5 2 Louise F 12 56.3 77.0 3 Alice F 13 56.5 84.0 4 Jane F 12 59.8 84.5 5 Janet F 15 62.5 112.5 6 Carol F 14 62.8 102.5 7 Judy F 14 64.3 90.0 8 Barbara F 13 65.3 98.0 9 Mary F 15 66.5 112.0 10 James M 12 57.3 83.0 11 Thomas M 11 57.5 85.0 12 John M 12 59.0 99.5 13 Jeffrey M 13 62.5 84.0 14 Henry M 14 63.5 102.5 15 Robert M 12 64.8 128.0 16 William M 15 66.8 112.0 17 Ronald M 15 67.0 133.0 18 Alfred M 14 69.0 112.5 19 Philip M 16 72.0 150.0
- top 5 方法1 data test1; set sashelp.class; proc sort; by sex descending height; run; data test2; set test1; by sex; if first.sex then count=0; count+1; output; run; data test3; set test2; where count= a.height and a.sex = b.sex ) as h_rank from sashelp.class a where calculated h_rank = a.height and a.sex = b.sex ) as h_rank from sashelp.class a where calculated h_rank = 2 order by sex, h_rank; quit; Sex Name Height h_rank F Barbara 65.3 2 M Alfred 69 2 - 第N大
方法1 data test1; set sashelp.class; proc sort; by sex descending height; run; data test2; set test1; by sex; if first.sex then count=0; count+1; output; run; %let n = 6; data test3; set test2; where count = &n; drop weight; run; Obs Name Sex Age Height count 1 Jane F 12 59.8 6 2 Henry M 14 63.5 6 方法2 %let n = 6; proc sql; select a.sex, a.name, a.height, (select count(distinct b.height) from sashelp.class b where b.height >= a.height and a.sex = b.sex ) as h_rank from sashelp.class awhere calculated h_rank = &n order by sex, h_rank; quit; Sex Name Height h_rank F Jane 59.8 6 M Henry 63.5 6 ==== ![]() |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |