BookmarkSubscribeRSS Feed
molla
Fluorite | Level 6

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’ ?

3 REPLIES 3
PaigeMiller
Diamond | Level 26

@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
ballardw
Super User

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?

 

Ksharp
Super User
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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2662 views
  • 0 likes
  • 4 in conversation