Help using Base SAS procedures

Do Loop Until Criteria is Met?

Reply
Regular Contributor
Posts: 164

Do Loop Until Criteria is Met?

Hello I have an array of procedures of up to 20 cases.  I want to loop through looking for specific codes and when found then I don't want to continue looping through.  The reason is because the first occurrence of the matching procedure is the one I want to use and there is a possibility another code could match it's not the one I want to evaluate. if ca_dx eq "N" and age>=18 and Adm= "L" then do i = 1 to 20 until (proccde = ''); if ooh ne "Y" and substr(proccde,1,5) in ("1SY80") and substr(proccde,6,2) ne "BA" and atribloc Not In ("0","B") and atribstat ne "A" then ventral_px=1; end; But really for the above I want it to stop at the first procedure that is "1SY80"....is there any way to do this?  Thanks.

Super User
Posts: 19,822

Re: Do Loop Until Criteria is Met?

Yes, by a few ways.

1. In your do loop add another condition which is a flag and when the flag is set it stops. Set the flag using a if/then/do when the first occurance is found.

e.g.

do while (original condition AND flag =0); *Watch your and/ifs

if proccde="ABC" then do;

flag=1;

index=i;

end;

end;

2. Change the way you search for the instance of procedure code using the whichc/whichn function instead.

Regular Contributor
Posts: 164

Re: Do Loop Until Criteria is Met?

Hi Reeza Thanks very much.  Sorry but I'm not understanding the first section.  So using my code, would it be:


do while cco eq "N" and age>=18 and Adm= "L" and flag=0;

if ooh ne "Y" and substr(proccde,1,5) in ("1NT86") and substr(proccde,6,2) ne "BA" and Type="DS" and atribstat ne "A" then do;

flag=1;

anoA_px=1;

There are many others and right now they are all within the do and end piece so is it still okay that I do that I can add the next one after the last line above?  Thanks.

Super User
Posts: 19,822

Re: Do Loop Until Criteria is Met?

I'm not 100% sure, depends on your logic Smiley Happy

I think you'll have to test it to see.

Essentially the idea is correct, except I think you do have to have the while condition in brackets, and make sure your end statements line up.

Super User
Super User
Posts: 7,060

Re: Do Loop Until Criteria is Met?

You could just use the LEAVE statement to exit the loop.

For example try this:

data _null_ ;

  do i=1 by 1 until (i>100) ;

    if i=10 then leave ;

  end;

  put i=;

run;

Ask a Question
Discussion stats
  • 4 replies
  • 595 views
  • 0 likes
  • 3 in conversation