标题: 求教一个编程小问题 [打印本页] 作者: shiyiming 时间: 2004-4-17 02:42 标题: 求教一个编程小问题 有一个数据库格式如下
id time m
1 5 6
1 12 8
1 17 8
2 3 3
2 8 2
2 10 1
……
如何把每个id中time最大的观测值提取出来
(即是:id为1选中time17那个值;id为2的选中time为10的)作者: shiyiming 时间: 2004-4-17 03:47
proc sort data = work.a out = work.b;
by id;
run;
proc means data = work.b max;
var time;
class id;
run;作者: shiyiming 时间: 2004-4-17 04:39
[code:24017]proc sql;
create table maxtime as
select id, max(time) as maxtime
from a
group by id;
quit;[/code:24017]作者: shiyiming 时间: 2004-4-17 10:26
proc sort data=a;
by id;
data b;
set a;
by id;
if last.id;
run;
this method can't handle with the dataset in which variable have two same max value.作者: shiyiming 时间: 2004-4-17 16:43
那就综合一下,把所有最大记录保存下来
proc sort data=a;
by id descending time;
data b;
retain max;
set a;
by id;
if first.id then do;
max=time;
output;
end;
else
if time=max then output;
drop max;
run;作者: shiyiming 时间: 2004-4-17 17:03
我这边不能试,这个可能更简洁点
[code:b042b]proc sort data=a;
by id descending time;
data b;
set a;
by id;
if first.id or time=lag(time);
run;[/code:b042b]作者: shiyiming 时间: 2004-4-17 18:13
可以运行,的确要简洁些作者: shiyiming 时间: 2004-4-19 00:08
谢谢大家,问题解决^_^作者: shiyiming 时间: 2004-4-19 20:43
[quote="yooyork":46d54]
......
[color=red:46d54]by id;[/color:46d54]
......
[/quote:46d54]
应该是
[code:46d54]proc sort data=a;
by id time;
data b;
set a;
by id;
if last.id;
run;[/code:46d54]作者: shiyiming 时间: 2004-4-19 22:30
哦,是的,疏忽了 :)