DATA Step, Macro, Functions and more

Select Consecutive Identical values

Reply
Frequent Contributor
Posts: 76

Select Consecutive Identical values

[ Edited ]

Hii,

am not getting the idea about this.pls help me

OBS        SUBJECT               FLAG   DATE

1              A                             Y

2              A                             N                            

3              B                             Y

4              B                             Y

5              B                             Y

6              C                             N

7              C                             Y

8              C                             Y

9              C                             Y

10           C                             Y

11           C                             N

HOW TO GET  THE OBSERVATIONS WITH THREE CONSECUTIVE   FLAG  ‘Y’ ?

Respected Advisor
Posts: 3,040

Re: base sas


molla wrote:

Hii,

am not getting the idea about this.pls help me

OBS        SUBJECT               FLAG   DATE

1              A                             Y

2              A                             N                            

3              B                             Y

4              B                             Y

5              B                             Y

6              C                             N

7              C                             Y

8              C                             Y

9              C                             Y

10           C                             Y

11           C                             N

HOW TO GET  THE OBSERVATIONS WITH THREE CONSECUTIVE   FLAG  ‘Y’ ?


So you want observations 5, 9 and 10. Is that correct?

 

Here's one solution.

data want;
    set have;
    prev1=lag(flag);
    prev2=lag2(flag);
    if flag='Y' and prev1='Y' and prev2='Y' then output;
run;
--
Paige Miller
Super User
Posts: 13,560

Re: base sas

Please use a subject that relates to the problem. For example this could be Select Consecutive Identical values.

 

And please show the actuall desired output. Your request could be considered as 1) keep the record where it and the two previous have the same value for flag 2) select all of the records in a row of 3 or more identical values, which depending on how this is interpretted could lead to multiples of some observations due to overlap of the consecutives. Example: Obs 9 is third Y so output observations 7, 8 and 9. Then Obs 10 is 4th Y so output observations 8, 9 and 10.

 

Your data tends to imply that the consideration should be within a SUBJECT but that was not stated. Otherwise suppose the data looks like this:

OBS        SUBJECT               FLAG   DATE
1              A                             Y
2              A                             Y                             
3              B                             Y
4              B                             N
5              B                             Y
6              C                             N
7              C                             Y
8              C                             Y
9              C                             Y
10           C                             Y
11           C                             N

Should observation 1, 2 and 3 be output?

 

Super User
Posts: 10,784

Re: Select Consecutive Identical values

data have;
input OBS        SUBJECT    $    FLAG $;
cards;
1              A                             Y
2              A                             N                            
3              B                             Y
4              B                             Y
5              B                             Y
6              C                             N
7              C                             Y
8              C                             Y
9              C                             Y
10           C                             Y
11           C                             N
;
run;
data temp;
 set have;
 if flag=lag(flag) and subject=lag(subject) and
    flag=lag2(flag) and subject=lag2(subject) then output;
keep obs;
run;
data key;
 set temp;
 k=obs;output;
 k=obs-1;output;
 k=obs-2;output;
run;
proc sql;
select *
 from have where obs in
 (select k from key);
quit;
Ask a Question
Discussion stats
  • 3 replies
  • 329 views
  • 0 likes
  • 4 in conversation