DATA Step, Macro, Functions and more

how to flag consecutive values

Reply
New Contributor
Posts: 3

how to flag consecutive values

Hi,

I have this querry...

i have a dataset like this and i want to flag it where i have consecutive values.

data bob;

input subjid lbtestcd$ lbdy flag1$;

datalines;

101 alt 1 e

101 alt 2 .

101 alt 3 e

101 alt 4 e

101 ast 1 e

101 ast 3 e

101 ast 4 .

101 ast 5 e

102 alt 1 e

102 alt 2 .

102 alt 3 e

102 alt 4 e

102 ast 1 e

102 ast 3 e

102 ast 4 .

102 ast 5 e

102 ast 6 e

;

run;


i want an output like

i want to flag consecutive 'e' values for consecutive visits by subject and test.


could you please help me out


thanks.

Super Contributor
Posts: 543

Re: how to flag consecutive values

hmm, something like this may work:

proc sort data = bob;by subjid lbtestcd lbdy;run;

data bob_1;

    set bob;

    by subjid lbtestcd lbdy;

        prev_e = lag(flag1);

        prev_visit = lag(lbdy);

        if first.subjid then do;

                prev_e = .;prev_visit = .;

        end;

yes_prior = (prev_e = flag1);*flag that there is a prior 'e';

consecutive = ( lbdy - prev_visit = 1);*flag that there are consecutive visits;

what_you_want = (yes_prior = 1 & consecutive = 1);*the flag you are looking for, I think;

run;

Best of luck!

Anca.

Super User
Posts: 5,516

Re: how to flag consecutive values

Perhaps a simpler way to go:

proc sort data=bob;

by subjid lbtestcd lbdy;

run;

data want;

   set bob;

   by subjid lbtestcd flag1 notsorted;

   if lbtestcd='e' and (first.flag1=0 or last.flag1=0) then new_flag='*';

run;

Depending on what you need, you could remove one condition:

if (first.flag1=0 or last.flag1=0) then new_flag='*';

Good luck.

Contributor
Posts: 45

Re: how to flag consecutive values

Hello Rams,

Is the outcome below ("RESULTS") what you desire? I'm not sure I have understood the question.

data bob;

input subjid lbtestcd$ lbdy flag1$;

datalines;

101 alt 1 e

101 alt 2 .

101 alt 3 e

101 alt 4 e

101 ast 1 e

101 ast 3 e

101 ast 4 .

101 ast 5 e

102 alt 1 e

102 alt 2 .

102 alt 3 e

102 alt 4 e

102 ast 1 e

102 ast 3 e

102 ast 4 .

102 ast 5 e

102 ast 6 e

;

run;

data want(keep=subjid lbtestcd lbdy flag1 flag2);

if(1=2) then set bob;

           declare hash f(ordered:"a",multidata:"y");

rc=f.defineKey("subjid", "lbtestcd", "flag2");

rc=f.defineData("subjid", "lbtestcd", "lbdy","flag1","flag2");

rc=f.defineDone();

length flag2 $1;

flag2="";

do until(done);

set bob end=done;

rcf=f.check();

if (missing(flag1)+rcf)=0 then do;

flag2="Y";

f.add();

end;

else if rcf ne 0 then f.add();

else if missing(flag1) then f.remove();

output;

call missing(of _all_);

end;

stop;

run;

RESULTS:

 

subjid

lbtestcd

lbdy

flag1

flag2

1

101

alt

1

e

 

2

101

alt

2

 

 

3

101

alt

3

e

 

4

101

alt

4

e

Y

5

101

ast

1

e

 

6

101

ast

3

e

Y

7

101

ast

4

 

 

8

101

ast

5

e

 

9

102

alt

1

e

 

10

102

alt

2

 

 

11

102

alt

3

e

 

Ask a Question
Discussion stats
  • 3 replies
  • 1114 views
  • 0 likes
  • 4 in conversation