<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Keep only Consecutuive flags in consecutive cycles in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/741384#M231759</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80890"&gt;@Paakay&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to solve the problem is by using the lag function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=old_cycle outkey);
	set chk;
	by ID;
	retain outkey;
	old_Cycle = lag(Cycle);
	if first.ID then outkey = 1;
	else if Cycle &amp;gt; old_cycle + 1 then outkey = 0;
	if outkey = 1 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 14 May 2021 11:32:50 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2021-05-14T11:32:50Z</dc:date>
    <item>
      <title>Keep only Consecutuive flags in consecutive cycles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/741380#M231758</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have ID's with multiple flags in cycles however some id's flags are not consecutive. For example ID A has two flags in cycle 1 but none in cycle 2,3,4 but then has another flag in cycle 5. I want to keep only the flags in cycle 1 and delete flags in cycle 5 since there is a break&amp;nbsp;in cycle at 2. Another example is ID B has flags in cycles 1, 2,3 but not in cycle 4 however has flags in cycles 5 and 6. Since there is a break in cycle at 4, I want to keep the consecutive flags in cycles 1,2,3 and delete cycles 5 and 6. Can someone help me with this. Thank you. Sample data is below&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA chk;
   INPUT  ID $  Flag Cycle ;
CARDS;
A 1 1
A 1 1
A 1 5
A 1 5
B 1 1
B 1 2
B 1 3
B 1 5
B 1 6
C 1 1
C 1 1
C 1 2
C 1 4
C 1 5
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Output Data should look like this&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data chk2;
  INPUT  ID $  Flag Cycle ;
CARDS;
A 1 1
A 1 1
B 1 1
B 1 2
B 1 3
C 1 1
C 1 1
C 1 2
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 May 2021 10:58:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/741380#M231758</guid>
      <dc:creator>Paakay</dc:creator>
      <dc:date>2021-05-14T10:58:00Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only Consecutuive flags in consecutive cycles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/741384#M231759</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80890"&gt;@Paakay&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to solve the problem is by using the lag function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=old_cycle outkey);
	set chk;
	by ID;
	retain outkey;
	old_Cycle = lag(Cycle);
	if first.ID then outkey = 1;
	else if Cycle &amp;gt; old_cycle + 1 then outkey = 0;
	if outkey = 1 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 May 2021 11:32:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/741384#M231759</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2021-05-14T11:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only Consecutuive flags in consecutive cycles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/741404#M231762</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA chk;
   INPUT  ID $  Flag Cycle ;
CARDS;
A 1 1
A 1 1
A 1 5
A 1 5
B 1 1
B 1 2
B 1 3
B 1 5
B 1 6
C 1 1
C 1 1
C 1 2
C 1 4
C 1 5
;
RUN;

data want;
 set chk;
 by id;
 if first.id then group=0;
 if first.id or dif(cycle)&amp;gt;1 then group+1;
 if group=1;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 May 2021 12:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/741404#M231762</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-05-14T12:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only Consecutuive flags in consecutive cycles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/744973#M233464</link>
      <description>&lt;P&gt;Thanks Eriklund. That works but there was problem . What if I want to flag ID's with consecutive flags as 1 and those without consecutive flags as 0. For example ID 1 is consecutive because it starts from cycle 1 to cycle 4 so I create a flag for ID 1 as 1. However for ID's with no consecutive cycle numbers like ID 2 (its starts from cycle 2 to cycle 5, missing cycle 1) and ID 3 also starts from cycle 8 missing cycles 1-7, &amp;nbsp;I flags these ID's as 0. Is there a way to go about this. Thanks you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
 input ID $1. Cycle;
Datalines;
1    1
1    1
1    2
1    3
1    4
2   3
2   4
2   5
3   8
3   8
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Jun 2021 15:38:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/744973#M233464</guid>
      <dc:creator>Paakay</dc:creator>
      <dc:date>2021-06-01T15:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only Consecutuive flags in consecutive cycles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/744975#M233466</link>
      <description>&lt;P&gt;Thanks Ksharp. That works but there was problem . What if I want to flag ID's with consecutive flags as 1 and those without consecutive flags as 0. For example ID 1 is consecutive because it starts from cycle 1 to cycle 4 so I create a flag for ID 1 as 1. However for ID's with no consecutive cycle numbers like ID 2 (its starts from cycle 2 to cycle 5, missing cycle 1) and ID 3 also starts from cycle 8 missing cycles 1-7, &amp;nbsp;I flags these ID's as 0. Is there a way to go about this. Thanks you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
 input ID $1. Cycle;
Datalines;
1    1
1    1
1    2
1    3
1    4
2   3
2   4
2   5
3   8
3   8
;
run;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Jun 2021 15:40:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/744975#M233466</guid>
      <dc:creator>Paakay</dc:creator>
      <dc:date>2021-06-01T15:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only Consecutuive flags in consecutive cycles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/745146#M233539</link>
      <description>&lt;P&gt;OK. Firstly pick up the ID which have 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA chk;
infile cards expandtabs truncover;
   INPUT  ID $  Flag Cycle ;
CARDS;
1 1   1
1  1  1
1 1   2
1  1  3
1 1   4
2 1  3
2 1  4
2 1  5
3 1  8
3  1 8
;
RUN;
proc sql;
create table have as
select * from chk
 where id in (select id from chk where cycle=1)
  order by id,flag,cycle;
quit;

data want;
 set have;
 by id;
 if first.id then group=0;
 if first.id or dif(cycle)&amp;gt;1 then group+1;
 if group=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Jun 2021 11:50:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-only-Consecutuive-flags-in-consecutive-cycles/m-p/745146#M233539</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-02T11:50:41Z</dc:date>
    </item>
  </channel>
</rss>

