datastep - SAS task. Need to pick records conditionally -
i have been given task solve, i'm quite new programming language. when flag 0, have pick next immediate record, provided flag variable has 3 or more consecutive records value 1. have been going @ hours. below datastep. please suggest.
data two; input usubjid visit flag; cards; 1001 1 1 1001 2 1 1001 3 0 1001 4 1 1001 5 1 1001 6 1 1002 1 1 1002 2 1 1002 3 0 1002 4 1 1002 5 1 1003 1 0 1003 2 1 1003 3 1 1003 4 1 1003 5 1 ; run;
output be:-
usubjid visit flag 1001 4 1 1003 2 1
this have tried far.
proc sort data = 2 ; usubjid ; run; proc transpose data = 2 out = tran ; usubjid ; id visit ;var flag ; run; data b ; set tran ; if ( ( _1 = _2 ) , ( _1 = _3 ) ) or ( ( _2 = _3 ) , ( _2 = _4 ) ) or ( ( _3 = _4 ) , ( _3 = _5 ) ) or ( ( _4 = _5 ) , ( _4 = _6 ) ) ; run; proc sort data = b ; usubjid ; run; data c ; merge ( in = ) b ( in = b ) ; usubjid ; if ; run;
ok. deleted old answer didn't understand looking for.
data two; set two; n = _n_; run; proc sort data=two; descending n ; run; data two; set two; retain count 0; if flag count = count + 1; else count = 0; run; proc sort data=two; n; run; data 2 (drop=count n); set two; if lag(flag) = 0 , count >=3; run;
first, add row number variable.
then sort value in reverse order.
add variable counts number of consecutive records flag=1.
resort original order.
finally, subset down looking for.
Comments
Post a Comment