<?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 count consecutive occurrence of same value within group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747492#M234610</link>
    <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am stuck by counting consecutive values in the program and wish you could help me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data is like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID count keep&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 1&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 2&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 3&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 4&amp;nbsp; No&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 5&amp;nbsp; No&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 6&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 7&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 1&amp;nbsp; No&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 2&amp;nbsp; No&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 3&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 4&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 5&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 6&amp;nbsp; No&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the "count number" range of the first consecutive "Yes"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For ID=1&amp;nbsp; count number should be "1-3"&lt;/P&gt;&lt;P&gt;For ID=2&amp;nbsp; count number should be "3-5"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Jun 2021 22:42:37 GMT</pubDate>
    <dc:creator>linda0910</dc:creator>
    <dc:date>2021-06-11T22:42:37Z</dc:date>
    <item>
      <title>count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747492#M234610</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am stuck by counting consecutive values in the program and wish you could help me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data is like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID count keep&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 1&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 2&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 3&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 4&amp;nbsp; No&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 5&amp;nbsp; No&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 6&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 7&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 1&amp;nbsp; No&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 2&amp;nbsp; No&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 3&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 4&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 5&amp;nbsp; Yes&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 6&amp;nbsp; No&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find the "count number" range of the first consecutive "Yes"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For ID=1&amp;nbsp; count number should be "1-3"&lt;/P&gt;&lt;P&gt;For ID=2&amp;nbsp; count number should be "3-5"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 22:42:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747492#M234610</guid>
      <dc:creator>linda0910</dc:creator>
      <dc:date>2021-06-11T22:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747498#M234612</link>
      <description>&lt;P&gt;Is this supposed to be a data set as a result? If so, show what the entire data set result should look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A bit of hint as to what we are actually counting and the rules involved would be nice, such as which specific VARIABLE .&lt;/P&gt;
&lt;P&gt;"1-3" is not a number (unless resolved to -2 or similar). 3 is a number.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 23:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747498#M234612</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-11T23:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747506#M234615</link>
      <description>Sorry, if I confused you.&lt;BR /&gt;&lt;BR /&gt;This is the dataset.&lt;BR /&gt;&lt;BR /&gt;But for ID=1 the final outcome would be first consecutive “yes “ starting count at “1”, and ending “3”.&lt;BR /&gt;&lt;BR /&gt;For ID=2, the first consecutive “yes” staring at count=3 and ending at count=5</description>
      <pubDate>Sat, 12 Jun 2021 00:41:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747506#M234615</guid>
      <dc:creator>linda0910</dc:creator>
      <dc:date>2021-06-12T00:41:50Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747509#M234616</link>
      <description>&lt;P&gt;Well, this may not be my most polished program, but it is producing the desired results.&amp;nbsp; I wrote it in a hurry.&amp;nbsp; I would want to test it with a lot more test data before using it for something important.&lt;/P&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1623460461017.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60298i65DBAB7661F3737C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1623460461017.png" alt="jimbarbour_0-1623460461017.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data	Have;
	INPUT
		ID		$
		count	$
		keep	$
		;

DATALINES;
1  1  Yes
1  2  Yes
1  3  Yes
1  4  No
1  5  No
1  6  Yes
1  7  Yes
2  1  No
2  2  No
2  3  Yes
2  4  Yes
2  5  Yes
2  6  No
;
RUN;

PROC	SORT	DATA=Have;
	BY	ID	Count;
RUN;

