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.
day1 | day2 | day3 | day4 | day5 | Start |
---|---|---|---|---|---|
1 | 1 | 1 | 1 | 0 | 1 |
0 | 1 | 0 | 0 | 1 | 2 |
1 | 1 | 1 | 0 | 0 | 3 |
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;
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.
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
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.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.