<?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 to start a loop from mid-array? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/747302#M234526</link>
    <description>&lt;P&gt;how about this code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input a1-a9;
datalines;
1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1
;
run;

data want;
  set have;
  length result $200;
  array a{9};
  idx1=1;
  idx2=1;
  do i=1 to dim(a);
    if a{i}=1 then do;
      result=catx(' ',result,catx('_',idx1,idx2));
      idx2+1;
    end;
    if a{i}=0 then do;
      result=catx(' ',result,0);
      if i&amp;gt;1 then do;
        if not a{i-1}=0 then do;
          idx1+1;
          idx2=1;
        end;
      end;
    end;
  end;
  drop idx: i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results is follow.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-06-11_19h43_19.png" style="width: 644px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60247i0FFD7D8F3988554C/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-06-11_19h43_19.png" alt="2021-06-11_19h43_19.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Jun 2021 10:44:30 GMT</pubDate>
    <dc:creator>japelin</dc:creator>
    <dc:date>2021-06-11T10:44:30Z</dc:date>
    <item>
      <title>How to start a loop from mid-array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/747296#M234524</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1 1 0 0 0 0 1 1 1 
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having some trouble trying to count the dummies as individual sequences seperated by the 0's (such as in the datalines above).&lt;/P&gt;&lt;P&gt;What I am trying to achieve, is have SAS count the dummies so that line 1 would count group 1 as having two elements, stop and group 2 as having three elements.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;I.e.: &lt;BR /&gt;1_1 1_2 0 0 0 0 2_1 2_2 2_3&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have tried to do this by generating the lines as arrays and looping through the groups with the "do until" function.&lt;/P&gt;&lt;P&gt;However, I am wondering if there is any do-loop function that would allow me to loop "from" a point mid-array, so that I could begin a second loop from "2_1"?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this makes sense, any help is hugely appreciated!&lt;/P&gt;&lt;P&gt;Thanks in advance. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 09:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/747296#M234524</guid>
      <dc:creator>MLautrup</dc:creator>
      <dc:date>2021-06-11T09:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to start a loop from mid-array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/747302#M234526</link>
      <description>&lt;P&gt;how about this code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input a1-a9;
datalines;
1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1
;
run;

data want;
  set have;
  length result $200;
  array a{9};
  idx1=1;
  idx2=1;
  do i=1 to dim(a);
    if a{i}=1 then do;
      result=catx(' ',result,catx('_',idx1,idx2));
      idx2+1;
    end;
    if a{i}=0 then do;
      result=catx(' ',result,0);
      if i&amp;gt;1 then do;
        if not a{i-1}=0 then do;
          idx1+1;
          idx2=1;
        end;
      end;
    end;
  end;
  drop idx: i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Results is follow.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2021-06-11_19h43_19.png" style="width: 644px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60247i0FFD7D8F3988554C/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-06-11_19h43_19.png" alt="2021-06-11_19h43_19.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 10:44:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/747302#M234526</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-06-11T10:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to start a loop from mid-array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/747374#M234560</link>
      <description>&lt;P&gt;You may want to consider a long formatted data instead, I suspect it will be more useful in the long run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input a1-a9;
	ID=_n_;
	datalines;
1 1 0 0 0 0 1 1 1
1 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1
1 1 1 1 1 1 0 1 1
;
run;

proc transpose data=have out=long;
	by ID;
	var a1-a9;
run;

data long_indexed;
	set long;
	by ID col1 notsorted;

	if first.ID then
		do;
			group_cnt=0;
			sub_index=0;
		end;

	if first.col1 and col1 then
		do;
			group_cnt+1;
			sub_index=0;
		end;
	sub_index+1;

	if col1=1 then
		index=catx("_", group_cnt, sub_index);
	else
		index=0;
run;

proc transpose data=long_indexed;
by ID;
var index;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Jun 2021 15:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/747374#M234560</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-11T15:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to start a loop from mid-array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/748079#M234901</link>
      <description>That did the trick, thank you so much!&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Jun 2021 10:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-start-a-loop-from-mid-array/m-p/748079#M234901</guid>
      <dc:creator>MLautrup</dc:creator>
      <dc:date>2021-06-15T10:06:17Z</dc:date>
    </item>
  </channel>
</rss>

