LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (October 2004, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Thu, 14 Oct 2004 11:19:28 -0400
Reply-To:     Howard Schreier <Howard_Schreier@ITA.DOC.GOV>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Howard Schreier <Howard_Schreier@ITA.DOC.GOV>
Subject:      Re: SAS datastep-looping problem

It looks like there is a bit more to the looping detenction heuristics. Consider

data test3; count + 1; if count=1 then do until (last); set sashelp.prdsale end=last; end; output; run;

COUNT is incremented, so the PDV changes state on each iteration. Yet looping is detected, correctly, and the step is halted.

The same thing happens in this variation;

data test4; count + 1; if count=1 then do until (last); set sashelp.prdsale end=last; end; output; if count=3 then stop; run;

In this case, the step would stop itself at the end of the third iteration, so there is no infinite loop. Yet the step is halted prematurely with two observations in TEST4.

On Thu, 14 Oct 2004 09:04:29 -0400, Richard A. DeVenezia <radevenz@IX.NETCOM.COM> wrote:

>Jesper Sahner wrote: >> Hi! >> >> Just a little teaser :-) >> >> Try the following: >> >> data test; >> do until (last); >> set sashelp.prdsale end=last; >> end; >> output; >> run; >> >> No problem. Now try this: >> >> data test2; >> retain first 1; >> if first then do; >> first=0; >> do until (last); >> set sashelp.prdsale end=last; >> end; >> end; >> output; >> run; >> >> This doesn't work. What is the EXACT explanation? > >Not exact, but briefly... > > >In test, in the second implicit data step loop (IDSL) iteration (_n_=2), the >SET statement is reached and the data set has no more rows to read, causing >the the DATA Step to end immediately (with only one record output at the end >of IDSL _n_=1). > >In test2, the 'if first' prevents the SET from occuring in the _N_=2 loop, >so the SET is never reached when it is exhausted, which means the step >cannot end at the _n_=2 SET. Instead, control flows past the "if then do" >block, to the OUTPUT, which creates the 2nd obs of test2. Control returns >back to the top IDSL. The data step automatically detects a 'looping' >condition, that is an infinite IDSL, which occurs when none of the PDV >variables change state (i.e. get a new value by some effect of the prior >_n_). When 'no changes' happening condition occurs, the data step ends >with a "NOTE: DATA STEP stopped due to looping." This is what happens in >test2 > >-- >Richard A. DeVenezia >http://www.devenezia.com


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