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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

EvanM
Calcite | Level 5

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.

 

ballardw
Super User

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;
EvanM
Calcite | Level 5

Cool ! Appreciate it !

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 832 views
  • 0 likes
  • 3 in conversation