BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I want to add condition to my code that If some conditions meet then stop run the program and send email with a message.

Specifically in this example : If number of observations in data set A <5 OR  number of observations in data set b<5 then stop run the program and send email "Program Stop running due to low number of obs"

What is the way to do it please?

Can you show the full code? thanks

 


/*conditionally stop SAS code execution*/
/*conditionally stop SAS code execution*/
/*conditionally stop SAS code execution*/
Data A;
Input ID Name$ Height;
cards;
1 A 1
3 B 2
5 C 2
7 D 2
9 E 2
;
run;

Data B;
Input ID Name$ Weight;
cards;
2 A 2
4 B 3
7 D 5
;
run;

proc sql noprint;
select count(*) as nr_A into :nr_A
from A
;
quit;
%put &nr_A;
proc sql noprint;
select count(*) as nr_B  into :nr_B
from B
;
quit;
%put &nr_B;


/*Here I want to add condition to code execution*/
/*IF &nr_A<5 OR &nr_B<5 then STOP execustion and send email "Program Stop running due to low number of obs"*/
proc sort data = a;
by id;
run;
proc sort data = b;
by id;
run;

data want;
merge a (in=x) b(in=y);
by id;
a = x;
b = y;
run;
5 REPLIES 5
Kurt_Bremser
Super User

Use either the ABORT data step statement, or the %ABORT macro statement.

Use the ABEND option, and specify a non-zero exit code to notify your scheduler about the condition, so it can react properly (schedulers can send notification emails).

 

Use the search option here in the communities, or do a Google search for "SAS communities send email from SAS". We have a ton of posts about this. 

Ronein
Meteorite | Level 14

Can you please show the full code that stop run the program ? (About send email I know how to do it)

Ronein
Meteorite | Level 14

I run this code  and it is working well (Program stop running as required) but why do I get an error??

 

Also where and how should I add the send email?

 

 

FILENAME mail EMAIL
TO=("ABC@gmail.com")
FROM='ABC@gmail.com>'
SUBJECT="Stop execute message "
CONTENT_TYPE="text/html" encoding="utf-8";
ODS LISTING CLOSE;
ODS HTML BODY=mail;

 

 

/*conditionally stop SAS code execution*/
/*conditionally stop SAS code execution*/
/*conditionally stop SAS code execution*/
Data A;
Input ID Name$ Height;
cards;
1 A 1
3 B 2
5 C 2
7 D 2
9 E 2
;
run;

Data B;
Input ID Name$ Weight;
cards;
2 A 2
4 B 3
7 D 5
;
run;

proc sql noprint;
select count(*) as nr_A into :nr_A
from A
;
quit;
%put &nr_A;
proc sql noprint;
select count(*) as nr_B  into :nr_B
from B
;
quit;
%put &nr_B;


/*Here I want to add condition to code execution*/
/*IF &nr_A<5 OR &nr_B<5 then STOP execustion and send email "Program Stop running due to low number of obs"*/
%macro runquit;
; run; quit;
%if &nr_A<5 OR &nr_B<5  %then %do;
%abort;
%end;

%else %Do;
proc sort data = a;
by id;
run;
proc sort data = b;
by id;
run;

data want;
merge a (in=x) b(in=y);
by id;
a = x;
b = y;
run;
%end;
%mend runquit;
%runquit
Kurt_Bremser
Super User

Since SAS 9.4, you do not need a macro definition for a simple %IF-%THEN.

You can do this in "open code":

%if &nr_A. < 5 or &nr_B. < 5
%then %do;
  /* put your email code here */
  %abort abend;
%end;
Patrick
Opal | Level 21

Below one option if you just want to send the email and then skip any subsequent statements.

Data A;
  Input ID Name$ Height;
  cards;
1 A 1
3 B 2
5 C 2
7 D 2
9 E 2
;
run;

Data B;
  Input ID Name$ Weight;
  cards;
2 A 2
4 B 3
7 D 5
;
run;

data _null_;
  call symputx('nr_a',nobs_a);
  call symputx('nr_b',nobs_b);
  stop;
  set a nobs=nobs_a;
  set b nobs=nobs_b;
run;

%put &=nr_a &=nr_b;

%macro demo();
  %if &nr_A<5 OR &nr_B<5 %then
    %do;
      /* here your send email code */
      
      /* goto end of program */
      %goto done;

    %end;

  proc sort data = a;
    by id;
  run;

  proc sort data = b;
    by id;
  run;

  data want;
    merge a (in=x) b(in=y);
    by id;
    a = x;
    b = y;
  run;

  %done:

%mend;
%demo();

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1023 views
  • 0 likes
  • 3 in conversation