DATA Step, Macro, Functions and more

How to email and update in a single step using SAS?

Reply
Contributor
Posts: 20

How to email and update in a single step using SAS?

[ Edited ]

Hi all

 

I have the following code that sends email and updates a column (process_flag) to E after the email has been sent.

 

proc sql noprint;
Connect to Greenplm as dbcon (user=xxxxx pw=xxxxx server=xxxxx port=xxxxx database=xxxxxx);

SELECT * INTO :email_flg
FROM connection to dbcon (
SELECT count(1)::smallint
FROM f2_t1
WHERE rflag = 1 AND (process_flag <> 'E')
);
disconnect from dbcon;
quit;

%let email_body = Hello world;
%let to = aa@ss.com;
%let cc = ab@ss.com
%let subject_text = Test;


%if &email_flg > 0 %then %do;

filename outbox email &to;
data _null_;
file outbox
to=(&to)
cc=(&cc)
subject=&subject_text
;
put &email_body;
run;

%end;

 

proc sql;
Connect to xxxx as dbcon (user=xxx pw=xxx server=xxxx port=xxxx database=xxxxx);
execute ( update f2_t1 set process_flag = 'E' where run_flag = 1) by dbcon ;
disconnect from dbcon;
quit;

 

I am updating the process_flag to E once the email for the process has been sent. I create the email_flg to ensure that email doesn't get sent again.


But if the process fails after the email step and before the update statement, the email gets sent twice (or multiple times if the failure keeps occuring after the email step). Is there a way where it is possible to email and do the update in single transaction so that sending email fails if the update fails and vice versa? Or is there any other way to accomplish this scenario in SAS?

 

Thanks!

Trusted Advisor
Posts: 1,368

Re: How to email and update in a single step using SAS?

I cannot test it, but mainly I believe it is posiible.

I sugget to try it on a sample in work library;

 

FILENAME OUTBOX ...;

data <dsname>;

  modify <dsname>;

     if flag = 0 then do;

        flag = 1;

        file outbox ...;

              to = ...;

              cc = ....;

              subject = ....;

              put ....;

    

     end;

     if _iorc_ = 0 then replace;

run;

Ask a Question
Discussion stats
  • 1 reply
  • 269 views
  • 0 likes
  • 2 in conversation