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;
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.
Can you please show the full code that stop run the program ? (About send email I know how to do it)
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
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;
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();
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.