楼主: wdxmahone
8758 16

sas取前100行的最小值 [推广有奖]

  • 0关注
  • 1粉丝

已卖:348份资源

硕士生

40%

还不是VIP/贵宾

-

威望
0
论坛币
12568 个
通用积分
0
学术水平
2 点
热心指数
3 点
信用等级
2 点
经验
3518 点
帖子
121
精华
0
在线时间
155 小时
注册时间
2010-1-11
最后登录
2022-9-4

楼主
wdxmahone 发表于 2011-7-22 16:13:36 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
各位大侠,我想取前100行的最小值,怎么实现。可以考虑以下测试数据。x1 x2
01 1
01 2
01 1
01 4
01 5
02 1
02 -3
02 3
02 4
02 5
;
X1是分组变量,我想取X2的每个分组变量前三行的最小值。
我用sql的group by做,但是由于数据量很大,要循环几千次,相当慢,不知道各位大侠有什么好的办法。谢哈~~
二维码

扫码加我 拉你入群

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

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

关键词:最小值 Group 各位大侠 测试数据 sql SAS

回帖推荐

pobel 发表于6楼  查看完整内容

data test1; set test; by x1; retain min_x2; drop n min_x2; if first.x1 then do; n=1; min_x2=x2; end; else do; n+1; if n=3 then min=min_x2; run;

本帖被以下文库推荐

沙发
pobel 在职认证  发表于 2011-7-22 16:28:45
data test;
  input x1 x2;
  cards;
01 1
01 2
01 1
01 4
01 5
02 1
02 -3
02 3
02 4
02 5
;

*** Solution 1;
data test1;
    set test;
        by x1;
        retain min_x2;
        keep x1 min_x2;
        if first.x1 then do;
           n=1;
           min_x2=x2;
        end;
        else do;
       n+1;
           min_x2=min(min_x2,x2);
           if n=3 then output;
        end;
run;
和谐拯救危机

藤椅
wdxmahone 发表于 2011-7-22 16:39:29
2# pobel 可能我说的有点歧义,我想得到
x1 x2 x3
01 1 .
01 2 .
01 1 1
01 4 1
01 5 1
02 1 .
02 -3 .
02 3 -3
02 4 -3
02 5  3
;
每一行都要找到前三行的最小值,谢谢!

板凳
soporaeternus 发表于 2011-7-22 16:46:43
这个sql貌似真的小小复杂,还是data步比较直观啊
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
pobel + 1 + 1 + 1 没想出只用SQL的方法

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

Let them be hard, but never unjust

报纸
wdxmahone 发表于 2011-7-22 16:59:20
4# soporaeternus 是啊,data步不大清楚怎么弄,哎,sas就是行处理比较麻烦

地板
pobel 在职认证  发表于 2011-7-22 17:18:51
data test1;
    set test;
        by x1;
        retain  min_x2;
        drop n min_x2;
        if first.x1 then do;
           n=1;
           min_x2=x2;
        end;
        else do;
       n+1;
           if n<=3 then min_x2=min(min_x2,x2);
           put x1= min_x2=;
        end;
        if n>=3 then  min=min_x2;
run;
已有 2 人评分经验 威望 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子
admin + 1 感谢您长久以来对各位坛友的热心帮助

总评分: 经验 + 3  威望 + 1  论坛币 + 3   查看全部评分

和谐拯救危机

7
yugao1986 发表于 2011-7-22 17:26:44
  1. DATA a;
  2.   INPUT x1 x2;
  3.   m=lag(x2);
  4.   n=lag(m);
  5.   DATALINES;
  6. 01 1
  7. 01 2
  8. 01 1
  9. 01 4
  10. 01 5
  11. 02 1
  12. 02 -3
  13. 02 3
  14. 02 4
  15. 02 5
  16. ;
  17. data b(keep=x1 x2 x3);
  18.    set a;
  19.    by x1;
  20.    retain x3 l;
  21.    if first.x1 then call missing(x3);
  22.       else x3=min(x2,m,n);
  23.    l=lag(x3);
  24.    if l=. then x3=.;
  25. run;
  26. proc print;run;
复制代码
已有 1 人评分经验 热心指数 收起 理由
crackman + 80 + 2 鼓励积极发帖讨论

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

三人行必有我师

8
shenliang_111 发表于 2011-7-22 19:49:00
1# wdxmahone
试试这个:
data aaa(drop=minn) ;
do _n_=1 by 1 until(last.x1);
set a;
by x1;
/*这里还可以优化*/
if _n_=1 then x3=x2;
else if _n_ le 3 then x3=min(x3,x2);
end;
minn=x3;
do _n_=1 by 1 until(last.x1);
set a;
by x1;
if _n_ le 2 then do; call missing(x3);
output;
end;
else do;
x3=minn;
output;
end;
end;
run;

9
ntsean 发表于 2011-7-22 22:52:30
try this:

1. Use proc expand to find first lag, second lag, for each obs (use by to calculate for each group)

2. find minimum of those 2 lags and the current value for each obs. But be careful when one of those 3 values having missing value, since min function doesn't count missing values.

10
ntsean 发表于 2011-7-22 23:09:46
data test;
input x1 $ x2;
datalines;
01 1
01 2
01 1
01 4
01 5
02 1
02 -3
02 3
02 4
02 5
;
run;

proc expand data=test out=test1;
by x1;
convert x2 = lag1 / transformout=(lag 1);
convert x2 = lag2 / transformout=(lag 2);
run;

data out(keep=x1 x2 x3);
set test1;
if x2 ne . and lag1 ne . and lag2 ne . then x3=min(x2,lag1,lag2);
run;
已有 1 人评分经验 热心指数 收起 理由
crackman + 80 + 2 鼓励积极发帖讨论

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

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-6 21:43