Statistical programming, matrix languages, and more

Do until loop

Reply
Occasional Contributor
Posts: 5

Do until loop

Hello,

I am new to SAS and I would like to know how to tweak this program to break the infinite do until loop. At the end of the do until loop I would like to be able to print the updated phi. Thanks.

Attachment
Respected Advisor
Posts: 4,948

Re: Do until loop

With any DO UNTIL loop, it is possible that it never ends because the UNTIL condition never becomes true.  In your case, this condition might never become true:  (n < 0.001)

One way to guard against it is to allow two ways for the loop to end:

do k=1 to 100000 until (n < 0.001);

If the loop iterates 100,000 times, it will end regardless of the value of n.  Your subsequent statements might have to allow for the possibility that n > 0.001.  The loop will end before 100,000 iterations if the UNTIL condition becomes true.

Good luck.

Occasional Contributor
Posts: 5

Re: Do until loop

Thank you so much.

SAS Super FREQ
Posts: 3,383

Re: Do until loop

Welcome to SAS!  To break the infinite DO UNTIL loop, I often include a maximum number of iterations, like this:

maxIter = 50;

iter = 0;

do until(n<0.001 | iter>=maxIter);

   /* computations */

   n = sqrt(direc[##]);

   iter = iter + 1;

end;

If I might offer another suggestion, your computation is looping over the I variable 50,000 times and updating elements of vectors. It might be possible for you to eliminate that loop. For ideas, see

http://blogs.sas.com/content/iml/2013/05/15/vectorize-computations/

Occasional Contributor
Posts: 5

Re: Do until loop

Thank you so much.

Ask a Question
Discussion stats
  • 4 replies
  • 407 views
  • 6 likes
  • 3 in conversation