Solved
New Contributor
Posts: 3

# Count a value _ Conditional loop

Hello!

I have got the following code to generate 20 random integers between 1 and 10.

data Unif(keep=u x);
call streaminit(123);
do i = 1 to 20;
u = rand("Uniform");
x = ceil( 10*u );

output;
end;
run;

What I would like to do is that, once a number is generated for the 3rd time, leave the loop and output the numbers generated.

Any suggestions on how to realize the function? Thanks in advance.

I am using Enterprise Guide 7.12 btw.

Accepted Solutions
Solution
‎11-15-2017 01:13 PM
Super User
Posts: 13,338

## Re: Count a value _ Conditional loop

You may need to clarify the requirement.

This will stop on the first value of X repeated 3 times.

```data Unif(keep=u x);
call streaminit(123);
array t {10} _temporary_ (0,0,0,0,0,0,0,0,0,0) ;
do while (1=1);
u = rand("Uniform");
x = ceil( 10*u );
t[x]= t[x]+1;
output;
if t[x]=3 then leave;
end;
run;```

All Replies
Super User
Posts: 9,437

## Re: Count a value _ Conditional loop

[ Edited ]

Its not clear to me why.  If you only want 3 iterations then set the do loop to maximum 3 iterations.  You can control the loop with:

https://blogs.sas.com/content/iml/2017/03/15/leave-continue-sas.html

However why, if you know how many iterations, just fix it?  To me, I can't see a reason to ever use these, as they effectively obfuscate the code around them.

New Contributor
Posts: 3

## Re: Count a value _ Conditional loop

Sorry I thought I made it clear.

I don't know how many iterations it will take. I just want it to stop once ANY number is drawn 3 times. not randomly generate 3 numbers.

For example, 1, 2, 3    continue loop because no number is generated for 3 times.

1, 2, 3, 1, 2, 3,  continue loop because no number is generated for 3 times.

1, 2, 3, 1, 2, 3,1, leave because number 1 is drawn 3 times.

The only chance it takes 20 iterations is each number (from 1 to 10) is generated 2 times. And then for the 21st iteration no matter which number it generates it will be the 3rd appearance so exit the loop. That's why I set the loop at 20 iterations.

Solution
‎11-15-2017 01:13 PM
Super User
Posts: 13,338

## Re: Count a value _ Conditional loop

You may need to clarify the requirement.

This will stop on the first value of X repeated 3 times.

```data Unif(keep=u x);
call streaminit(123);
array t {10} _temporary_ (0,0,0,0,0,0,0,0,0,0) ;
do while (1=1);
u = rand("Uniform");
x = ceil( 10*u );
t[x]= t[x]+1;
output;
if t[x]=3 then leave;
end;
run;```
New Contributor
Posts: 3

## Re: Count a value _ Conditional loop

Cool ! Appreciate it !

☑ This topic is solved.