DATA	Want;
	DROP	_:	Count	Keep;
	RETAIN	_Prior_Was_Yes	0;
	RETAIN	_ID_Processed	0;
	RETAIN	_New_For_ID		1;
	RETAIN	_First_Yes		0;
	RETAIN	_Last_Yes		0;

	SET	Have	END						=	_End_of_File;
		BY	ID;

	LENGTH	Count_Number	$8;

	IF	First.ID									THEN
		DO;		
			_New_For_ID					=	1;
			_ID_Count					=	0;
			_Counter					=	1;
		END;

	_ID_Count							+	1;

	IF	_ID_Processed								THEN
		DO;
			IF	Last.ID								THEN
				DO;
					_Prior_Was_Yes		=	0;
					_ID_Processed		=	0;
					_New_For_ID			=	1;
				END;
			DELETE;
		END;

	IF	UPCASE(Keep)					=	'YES'	THEN
		_Current_Is_Yes					=	1;

	IF	_N_								=	1		THEN
		IF	_Current_Is_Yes							THEN
			DO;
				_First_Yes				=	1;
				_Last_Yes				=	1;
				IF	Last.ID							THEN
					DO;
						LINK	Write_Record;
						_Prior_Was_Yes	=	0;
						_ID_Processed	=	0;
						_New_For_ID		=	1;
						_First_Yes		=	0;
						_Last_Yes		=	0;
					END;
				ELSE
					DO;
						_Prior_Was_Yes	=	1;
						_First_Yes		=	1;
					END;
				DELETE;
			END;
		ELSE
			DO;
				_Prior_Was_Yes			=	0;
				DELETE;
			END;
	ELSE
	IF	_Current_Is_Yes								THEN
		IF	_Prior_Was_Yes							THEN
			DO;
				_Counter				+	1;
				IF	_New_for_ID						THEN
					DO;
						_New_for_ID		=	0;
						_First_Yes		=	_ID_Count	-	1;
					END;
				IF	Last.ID							THEN
					DO;
						_Last_Yes		=	_ID_Count;
						LINK	Write_Record;
						_Prior_Was_Yes	=	0;
						_ID_Processed	=	0;
						_New_For_ID		=	1;
					END;
				ELSE
					DO;
						_Prior_Was_Yes	=	1;
					END;
				DELETE;
			END;
		ELSE
			DO;
				IF	Last.ID							THEN
					DO;
						_First_Yes		=	0;
						_Last_Yes		=	0;
						LINK	Write_Record;
						_Prior_Was_Yes	=	0;
						_ID_Processed	=	0;
						_New_For_ID		=	1;
					END;
				ELSE
					DO;
						_Prior_Was_Yes	=	1;
					END;
				DELETE;
			END;
	ELSE
		IF	_Prior_Was_Yes							THEN
			DO;
				IF	Last.ID							THEN
					DO;
						IF	_Counter	&amp;gt;	1		THEN
							DO;
								_Last_Yes	=	_ID_Count	-	1;
							END;
						LINK	Write_Record;
						_Prior_Was_Yes	=	0;
						_ID_Processed	=	0;
						_New_For_ID		=	1;
					END;
				ELSE
					DO;
						IF	_Counter	&amp;gt;	1		THEN
							DO;
								_Last_Yes	=	_ID_Count	-	1;
							END;
						LINK	Write_Record;
						_Prior_Was_Yes	=	0;
						_ID_Processed	=	1;
						_New_For_ID		=	1;
					END;
				DELETE;
			END;
		ELSE
			DO;
				_Prior_Was_Yes			=	0;
				DELETE;
			END;
	******;
	RETURN;
	******;

	************;
	Write_Record:
	************;
		Count_Number					=	CATS(PUT(_First_Yes, 8.), '-', PUT(_Last_Yes, 8.));
		OUTPUT;
	******;
	RETURN;
	******;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jun 2021 01:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747509#M234616</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-12T01:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747511#M234618</link>
      <description>Thanks so much. It worked on my data, but for some of the observations, it count_number from 0-1. Since there is no "Zero" in the count, I guess there is just one "Yes".&lt;BR /&gt;&lt;BR /&gt;I do appreciate your help.</description>
      <pubDate>Sat, 12 Jun 2021 01:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747511#M234618</guid>
      <dc:creator>linda0910</dc:creator>
      <dc:date>2021-06-12T01:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747512#M234619</link>
      <description>&lt;P&gt;I just found if some cases have all the "Yes" from the first count till last, it would be deleted in the final output.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jun 2021 01:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747512#M234619</guid>
      <dc:creator>linda0910</dc:creator>
      <dc:date>2021-06-12T01:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747514#M234620</link>
      <description>&lt;P&gt;Seems simple enough.&amp;nbsp; Use NOTSORTED keyword in the BY statement so SAS will calculate FIRST.KEEP and LAST.KEEP flags for you.&amp;nbsp; Note the data needs to actually be sorted by ID and COUNT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use RETAIN to keep track of when the first and last count for the first YES group was.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $ count keep $ ;
