Read previous and next observations

前端 未结 2 923
一生所求
一生所求 2021-01-13 03:28

I have a dataset like this(sp is an indicator):

datetime        sp
ddmmyy:10:30:00 N
ddmmyy:10:31:00 N
ddmmyy:10:32:00 Y
ddmmyy:10:33:00 N
ddmmyy:10:34:00 N
         


        
2条回答
  •  一生所求
    2021-01-13 04:05

    /* generate test data */
    data test;
        do dt = 1 to 100;
            sp = ifc( rand("uniform") > 0.75, "Y", "N" );
            output;
        end;
    run;
    
    proc sql;
        create table test2 as
            select  *,
                monotonic() as _n
            from  test
        ;
        create table test3 ( drop= _n ) as
            select  a.*
            from    test2 as a
            full join   test2 as b
                on  a._n = b._n + 1
            full join   test2 as c
                on a._n = c._n - 1
            where   a.sp = "Y"
                or b.sp = "Y"
                or c.sp = "Y"
        ;
    quit;
    

提交回复
热议问题