| Date: | Tue, 15 Mar 2005 18:27:12 -0000 |
| Reply-To: | Ian Wakeling <ian.wakeling@HANANI.QISTATS.CO.UK> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Ian Wakeling <ian.wakeling@HANANI.QISTATS.CO.UK> |
| Subject: | Re: How to detect an Error in an IML subroutine,
then exit IML gracefully? |
| Content-Type: | text/plain; charset="iso-8859-1" |
John,
Haven't got time to test this, but does the problem go away
if you sandwich all your IML commands inside start and finish
statements and then add a command to run the module?
Ian.
Ian Wakeling
Qi Statistics.
>
> Date: Tue, 15 Mar 2005 09:06:51 -0500
> From: John Hixon <john.hixon@KODAK.COM>
> Subject: How to detect an Error in an IML subroutine, then exit IML gracefully?
>
> Hi Folks,
>
> I used to have time to monitor the SAS-L list regularly, but...
> I realize I am missing out on the opportunity to grow
> as a SAS pgmer by not finding the time to scan the
> list, but...
>
> Right now, I have a problem that may have a simple
> solution, and I'm not seeing it? I'm hoping maybe
> some clever IML programmer has a work-around
> for me.
>
> SAS V9.12, Windows
>
> The following code snippet is from within a bigger macro.
> The problem arises because sometimes the CALL LTS
> subroutine fails, and I can not find a diagnostic to allow
> me to gracefully exit PROC IML. I work around this
> problem by detecting that the SC&i dataset does not
> exist after the Proc IML exits (with errors) and the macro
> runs fine, but...ONLY IN INTERACTIVE MODE.
>
> I have dozens of SAS Batch-mode tasks that run
> overnight. It was a real eye-opener to me how
> "picky" Batch mode execution is compared to
> interactive mode. The code below works fine
> in Interactive mode, but NOT IN BATCH MODE.
>
> The key issue is how can I tell that the CALL LTS
> subroutine failed and quit the IML Procedure?
>
> I've tried pre-defining the vectors that
> CALL LTS creates when it executes, hoping that
> I could detect that they were "still the same" as
> before the CALL LTS, but...they do not exist
> after CALL LTS exits with errors.
>
> The code below should allow you to reproduce
> the problem. Note that in INTERACTIVE mode
> I can handle the errors from Proc IML, but,
> in BATCH mode, the macro fails after the
> 1st time that the CALL LTS subroutine throws
> an error.
>
> *Make a sample dataset that has 3 params.
> Params 2 and 3 are OK, but param#1 will
> cause the Call LTS subroutine to fail;
> data raw;
> do nparam=1 to 3;
> do seq=1 to 100;
> if nparam=1 then do;
> Response=0;
> if mod(seq,10)=0 then Response=1;
> output;
> end;
> else do;
> Response=rannor(12345);
> output;
> end;
> end;
> end;
> run;
>
> * Here is a dummy macro just to illustrate
> the problem. In interactive mode I can
> trap the fact that Call LTS failed by
> looking for the existence of the output
> dataset that is created when the Call LTS
> executes successfully. If the dataset does
> not exist, then I know Proc IML exited with
> and error..
> BUT...in Batch mode, once the error occurs
> one time in Proc IML...all is lost for
> subsequent iterations through the macro.;
>
> %macro ShowProblem(start=1,stop=3);
> %do i=&start %to &stop;
> data _one;
> set raw(where=(nparam=&i));
> run;
> %let rc=%sysfunc(exist(work.SC&i));
> %if &rc=1 %then %do;
> proc datasets;
> delete SC&i;
> run;
> %end;
> proc iml;
> use _one;
> read all var ("Response") into b;
> read all var ("nparam") into nparam;
> read all var ("seq") into Seq;
> n=nrow(b);
> optn = j(8,1,.);
> CALL LTS(sc,coef,wgt,optn,b);
> wgt2=wgt`;
> Quantile=J(n,1,sc[1]);
> outlabel="nParam"||"Seq"||"OneZero"||"RdivS"||"Quantile";
> out=nparam||Seq||wgt2[,1:2]||Quantile;
> create outlier&i from out[colname=outlabel];
> append from out;
> create SC&i from SC;
> append from SC;
> quit;
> %let rc=%sysfunc(exist(work.SC&i));
> %if &rc=1 %then %do;
> *stuff for CALL LTS executed...;
> %put >>>> In Interactive mode I can catch the errors;
> %put >>>> in Call LTS and execute code to deal with it;
> title "Iteration &i, Fine and Dandy in Interactive Mode";
> title2 c=blue "CALL LTS executed successfully for Iteration &i";
> proc print data=_one (obs=1);
> run;
> %end;
> %else %do;
> *stuff for CALL LTS failed....;
> %put >>>> In Interactive mode I can catch the errors;
> %put >>>> in Call LTS and execute code to deal with it;
> title "Uh Oh, CALL LTS failed, so deal with it...";
> title "Iteration &i, Fine and Dandy in Interactive Mode";
> title2 c=blue "CALL LTS failed Iteration &i, (but I can detect it)";
> proc print data=_one (obs=1);
> run;
> %end;
> %end;
> %mend;
>
>
> *If you execute this code in INTERACTIVE mode,
> everything is fine (even with the errors in the log
> from the failure of CALL LTS).
>
> But, if you make a small batch file to execute
> this same code......the first time the error
> occurs in CALL LTS...you are lost. All further
> iterations fail as well (even if the data is
> 'OK' for CALL LTS.)
>
> So...how can I exit gracefully from Proc IML
> when CALL LTS fails?;
>
> %ShowProblem(start=1,stop=3);
>
>
> Note, to set up the batch file, make a text file with (typically) these
> statements:
>
> "C:\Program Files\SAS\SAS 9.1\sas.exe" -SYSIN c:\temp\testcode.sas
>
> and name the file xxxx.bat. Then double click on it to run.
>
> Does anyone have a cure for this? I'll bet there is a very simple
> way to detect an error from CALL LTS within Proc IML and then
> exit the Proc IML gracefully, but I just haven't found it.
>
> Note that the errors in BATCH execution have messages like this:
>
>
> "NOTE: SAS set option OBS=0 and will continue to check statements.
> This may cause NOTE: No observations in data set."
>
> [This is a killer error, since from this point on, EVERY dataset
> has 0 observations ! Yikes!]
>
> In iteration 2 of BATCH mode, I get this error as well:
>
> "NOTE: IML Ready
> WARNING: IML is now in syntax-check-only mode due to errors in previous
> steps. No statements
> will be executed.
> NOTE: Exiting IML."
>
> [Obviously not a good thing!]
>
> TIA.
>
> Best regards,
>
> John Hixon
> Eastman Kodak Company
> Rochester, NY USA
> 585-477-1984
>
|