SAS中文论坛

标题: 求助sas 一列中0之间取最大值 [打印本页]

作者: shiyiming    时间: 2010-5-4 13:53
标题: 求助sas 一列中0之间取最大值
求助在sas 里如何在一列中 0中间取最大值
0
1
2
3
0
0
1
2
0
中间取最大值3、2
作者: shiyiming    时间: 2010-5-4 21:17
标题: Re: 求助sas 一列中0之间取最大值
[code:1xg7lvld]data a;
input x@@; datalines;
0 1 3 2 0 0 1 2 0
;
data b;
        set a;
        if x = 0 then n+1;
run;
data c;
        do until(last.n);
                set b;         by n;
                if x > y then y = x;
        end;
        do until(last.n);
                set b;         by n;
                z = ifn(x ^= 0, y, 0);
                output;
        end;
        drop n y;
run;[/code:1xg7lvld]
作者: shiyiming    时间: 2010-5-5 22:05
标题: Re: 求助sas 一列中0之间取最大值
谢谢 jingju11 , 解决了我的问题,高手阿
作者: shiyiming    时间: 2010-5-6 08:24
标题: Re: 求助sas 一列中0之间取最大值
to jingju11, why not do it within one pass of the data?

a simple retain works:
[code:3heasua2]
data a;
input x@@; datalines;
0 1 3 2 0 0 1 2 0
;
run;

data b;
       set a;
       retain zero 0;
       retain maxi  -9999999;
       if x^=0 then maxi=max(maxi, x);
      else do;            
            if mod(zero, 2)=1 then output;
            keep maxi;
           maxi=-constant('BIG'); zero+1;
    end;
run;
[/code:3heasua2]
作者: shiyiming    时间: 2010-5-6 16:50
标题: Re: 求助sas 一列中0之间取最大值
搅和一下
[code:j2t4m8zg]data b(keep=max);
        set a end=last ;
        retain Max;
        if x+lag(x)=0 or last then output;
        max=ifn(x+lag(x),max(x,max),x);
run;[/code:j2t4m8zg]
作者: shiyiming    时间: 2010-5-6 21:03
标题: Re: 求助sas 一列中0之间取最大值
you are right. both of your codes are much more concise than mine. on the other hand, the outputs are slight different.
[u:13i06l6r]yours[/u:13i06l6r]
Obs    Max
1      3
2      2

[u:13i06l6r]mine[/u:13i06l6r]
Obs    x    z
1     0    0
2     1    3
3     3    3
4     2    3
5     0    0
6     0    0
7     1    2
8     2    2
9     0    0
作者: shiyiming    时间: 2010-5-16 22:59
标题: Re: 求助sas 一列中0之间取最大值
按理来说jingju的code更适用,这个好像可用于挑出连续信号峰中的峰值。

另外一种情况:
[code:16tujca3]data a ;
input x;
cards;
0
1
2
3
0
0
0
1
2
0
1
3
2
0
;
run;
data ex;
  set a;
  if x=lag(x)+x and x^= 0 then n+1;
  run;
proc transpose data=ex(firstobs=2) out=ex2 prefix=n;
by n;
run;
data  ex2(keep=n max);
set ex2;
max=max(of n:);
run;
proc print;
run;[/code:16tujca3]
作者: shiyiming    时间: 2010-5-17 09:58
标题: Re: 求助sas 一列中0之间取最大值
好啊




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2