Date: Thu, 13 May 2010 10:19:30 -0500
Reply-To: Joe Matise <snoopy369@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Joe Matise <snoopy369@GMAIL.COM>
Subject: Re: Alternative for "goto" in do loop
In-Reply-To: <379A927A452F3D43A3C8705F4E67905F0FBB0A081C@EX05.net.ucsf.edu>
Content-Type: text/plain; charset=ISO-8859-1
Certainly effective. Also could use STOP or ABORT to exit the data step,
although neither causes the data step to not write out the current dataset.
I was somewhat surprised that there wasn't an option in the ERROR statement
to cause just that, but I haven't seen one.
-Joe
On Wed, May 12, 2010 at 6:06 PM, Anderson, James <James.Anderson@ucsf.edu>wrote:
> Hi Chang,
>
> Following Joe's suggestion:
>
> data four;
> set sashelp.class;
> if age < 14 then error "age error";
> else if height < 63 then error "height error";
> else if sex ^= "F" then error "sex error";
> else if not ("A"<=substr(name,1,1)<="S") then error "last name error";
> else do;
> put name=;
> output;
> end;
> run;
>
> Jim
>
> -----Original Message-----
> From: Chang Chung [mailto:chang_y_chung@HOTMAIL.COM]
> Sent: Wednesday, May 12, 2010 2:23 PM
> To: SAS-L@LISTSERV.UGA.EDU; Anderson, James
> Subject: [SPAM] Re: Alternative for "goto" in do loop
> Importance: Low
>
> On Wed, 12 May 2010 11:38:42 -0700, Anderson, James
> <James.Anderson@UCSF.EDU> wrote:
> >Michael,
> >
> >Thanks for the wonderful historical perspective. My admiration for Knuth
> is even higher than my admiration for Dijkstra, but I think that this is an
> argument where history shows Dijkstra won. Please, can you give a SAS
> example where you "really, really needed" goto?
> ...
> Hi, Jim:
> I am obviously not Mikeeeeee, but I find that data step two below is easier
> for me to read (and write) then the data step one, don't you? HTH.
> Cheers,
> Chang
>
> data one;
> set sashelp.class;
> if age >= 14 then do;
> if height >= 63 then do;
> if sex = "F" then do;
> if "A"<=substr(name,1,1)<="S" then do;
> put name=;
> output;
> end; else do;
> put "last name error occurred";
> end;
> end; else do;
> put "sex error occurred";
> end;
> end; else do;
> put "height error occurred";
> end;
> end; else do;
> put "age error occurred";
> end;
> run;
>
> data two;
> length errType $9;
> drop errType;
> set sashelp.class;
> if age < 14 then do;
> errType="age"; goto err;
> end;
> if height < 63 then do;
> errType="height"; goto err;
> end;
> if sex ^= "F" then do;
> errType="sex"; goto err;
> end;
> if not ("A"<=substr(name,1,1)<="S") then do;
> errType="last name"; goto err;
> end;
> put name=;
> output;
> return;
> err:
> put errType $ " error occurred";
> run;
>
> /* on log
> sex error occurred
> age error occurred
> age error occurred
> height error occurred
> sex error occurred
> age error occurred
> age error occurred
> height error occurred
> age error occurred
> age error occurred
> age error occurred
> Name=Judy
> age error occurred
> Name=Mary
> sex error occurred
> age error occurred
> sex error occurred
> age error occurred
> sex error occurred
> */
>
|