<?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: How do I count maximum consecutive 1:s? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773396#M245691</link>
    <description>Thank you so much for helping me. I have finished working for the day but will try it out tommorow!</description>
    <pubDate>Mon, 11 Oct 2021 12:46:45 GMT</pubDate>
    <dc:creator>vanja</dc:creator>
    <dc:date>2021-10-11T12:46:45Z</dc:date>
    <item>
      <title>How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773359#M245669</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;I want to count the maximum number of consecutive 1:s in my dataset. See example of have and want dataset. My dataset(s) have millions of rows so I want to avoid transposing the data. I have seen similar solutions of this problem here but I don't quite understand them. Is there someone who can help me with the code needed to calculate the variable maxYears?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
	input id yr2010-yr2019; 
	datalines; 
		 1 . . . . . . . . 1 1
		 2 1 1 1 . . . 1 1 1 1
		 3 1 1 1 1 1 . . . 1 1
		 4 . . . . . . 1 . . .
		; 
run; 

data want; 
	input id yr2010-yr2019 maxYears; 
	datalines; 
		 1 . . . . . . . . 1 1 2
		 2 1 1 1 . . . 1 1 1 1 4
		 3 1 1 1 1 1 . . . 1 1 5
		 4 . . . . . . 1 . . . 1
		; 
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Oct 2021 11:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773359#M245669</guid>
      <dc:creator>vanja</dc:creator>
      <dc:date>2021-10-11T11:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773364#M245672</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I have seen similar solutions of this problem here but I don't quite understand them.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Did you try them? Do they work? Don't make us re-invent a solution if there already is a solution (which we could then explain to you).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a solution that could be explained:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Counting-groups-of-consecutive-characters-in-string/m-p/673498" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Counting-groups-of-consecutive-characters-in-string/m-p/673498&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 11:35:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773364#M245672</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-11T11:35:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773370#M245676</link>
      <description>&lt;P&gt;Sorry, but I can't translate the similar solutions I have found to my specific problem.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 11:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773370#M245676</guid>
      <dc:creator>vanja</dc:creator>
      <dc:date>2021-10-11T11:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773393#M245689</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
	input id yr2010-yr2019; 
	datalines; 
		 1 . . . . . . . . 1 1
		 2 1 1 1 . . . 1 1 1 1
		 3 1 1 1 1 1 . . . 1 1
		 4 . . . . . . 1 . . .
		;

data want;
 set have;
 temp=cats(of yr:);
pid=prxparse('/1+/');
s=1;e=length(temp);
call prxnext(pid,s,e,temp,p,l);
do while(p&amp;gt;0);
 maxYears=max(maxYears,l);
 call prxnext(pid,s,e,temp,p,l);
end;
drop pid s e p l temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Oct 2021 12:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773393#M245689</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-11T12:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773396#M245691</link>
      <description>Thank you so much for helping me. I have finished working for the day but will try it out tommorow!</description>
      <pubDate>Mon, 11 Oct 2021 12:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773396#M245691</guid>
      <dc:creator>vanja</dc:creator>
      <dc:date>2021-10-11T12:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773412#M245698</link>
      <description>&lt;P&gt;A straightforward way:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 array yrs {2010:2019} yr2010 - yr2019;
 maxyears = 0;
 count = 0;
 do _n_=2010 to 2019;
   if yrs{_n_} = 1 then count + 1;
   else do; &lt;BR /&gt;     maxyears = max(maxyears, count);
     count = 0;
   end;
 end;
 maxyears = max(maxyears, count);
 drop count;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Oct 2021 14:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773412#M245698</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-10-11T14:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773531#M245750</link>
      <description>&lt;P&gt;If the absent years really are always represented with a standard missing value, then your could&amp;nbsp; (1) concatenate all the year values into a character string, call it _STRNG, (2) calculate the length of each "word" in _STRNG, where a word is a substring containing no separators (no "." in this case):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;

  length _strng $10;
  _strng=cats(of yr:);
  if findc(_strng,'1') then do _w=1 to countw(_strng,'.');
    maxyears=max(maxyears,length(scan(_strng,_w)));
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Oct 2021 02:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773531#M245750</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-10-12T02:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773535#M245753</link>
      <description>&lt;P&gt;Similar to what others already suggested with some logic added to derive the start and stop year for the longest period.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
	input id yr2010-yr2019; 
  length str $10 word $10;
  array yrs {*} yr2010-yr2019;

  /* find longest string */
  str=cats(of yr:);
  do i=1 by 1;
    word=scan(str,i,,'kd');
    if missing(word) then leave;
    max_len=max(max_len,length(word));
  end;

  /* find start and stop variables (years) for longest period */
  start=find(str,repeat('1',max_len-1));
  stop=start+max_len-1;
  from_year =input(compress(vname(yrs[start]),,'kd'),best32.);
  to_year   =input(compress(vname(yrs[stop]),,'kd'),best32.);

  drop str word i start stop;

	datalines; 
