Date: Mon, 20 Mar 2000 21:26:03 GMT sashole@mediaone.net "SAS(r) Discussion" Paul Dorfman Re: How to capture first and last non-blank element in dynamic array To: maurice.muoneke@TPWD.STATE.TX.US text/plain; format=flowed

Maurice,

This is usually done using a 'null-loop', i.e. a loop whose body executes nothing. Its purpose is only to stop where needed and return the stop-index. Consider burning the candle from both ends:

20 data _null_; 21 array a (10) \$1 ('','','','4','5','6','7','8','',''); 22 do fnbx=lbound(a) to hbound(a) by +1 while (a(fnbx) eq ''); end; 23 do lnbx=hbound(a) to lbound(a) by -1 while (a(lnbx) eq ''); end; 24 put fnbx= a(fnbx)= / lnbx= a(lnbx)=; 25 run; FNBX=4 A4=4 LNBX=8 A8=8

Of course, in this context, the clause

...while (a(...) eq '');

is equivalent to

until (a(...) gt '');

Also note that SAS allows to launch an infinite iterative loop by means of specifying BY value only (without TO), but then if all array elements are blank, the index will run out of bounds. It can be prevented by expanding the array by 1 dummy item to the left and one to the right and placing non-blank sentinels there, like in

50 data _null_; 51 array a (0:11) \$1 ('*','','2','','5','','7','8','9','','','*'); 52 do fnbx=lbound(a)+1 by +1 while (a(fnbx) eq ''); end; 53 do lnbx=hbound(a)-1 by -1 while (a(lnbx) eq ''); end; 54 put fnbx= a(fnbx)= / lnbx= a(lnbx)=; 55 run; FNBX=2 A3=2 LNBX=8 A9=9

Why bother? Because without the need to compare the index to the TO value in each iteration, the program will run faster, whilst the sentinels are guaranteed to terminate the loop.

HTH.

Kind regards, ====================== Paul M. Dorfman Jacksonville, Fl ======================

>From: Maurice Muoneke <maurice.muoneke@TPWD.STATE.TX.US> >Reply-To: Maurice Muoneke <maurice.muoneke@TPWD.STATE.TX.US> >To: SAS-L@LISTSERV.UGA.EDU >Subject: How to capture first and last non-blank element in dynamic array >Date: Mon, 20 Mar 2000 14:01:14 -0600 > >I have the following array definition: > >array FirstPA (*) vv1-vv45; > >first record looks something like 12 23 45 . . 56 11 . 19 etc. >the next record lioks like 14 23 125 . . etc > >what I want to do is loop through it, and capture the first non-balnk >element as well >as the last non-blank element in the array. Because this is a dymaic >array, >the records do not have the same number of elements. > >How do I do that? > >Your help is appreciated. >NED

______________________________________________________ Get Your Private, Free Email at http://www.hotmail.com

Back to: Top of message | Previous page | Main SAS-L page