cards;
1  1  Yes
1  2  Yes
1  3  Yes
1  4  No
1  5  No
1  6  Yes
1  7  Yes
2  1  No
2  2  No
2  3  Yes
2  4  Yes
2  5  Yes
2  6  No
3  1  No
4  1  Yes
;

data want;
  set have ;
  by id keep notsorted;
  retain first last ;
  length range $8 ;
  if first.id then call missing(first,last);
  if keep='Yes' then do;
    if not first then first=count;
    if last.keep and not last then last=count;
  end;
  if last.id then do;
    if first then range=catx('-',first,last);
    output;
  end;
  keep id range ;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    id    range

 1     1      1-3
 2     2      3-5
 3     3
 4     4      1-1

&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 Jun 2021 02:45:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747514#M234620</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-12T02:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747516#M234622</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/162964"&gt;@linda0910&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you post the cases that don't seem to be working right as Datalines?&amp;nbsp; Something like the below.&amp;nbsp; If you give me the cases, I can make a correction.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data	Have;
	INPUT
		ID		$
		count	$
		keep	$
		;

DATALINES;
1  1  Yes
1  2  Yes
1  3  Yes
1  4  No
1  5  No
1  6  Yes
1  7  Yes
2  1  No
2  2  No
2  3  Yes
2  4  Yes
2  5  Yes
2  6  No
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jun 2021 04:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747516#M234622</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-12T04:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747531#M234634</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id $ count keep $ ;
cards;
1  1  Yes
1  2  Yes
1  3  Yes
1  4  No
1  5  No
1  6  Yes
1  7  Yes
2  1  No
2  2  No
2  3  Yes
2  4  Yes
2  5  Yes
2  6  No
3  1  No
4  1  Yes
;
data temp;
 set have;
 by id keep notsorted;
 group+first.keep;
run;
proc sql;
create table want as
select id,catx('-',min(count),max(count)) as want length=10 from (
select *
 from temp 
  where keep='Yes'
    group by id
	  having group=min(group) )
group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 Jun 2021 11:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747531#M234634</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-12T11:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747550#M234646</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like both of your solutions better than my "sit down at the computer and bang out the first idea that comes to mind" program which is rather kludgey.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, with a larger set of data (see below), I didn't get the results that I wanted using your code.&amp;nbsp; Note that I switched Count to a numeric variable and that I add a numeric Sort_ID, both for sorting purposes.&amp;nbsp; I haven't played with it yet, but the change in ID, which is not in &lt;EM&gt;character&amp;nbsp;&lt;/EM&gt;sort order is likely futzing up the BY processing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data	Have;
	INPUT
		ID		$
		count	
		keep	$
		;

	Sort_ID	=	INPUT(ID, 8.);
DATALINES;
1  1  Yes
1  2  Yes
1  3  Yes
1  4  No
1  5  No
1  6  Yes
1  7  Yes
2  1  No
2  2  No
2  3  Yes
2  4  Yes
2  5  Yes
2  6  No
3  1  YES
3  2  YES
3  3  YES
3  4  YES
4  1  YES
4  2  NO
4  3  YES
4  4  YES
4  5  NO
5  1  NO 
5  2  YES
5  3  NO
5  4  YES
5  5  NO
5  6  NO
5  7  YES
5  8  YES
5  9  NO
5  10  YES
5  11  YES
5  12  YES
5  13  YES
6  1  NO
6  2  YES
6  3  yes
7  1  YES
7  2  NO
7  3  YES
8  1  NO
8  2  NO
8  3  YES
9  1  NO
10  1  yes
11  1  NO
11  2  YES
;
RUN;