1 . . . . . . . . 1 1
2 1 1 1 . . . 1 1 1 1
3 1 1 1 1 1 . . . 1 1
4 . . . . . . 1 . . .
; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 11:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773535#M245753</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-10-12T11:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773588#M245770</link>
      <description>&lt;P&gt;Thanks for this extra info with start and end years, could turn out to be really useful for this research project.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 11:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773588#M245770</guid>
      <dc:creator>vanja</dc:creator>
      <dc:date>2021-10-12T11:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773589#M245771</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/170717"&gt;@vanja&lt;/a&gt;&amp;nbsp;BTW: It wasn't in your sample data but what should happen if there is more than one max period? Which one would you want to select?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 11:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773589#M245771</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-10-12T11:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773595#M245777</link>
      <description>&lt;P&gt;I think&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp; 's code is better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
	input id yr2010-yr2019; 
	datalines; 
1 . . . . . . . . 1 1
2 1 1 1 . . . 1 1 1 1
3 1 1 1 1 1 . . . 1 1
4 . . . . . . 1 . . .
;

data want;
 set have;
 array yrs {*} yr2010 - yr2019;
 maxyears = 0;
 count = 0;
 do _n_=1 to dim(yrs);
   if yrs{_n_} = 1 then count + 1;
   else do;      maxyears = max(maxyears, count);
     count = 0;
   end;
 end;
 maxyears = max(maxyears, count);
 drop count;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Oct 2021 12:02:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773595#M245777</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-12T12:02:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773601#M245781</link>
      <description>You were the first to answer so I thought it was fair to give ju the role as solution programmer. Would you rather that I gave that to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; ? For other people that might have the same problem.</description>
      <pubDate>Tue, 12 Oct 2021 12:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773601#M245781</guid>
      <dc:creator>vanja</dc:creator>
      <dc:date>2021-10-12T12:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773604#M245783</link>
      <description>True, my real data will for sure have some observations where more periods will have same number of max years. That is a good question. I will have to look into that. Is there a way to flag them?</description>
      <pubDate>Tue, 12 Oct 2021 12:13:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773604#M245783</guid>
      <dc:creator>vanja</dc:creator>
      <dc:date>2021-10-12T12:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773616#M245790</link>
      <description>&lt;P&gt;If you need START and END years ,could try this too .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have; 
	input id yr2010-yr2019; 
	datalines; 
1 . . . . . . . . 1 1
2 1 1 1 . . . 1 1 1 1
3 1 1 1 1 1 . . . 1 1
4 . . . . . . 1 . . .
;

data want;
 set have;
 array x{*} yr: ;
 temp=cats(of x{*});
pid=prxparse('/1+/');
s=1;e=length(temp);
call prxnext(pid,s,e,temp,p,l);
do while(p&amp;gt;0);
 if maxYears&amp;lt;l then do;maxYears=l;start=vname(x{p}); end=vname(x{p+l-1});  end;
 call prxnext(pid,s,e,temp,p,l);
end;
drop pid s e p l temp;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Oct 2021 12:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773616#M245790</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-12T12:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773623#M245795</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/170717"&gt;@vanja&lt;/a&gt;&amp;nbsp;Define how such cases should be treated.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 12:43:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/773623#M245795</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-10-12T12:43:50Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/775796#M246632</link>
      <description>&lt;P&gt;Late homework, submitted for partial credit (less penalty for copying&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s homework). &lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=ones);
set have;
array y yr:;
do over y;
  ones=sum(ones,(y=1))*(y^=.);
  maxyears=max(maxyears,ones);
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Oct 2021 03:04:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/775796#M246632</guid>
      <dc:creator>tc</dc:creator>
      <dc:date>2021-10-22T03:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count maximum consecutive 1:s?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/775988#M246698</link>
      <description>&lt;P&gt;I see your array statement, and raise it by a repeat function (&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
  input id yr2010-yr2019; 
datalines; 
1 . . . . . . . . 1 1
2 1 1 1 . . . 1 1 1 1
3 1 1 1 1 1 . . . 1 1
4 . . . . . . 1 . . .
run; 

data want (drop=_:);
  set have;
  length _strng $10;
  _strng=cats(of yr:);
  do  LEN=length(_strng) to 1 by -1  while (find(_strng,repeat('1',LEN-1))=0);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Oct 2021 01:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-count-maximum-consecutive-1-s/m-p/775988#M246698</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-10-23T01:51:27Z</dc:date>
    </item>
  </channel>
</rss>

