Date: Mon, 15 Sep 2008 09:32:12 -0400
Reply-To: Ed Heaton <EdHeaton@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ed Heaton <EdHeaton@WESTAT.COM>
Subject: Re: why this "DO WHILE NOT" cause infinite loop?
In-Reply-To: <200809141741.m8EAlPdF015937@malibu.cc.uga.edu>
Content-Type: text/plain; charset="us-ascii"
Jerry;
Consider this analogy:
Take an apple from the basket while there is more than one apple in the basket. Stop when the basket is empty!
That's what you are doing.
SAS has a built in way to stop the implied loop. That is, it tries to read beyond the end of the input dataset. You never reach the end of that dataset because you read observations only if they are not the last observation.
Ed
Edward Heaton, Senior Systems Analyst,
Westat (An Employee-Owned Research Corporation),
1650 Research Boulevard, TB-286, Rockville, MD 20850-3195
Voice: (301) 610-4818 Fax: (301) 294-2085
mailto:EdHeaton@Westat.com http://www.Westat.com
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Jerry
Sent: Sunday, September 14, 2008 1:41 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: why this "DO WHILE NOT" cause infinite loop?
Dan,
I appreciate your explanation.
But, I think last.sex is NOT initialized to 1 at the beginning. It's initialized to 0 instead. Only when the data step reads in the record of "name=Mary sex=F" does last.sex change to 1. You can verify this by looking at the data TEST created by the codes below.
proc sort data=sashelp.class out=class; by sex name; run;
data test;
set class;
by sex name;
first_sex=first.sex;
last_sex=last.sex;
run;
So from my point of view, the DO loop is entered, and then stopped before reading in the record of "name=Mary sex=F"
I'm very confused....
I look forward to more explanations, and pointing out why my logic is wrong.
Thank you!
On Sat, 13 Sep 2008 22:52:05 -0700, Daniel Nordlund <djnordlund@VERIZON.NET>
wrote:
>> -----Original Message-----
>> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
>> Jerry
>> Sent: Saturday, September 13, 2008 7:00 PM
>> To: SAS-L@LISTSERV.UGA.EDU
>> Subject: why this "DO WHILE NOT" cause infinite loop?
>>
>> Hi,
>>
>> With the code below(DO UNTIL approach), I can produce the following 2
>> lines in the log
>>
>> Sex=F Name=Mary
>> Sex=M Name=William
>>
>> /*****DO UNTIL approach************/
>> proc sort data=sashelp.class out=class; by sex name; run;
>>
>> data test1;
>> do until (end1);
>> DO UNTIL (last.sex);
>> set class end=end1;
>> by sex;
>> end;
>> output;
>> put sex= name=;
>> end;
>> stop;
>> run;
>> /*****************/
>>
>>
>> But with the code (DO WHILE NOT approach) below, it cause an infinite
>> loop: produce the infinite lines of "Sex= Name=" in the log
>>
>> /******DO WHILE NOT approach***********/
>> proc sort data=sashelp.class out=class; by sex name; run;
>>
>> data test1;
>> do until(end2);
>> DO WHILE (NOT last.sex);
>> set class end=end2;
>> by sex;
>> end;
>> output;
>> put sex= name=;
>> end;
>> stop;
>> run;
>> /*****************/
>>
>> I'm using SAS 9.1.3 sp4.
>>
>> Could anyone please explain why the infinite loop is happening with
>> the DO WHILE NOT approach? I thought DO WHILE NOT is equivalent to DO
>> UNTIL.
>>
>> Thanks.
>
>DO WHILE NOT is not equivalent to DO UNTIL because the DO WHILE
>condition
is tested at the top of the loop, while the DO UNTIL is tested at the bottom of the loop. The reason you get an infinite loop is that the test of (NOT
last.sex) occurs at the beginning of the loop before any data is read. Last.sex is initialized to 1 at the beginning of the datastep so the DO WHILE condition, (NOT last.sex), evaluates as false and the DO loop is never entered. Since, last.sex always equals 1, no data is ever read, and end2 never becomes TRUE, so you loop forever.
>
>Hope this is helpful,
>
>Dan
>
>Daniel Nordlund
>Bothell, WA USA