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 (August 2005, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 12 Aug 2005 13:31:15 -0700
Reply-To:     Jim Simmons <emailjimsimmons@YAHOO.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Jim Simmons <emailjimsimmons@YAHOO.COM>
Subject:      Re: Data Step Question
In-Reply-To:  <07514e9557236f5cadd317e5291bcc6d@umich.edu>
Content-Type: text/plain; charset=iso-8859-1

Cameron Hooper <chooper@UMICH.EDU> wrote: Dear List

I've been playing around with the SAS data step and don't understand the following:

data samp; input x y; datalines; 34 2 2 87 97 12 ; run;

data samp1; do while (not eof); set samp end=eof; put "_N_ = " _N_ ; end; run;

proc print data = samp1; run;

This produces:

Obs x y

1 97 12 2 97 12

Why does it output only the last line of the input dataset, and why does it output this line twice? Inserting an output statement as in the code below produces "correct" output, but I am more interested in understanding why SAS behaves as it does in the above example. Thanks for any help.

Cameron

data samp1; do while (not eof); set samp end=eof; output; end; run;

Cameron,

The implicit output statement follows the 'end;' statement of your loop. So the output statement does not get executed until the loop is exited when eof=1. SAS then outputs the last obs it read and returns to the top of the data step. However, SAS cannot reenter the loop because eof=1. Since no SET statement is encountered, the variable values are unchanged when SAS subsequently hits the implicit output statement again at the end of this 2nd trip through the data set. This is the last trip through the data step because there is no SET statement outside the loop with obs remaining to be read. At least I think that's pretty close to an explanation.

Jim Simmons, Ph.D. Texas Education Agency Austin, Texas

"The Road Goes on Forever, The Party Never Ends . . ." Robert Earl Keen, Jr.


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