```Date: Mon, 7 Nov 2011 14:14:35 -0800 Reply-To: "Schwarz, Barry A" Sender: "SAS(r) Discussion" From: "Schwarz, Barry A" Subject: Re: Data step question: why different results In-Reply-To: Content-Type: text/plain; charset="us-ascii" If the data step contains a set statement, it will always iterate after reaching the last statement. In scenario 1, the sequence is read observation 1 from have1 (x and y get set) output observation 1 to want (z missing as expected) assign new value to x and z output observation 2 to want (y unchanged as expected) iterate data step attempt to read observation 2 from have1 (no such observation) terminate data step In scenario 2, the sequence is assign values to x and z output observation 1 to want (y missing as expected) read observation 1 from have1 (x and y get set) output observation 2 to want (new values for x and y as expected) iterate assign values to x and z (note that z is reset, not retained) output observation 3 to want (y is retained) attempt to read observation 2 from have1 (no such observation) terminate data step Since you presented a "solution" without describing the requirement, you are not likely to get any suggestions regarding a general solution. > -----Original Message----- > From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Bian, > Haikuo > Sent: Monday, November 07, 2011 12:25 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Data step question: why different results > > Dear list, > > Question emerged when I tried to insert rows into SAS table using > datastep. > > /*original table*/ > data have1; > x=1; > y=2; > run; > > /*Scenario 1*/ > data want; > set have1; > output; > x=23; > z=34; > output; > run; > > proc print;run; > and I got the following: > > > Obs x y z > > 1 1 2 . > 2 23 2 34 > > This is understandable to me. > > Then I switched the order like this: > > /*Scenario 2*/ > data want; > x=23; > z=34; > output; > set have1; > output; > run; > > proc print;run; > > Then I got: > > Obs x z y > > 1 23 34 . > 2 1 34 2 > 3 23 34 2 > > > Question 1: Why the third record? Is it because Datastep will loop again > after reaching the end? See next scenario. > 2. Why value of variable z is automatically retained even > z is not from a dataset? > > > I can get what I expected by doing: > /*Scenario 3*/ > > data want; > if _n_=1 then do; > x=23; > z=34; > output; > end; > set have1; > output; > run; > > proc print;run; > > then I can get: > > Obs x z y > > 1 23 34 . > 2 1 34 2 > > > > Thank you all in advance! > > Haikuo > > ----------------------------------------- > Email messages cannot be guaranteed to be secure or error-free as > transmitted information can be intercepted, corrupted, lost, > destroyed, arrive late or incomplete, or contain viruses. The > Centers for Medicare & Medicaid Services therefore does not accept > liability for any error or omissions in the contents of this > message, which arise as a result of email transmission. > > CONFIDENTIALITY NOTICE: This communication, including any > attachments, may contain confidential information and is intended > only for the individual or entity to which it is addressed. Any > review, dissemination, or copying of this communication by anyone > other than the intended recipient is strictly prohibited. If you > are not the intended recipient, please contact the sender by reply > email and delete and destroy all copies of the original message. ```

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