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.
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;
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.
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.
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;
Cool ! Appreciate it !
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.