BookmarkSubscribeRSS Feed
fred_major
Calcite | Level 5

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

10 REPLIES 10
ballardw
Super User

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);

fred_major
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

fred_major
Calcite | Level 5

Hi RW9,

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

Fred

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

fred_major
Calcite | Level 5

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

fred_major
Calcite | Level 5

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;

ballardw
Super User

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

Ksharp
Super User

endsas;

also can stop sas session .

jakarman
Barite | Level 11

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 --<-----

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1854 views
  • 0 likes
  • 5 in conversation