Solved
Contributor
Posts: 35

Array Loop Help

Hey,

I'm trying to write a loop that will give me the count of zero's after a provided input variable. I keep getting an error when I try to write it. Any help would be much appreciated.

day1day2day3day4day5Start
111101
010012
111003

This is what I envision the loop to look like, but its not working.

data array_loop;

set in_data;

array test(*) day1-day5;

do i = 0 to (5 - start);

if test(start + i ) = 0 then counter +1;

end;

run;

Accepted Solutions
Solution
‎02-17-2014 03:56 PM
Super User
Posts: 8,115

Re: Array Loop Help

Looks like its working to me.

data array_loop;

set in_data;

array test(*) day1-day5;

do i = 0 to (5 - start);

if test(start + i ) = 0 then counter +1;

end;

put (day1-day5 start counter) (;

run;

1 1 1 1 0 1 1

0 1 0 0 1 2 3

1 1 1 0 0 3 5

Now because you are retaining your counter variable each new rows starts counting at the value retained from the previous row.

Did you want it to count each row separately?  Just at counter=0 before the DO loop.

All Replies
Posts: 5,532

Re: Array Loop Help

Try this:

data array_loop;

set in_data;

array test{*} day1-day5;

counter = 0;

do i = start to dim(test);

counter = counter + test{i}=0;

end;

drop i;

run;

PG

PG
Solution
‎02-17-2014 03:56 PM
Super User
Posts: 8,115

Re: Array Loop Help

Looks like its working to me.

data array_loop;

set in_data;

array test(*) day1-day5;

do i = 0 to (5 - start);

if test(start + i ) = 0 then counter +1;

end;

put (day1-day5 start counter) (;

run;

1 1 1 1 0 1 1

0 1 0 0 1 2 3

1 1 1 0 0 3 5

Now because you are retaining your counter variable each new rows starts counting at the value retained from the previous row.

Did you want it to count each row separately?  Just at counter=0 before the DO loop.

Frequent Contributor
Posts: 83

Re: Array Loop Help

It may also make sense to use this:

do i = 0 to (dim(test)-start);

Instead of the 5-start so you don't have to count the number of rows, the inherent reason why you use (*) rather than putting a 5 into the array statement.

🔒 This topic is solved and locked.