楼主: bobguy
1227 2

[学习分享] Find maximum/minimum in a group with data step [推广有奖]

学科带头人

7%

还不是VIP/贵宾

-

威望
0
论坛币
14187 个
通用积分
28.9279
学术水平
344 点
热心指数
363 点
信用等级
228 点
经验
104882 点
帖子
1846
精华
0
在线时间
1608 小时
注册时间
2008-7-18
最后登录
2019-3-8

中级热心勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
The same problem can be solved with SQL. But it may have a performance issue.

A data step is much faster.

Replacing  tmpy[i]=1e308;  ---> tmpy[i]=-1e308;
and min=tmpy[1];  ---> max=tmpy[&k];

will get max in the previous k rows in each group.



%let group=20000;
%let maxn=1000;
%let k=200;
data t1;
   do g=1 to &group;
        n=ceil(ranuni(123)*&maxn);
  do i=1 to n;
  x=ceil(rannor(123)*&maxn);
     output;
  end;
end;
keep g x;
run;
data t2;
  array tmpx(10000) _temporary_ ;
  array tmpy(&k) _temporary_ ;
do until(last.g);
   set t1;
   by g;
   if first.g then cnt=0;
   cnt+1 ;
   tmpx[cnt]=x;
   k=0;
   do i=1 to &k;
     tmpy[i]=1e308;
   end;
   do i=max(cnt-&k+1,1) to cnt;
     k+1;
     tmpy[k]=tmpx[i];
  *put tmpy[k]=;
   end;
   call sortn(of tmpy[*]);
   min=tmpy[1];
   output;
end;
keep x g min;
run;
二维码

扫码加我 拉你入群

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

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

关键词:Data step Maximum minimum Group Data Group Data Maximum find Step

已有 1 人评分经验 热心指数 收起 理由
crackman + 100 + 2 精彩帖子

总评分: 经验 + 100  热心指数 + 2   查看全部评分

沙发
honghejing 发表于 2011-7-26 11:26:05 |只看作者 |坛友微信交流群
数据量好大。。。

使用道具

藤椅
bobguy 发表于 2011-7-26 22:42:15 |只看作者 |坛友微信交流群
honghejing 发表于 2011-7-26 11:26
数据量好大。。。
Taking away some zeros from 3 macro variabels will reduce the amount od data.

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-13 03:25