PROC	SORT	DATA=Have;
	BY	Sort_ID	Count;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jun 2021 16:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747550#M234646</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-12T16:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747551#M234647</link>
      <description>&lt;P&gt;Linda:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an improved version of my program, below.&amp;nbsp; Try running your data through it and see if it gives you what you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO	Get_Var_By_Pointer(d, x1, y1, x2, op2, y2, x3, op3, y3, j);
	_p_					=	_n_	+	&amp;amp;j;

	IF	(1 		&amp;lt;= _p_ 	&amp;lt;=	_o_)	THEN
		DO;
			_Found		=	1;
			SET	&amp;amp;d(KEEP=&amp;amp;x1 &amp;amp;x2 &amp;amp;x3 	RENAME=(&amp;amp;x1=&amp;amp;y1 &amp;amp;x2&amp;amp;op2&amp;amp;y2 &amp;amp;x3&amp;amp;op3&amp;amp;y3))	POINT=_p_	NOBS=_o_;
		END;
	ELSE
		DO;
			_Found		=	0;
		END;
%MEND	Get_Var_By_Pointer;

DATA	Want;
	Drop	_:	Count	Keep	Sort_ID;
	RETAIN	_ID_Processed						0;
	RETAIN	_ID_Count							0;
	_First_Yes								=	0;
	_Last_Yes								=	0;
	LENGTH	_N_Keep								$8.;

	SET	Have	END							=	_End_of_File;
		BY	Sort_ID;

	LENGTH	Count_Number						$8.;

	IF	LAST.Sort_ID									AND
		NOT	_ID_Processed								THEN
		DO;
			Count_Number					=	CATX('-', '0', '0');
			OUTPUT;
		END;

	IF	First.Sort_ID									THEN
		DO;
			_ID_Count						=	0;
			_ID_Processed					=	0;
		END;

	IF	_ID_Processed									THEN
		DO;
			DELETE;
		END;
	ELSE
		_ID_Count							+	1;

	IF	UPCASE(Keep)						=	'YES'	THEN
		DO;
			_Counter						=	1;
			_First_Yes						=	_ID_Count;
			_Last_Yes						=	0;
			_j								=	1;
			_Found							=	0;
			_N_Keep							=	'';
			_ID_Processed					=	0;
			DO	UNTIL	(NOT	_Found					OR
						ID					^=	_N_ID	OR
						UPCASE(_N_Keep)		^=	'YES');
				IF	UPCASE(_N_Keep)			=	'YES'	THEN
					DO;
						_Counter			+	1;
					END;
				%Get_Var_By_Pointer(Have, ID, _N_ID, Count, %STR(=), _N_Count, Keep, %STR(=), _N_Keep, _j);
				_j							+	1;
			END;
			IF	_Counter					&amp;gt;	1		THEN
				DO;
					_Last_Yes				=	_ID_Count	+	_Counter	-	1;
					Count_Number			=	CATS(PUT(_First_Yes, 8.), '-', PUT(_Last_Yes, 8.));
					_ID_Processed			=	1;
					OUTPUT;
				END;
			ELSE
				DO;
					_Counter				=	0;
					_j						=	1;
					_First_Yes				=	0;
					_Last_Yes				=	0;
				END;
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Sat, 12 Jun 2021 16:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747551#M234647</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-06-12T16:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: count consecutive occurrence of same value within group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747610#M234686</link>
      <description>You have messy data ,   Yes YES yes ...&lt;BR /&gt;So change my code this could handle this :&lt;BR /&gt;  where lowcase(keep)='yes'&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 13 Jun 2021 12:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-consecutive-occurrence-of-same-value-within-group/m-p/747610#M234686</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-06-13T12:05:52Z</dc:date>
    </item>
  </channel>
</rss>

