LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous (more recent) messageNext (less recent) messagePrevious (more recent) in topicNext (less recent) in topicPrevious (more recent) by same authorNext (less recent) 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 09:04:29 -0400
Reply-To:   "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Subject:   Re: SAS datastep-looping problem

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