全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
10368 4
2016-12-31
为简明起见,仍以 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<=5;
drop weight;
run;

                        Obs    Name       Sex    Age    Height    count
                          1    Mary        F      15     66.5       1
                          2    Barbara     F      13     65.3       2
                          3    Judy        F      14     64.3       3
                          4    Carol       F      14     62.8       4
                          5    Janet       F      15     62.5       5
                          6    Philip      M      16     72.0       1
                          7    Alfred      M      14     69.0       2
                          8    Ronald      M      15     67.0       3
                          9    William     M      15     66.8       4
                         10    Robert      M      12     64.8       5


方法2


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 a

where calculated h_rank <= 5

order by sex, h_rank;

quit;


                               Sex  Name        Height    h_rank
                               F    Mary          66.5         1
                               F    Barbara       65.3         2
                               F    Judy          64.3         3
                               F    Carol         62.8         4
                               F    Janet         62.5         5
                               M    Philip          72         1
                               M    Alfred          69         2
                               M    Ronald          67         3
                               M    William       66.8         4
                               M    Robert        64.8         5



- 最大值


方法1


data test1;
set sashelp.class;
proc sort;
by sex descending height;
run;

data test2;
set test1;
if first.sex then output;
by sex;
run;
                        Obs     Name     Sex    Age    Height    Weight

                         1     Mary       F      15     66.5       112
                         2     Philip     M      16     72.0       150


方法2


proc sql;

select *, max(height) as H_max

from sashelp.classgroup by sex

having height=H_max;
quit;

                     Name      Sex       Age    Height    Weight     H_max
                     Mary      F          15      66.5       112      66.5
                     Philip    M          16        72       150        72


方法3,


proc sql;

select a.name, a.sex, a.height from sashelp.class a

where height = (select max(height) from sashelp.class where sex = a.sex)

order by a.sex;

quit;
                                    Name      Sex    Height
                                    Mary      F        66.5
                                    Philip    M          72


- 第二大值


方法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 = 2;
drop weight;
run;
                        Obs     Name      Sex    Age    Height    count
                         1     Barbara     F      13     65.3       2
                         2     Alfred      M      14     69.0       2


方法2


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 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 a

where 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



====



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-1-4 18:01:38
写的不错 赞
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-1-4 18:02:16
写的不错 赞
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-9-4 17:03:08
作为初学的人一看就明白,赞
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2019-11-13 10:00:39
data test2;
set test1;
by sex;
if first.sex then count=0;
count+1;
output;
run;
这一段如果按照性别和年龄分组,怎么写
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群