🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Calcite | Level 5

## How to start a loop from mid-array?

``````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

``````

Hi!

I am having some trouble trying to count the dummies as individual sequences seperated by the 0's (such as in the datalines above).

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.

``I.e.: 1_1 1_2 0 0 0 0 2_1 2_2 2_3``

I have tried to do this by generating the lines as arrays and looping through the groups with the "do until" function.

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"?

I hope this makes sense, any help is hugely appreciated!

Thanks in advance. 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Rhodochrosite | Level 12

## Re: How to start a loop from mid-array?

``````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>1 then do;
if not a{i-1}=0 then do;
idx1+1;
idx2=1;
end;
end;
end;
end;
drop idx: i;
run;``````

Results is follow.

3 REPLIES 3
Rhodochrosite | Level 12

## Re: How to start a loop from mid-array?

``````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>1 then do;
if not a{i-1}=0 then do;
idx1+1;
idx2=1;
end;
end;
end;
end;
drop idx: i;
run;``````

Results is follow.

Calcite | Level 5

## Re: How to start a loop from mid-array?

That did the trick, thank you so much!
Super User

## Re: How to start a loop from mid-array?

You may want to consider a long formatted data instead, I suspect it will be more useful in the long run.

``````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;``````
Discussion stats
• 3 replies
• 481 views
• 1 like
• 3 in conversation