Help using Base SAS procedures

Abort Stop

Reply
Contributor
Posts: 38

Abort Stop

Hi,

I would like to stop all code from running if result is 0.

First I am creating data set with

Today = date() -1;

Then I am selecting MAX(date) from 2 diff sources, then combined all 3.

Last I am creating dataset like below.

I would like to Abort or stop all code after this if source1 or source2 contain 0


source 1


source 2


0


0

Thanks

Fred

Super User
Posts: 11,343

Re: Abort Stop

Posted in reply to fred_major

Are you running this in EG or base SAS? Batch or interactive session?

Do you want to end the SAS session or provide diagnostics or some output to indicate this condition?

It may be as easy as (in the data step that combines the sources)

if source1=0 and source2=0 then abort(cancel);

Contributor
Posts: 38

Re: Abort Stop

Hi,

Base SAS

I would like to end session, below is my code.

data
today;

Today = date()-1;

put
Today=
date.;

run;

  

proc sql;

create table
temp_date
as

select 'date'as date,(TODAY)format yymmdd10. as date

from today;

quit;

/* Max Date 1*/

proc sql exec;

connectto sqlservr as Sstats (server= myserver1 database =mydatabase1);

create table 1_maxdate as select Date,MAXDATE

from connection to sstats( SELECT 'date'as date,MAX(date) AS MAXDATE

FROM [dbo].[t_1ReportData]);

disconnect from sStats;

  quit;run;

      

/* Max Date 2 */

  proc sqlexec;

  connectto sqlservr as Sstats (server= myserver2 database=mydatabase2);

  create table 2_maxdate as select Date,MAXDATE

from connection to sstats( SELECT 'date'as date,MAX(rpt_date) AS MAXDATE

  FROM [dbo].[t_2ReportData]);

disconnect from sStats;

    quit;run;

proc sql;

create table 1_Maxdate as select  Date,DATEPART(MAXDATE)formatyymmdd10. as source1

from 1_Maxdate;

quit;

proc sql;

create table 2_Maxdate as  select  Date,DATEPART(MAXDATE)formatyymmdd10. as source2

from 2_Maxdate;

quit;

DATA temp;

SET TEMP_DATE 1_MAXDATE 2_MAXDATE;

RUN;

  

data Update_Status(drop = Date);

update
temp (obs=
0) temp;

by date;

run;

proc sql;

create table Error_Checking as

select case when date = source1 then '1'else '0' end as source1,

case when date = source2 then '1'else '0' end as source2

from Update_Status;

quit;

ABORT BEFORE PROC MEANS IF ABOVE SOURCE1 OR SOURCE2 RETURN 0

     /* Proc Means Results  */

      Proc Means noprint data = results.myData(where =(date >= &fromdate and date <= &todate));

     CLASS date  My1 My2 ;

     output out=My   Output SUM=;

      RUN;

I attempted to add what you suggested to Error_Checking but program continued on to process proc means.

Thanks Fred

Super User
Super User
Posts: 7,942

Re: Abort Stop

Posted in reply to fred_major

Personally I am quite against the abort abend type stopping.  Just conditionally execute the proc means e.g:

data _null_;

     set Error_Checking (obs=1); /* will only do the next code if data is present, and will only do it once max */

     call execute('Proc Means noprint data = results.myData(where =(date >= &fromdate and date <= &todate));

                    CLASS date  My1 My2 ;

                    output out=My   Output SUM=;

                          RUN;');

run;

Contributor
Posts: 38

Re: Abort Stop

Hi RW9,

Would I be required to add a conditional execute to ever other step after the Proc means, as there are many?

Fred

Super User
Super User
Posts: 7,942

Re: Abort Stop

Posted in reply to fred_major

Well, you could, or you could put the actual code in another file or macro and then call that:

call execute('%include "real_code.sas";');

or call execute('%real_code();');

You could of course do:

call execute('abort; abend;');

However as mentioned I don't really like programs terminating abnormally.

Contributor
Posts: 38

Re: Abort Stop

Hi RW9,

I think I am missing something as this proc means still runs no matter if source1 or source2 contain 0, 1 or null?

data _null_;

     set Error_Checking (obs=1); /* will only do the next code if data is present, and will only do it once max */

     call execute('Proc Means noprint data = results.myData(where =(date >= &fromdate and date <= &todate));

                    CLASS date  My1 My2 ;

                    output out=My   Output SUM=;

                          RUN;');

run;

Fred

Contributor
Posts: 38

Re: Abort Stop

Posted in reply to fred_major

Hi RW9,

Thanks for your help on this, with a little modification I got this to do what I wanted.

Data
_null_ ;

  Set Error_Checking;

   if source1  = '1'then do;

   call execute('Proc Means noprint data = results.MyData(where=(date >= &fromdate and date <= &todate));

CLASS date My1 My2;

output out=My SUM=;RUN;');

                                END;

  

run;

Super User
Posts: 11,343

Re: Abort Stop

Posted in reply to fred_major

Where you have

ABORT BEFORE PROC MEANS IF ABOVE SOURCE1 OR SOURCE2 RETURN 0

data _null_;

     set Error_checking;

     if source1=0 or source2=0 then abort(abend);

run;

should stop the SAS session.

Note that I did ask about environment as the behavior of Abort changes

Super User
Posts: 10,023

Re: Abort Stop

endsas;

also can stop sas session .

Trusted Advisor
Posts: 3,212

Re: Abort Stop

Posted in reply to fred_major

You are right RW9, terminating a program using abort/abend is a bad approach.
That ones should be used with care as last rescue for very serious issues. Within a server-based approach (eip baf) it could cause unforeseen issues causing more problems.

The best way to code exceptions is by coding those exceptions.
Either by some macro-logic (common in DI generated code) or by some data step generating the additional code.

I have seen some questions that make sense, when the report is not wanted generate an other report that says the report was discarded.

That information that it is not there is also information example:

- this page was intentionally left blank= .      

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 10 replies
  • 553 views
  • 0 likes
  • 5 in conversation