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’ ?
@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;
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?
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.