BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jsendzik
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

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
Tom
Super User Tom
Super User

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.

overmar
Obsidian | Level 7

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1034 views
  • 3 likes
  • 4 in conversation