Date: Wed, 29 Sep 2004 15:35:43 -0400
Reply-To: Michael Raithel <michaelraithel@WESTAT.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Michael Raithel <michaelraithel@WESTAT.COM>
Subject: Re: Replace GOTO loop?
Ben Powell posted the following:
> I have the following abomination in some code that I have
> been forced to use having failed to get the required
> functionality from and do loops or if
> data _NULL_;
> if symget('sumx') = 0 then goto exe;
> else if symget('sumx') > 0 then goto err;
> else call execute('%bkm_check;');
> goto start;
> err: call execute('%put WARNING: Unexpected result in check
> macro;'); goto xyz;
> exe: call execute('data &importdataset;set bkm_check;run;');
> goto xyz; xyz:; run;
> The execution of the macro bkm_check should eventually lead
> to the value of sumx being 0, although this may take several
> iterations - its checking to see when a remote file has
> stopped being written to and compares the recent and previous
> row count, completion being when the two counts are equal and
> the difference is zero.
> Obviously, with GOTO being the pariah statement (!) of
> programming I am loath to include any reference incase a
> future developer spurns my work.
> But all attempts thus far to use do-while have failed in a
> mire of non- execution and system hangs.
> For example, this doesn't work:
> data _NULL_;
> do while (symget('sumx') ^ = 0);
> call execute('%bkm_check;');
> call execute('data &importdataset;set bkm_check;run;');
> If someone could tell me why they don't expect the-do while
> above to work and perhaps suggest an alternative to the goto
> I would greatly appreciate it.
> Any comments much appreciated,
Ben, in the dim and distant past, I too was taught the abominability of the
"GOTO" statement and the salvation of Structured Programming. So, I'll be
glad to offer some advice on where you can go to in order to avoid GOTO's.
I would get around using the GOTO's by employing my old favorite: the LINK
statement. Here is how I might approach it, using your code, above:
if symget('sumx') = 0 then LINK exe;
else if symget('sumx') > 0 then LINK err;
else call execute('%bkm_check;');
err: call execute('%put WARNING: Unexpected result in check
exe: call execute('data &importdataset;set bkm_check;run;');
In the example, above, you LINK (travel to) to the ERR: "subroutine" when
your IF statement is true. After executing the CALL EXECUTE in the ERR:
subroutine, the STOP statement ends the execution of the current DATA step.
The same basic thing happens when you execute the EXE: subroutine, your
program execution LINKS (travels to) the EXE: subroutine.
So waddayouthink? This is a way to subroutine-ize your code so that future
reviewers can never accuse you of using the accursed "GOTO".
Ben, best of luck to you in your efforts to go to other SAS statements to
I hope that this suggestion proves helpful now, and in the future!
Of course, all of these opinions and insights are my own, and do not reflect
those of my organization or my associates. All SAS code and/or methodologies
specified in this posting are for illustrative purposes only and no warranty
is stated or implied as to their accuracy or applicability. People deciding
to use information in this posting do so at their own risk.
Michael A. Raithel
"The man who wrote the book on performance"
Author: Tuning SAS Applications in the MVS Environment
Author: Tuning SAS Applications in the OS/390 and z/OS Environments, Second
Currently Writing: The Complete Guide to Creating and Using SAS Indexes
(due Summer 2005)
A Diplomat is a person who can tell you to go to hell in such a way that you
actually look forward to the trip. - Caskie Stinnett