BookmarkSubscribeRSS Feed
shellp55
Quartz | Level 8

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.

4 REPLIES 4
Reeza
Super User

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.

shellp55
Quartz | Level 8

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.

Reeza
Super User

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.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1934 views
  • 0 likes
  • 3 in conversation