Check updation and send mail - sas

I using index to avoid duplicates everytime dataset gets updated and send mail according its failure and success. Below is the code.
data lib.new(index =(dual=(name date)/ unique));
set lib.old;
run;
options emailsys=smtp;
%macro sendmail;
%if &syserr ne 0 %then %do;
data _null_;
filename mailbox EMAIL 'abc#gmail.com' subject ='Failed';
file mailbox;
put "Data already avaialable. Please submit for different";
run; %end;
%else %do; data _null_;
filename mailbox EMAIL 'abc#gmail.com' subject ='Success';
file mailbox;
put "Data created"; run; %end;
%mend sendmail;
%sendmail;
when I run above code. I can receive mail whenever data update is success. But when I am not receiving mail when it fails
Is there any other way to do.
For success. I am getting below log with email details and mail triggered.
SYMBOLGEN: Macro variable SYSERR resolves to 0
MACROGEN(SENDMAIL): data _null_;
MACROGEN(SENDMAIL): filename mailbox EMAIL 'abc#gmail.com' subject 'Success';
MACROGEN(SENDMAIL): file mailbox;
MACROGEN(SENDMAIL): put "Data created";
MACROGEN(SENDMAIL): run;
NOTE: The file MAILBOX is:
E-Mail Access Device
Message sent
To: "abc#gmail.com"
The SAS System
Cc:
Bcc:
Subject: Job Success
Attachments:
NOTE: 1 record was written to the file MAILBOX.
For failure. I am getting below log and mail not triggered
SYMBOLGEN: Macro variable SYSERR resolves to 1012
MACROGEN(SENDMAIL): data _null_;
MACROGEN(SENDMAIL): filename mailbox EMAIL 'abc#gmail.com' subject ='Job Failed';
MACROGEN(SENDMAIL): file mailbox;
The SAS System
MACROGEN(SENDMAIL): put "Data already available. Please submit for different";
MACROGEN(SENDMAIL): run;

The filename statement should be before a data step. Try this.
options emailsys=smtp;
%macro sendmail;
filename mailbox EMAIL 'abc#gmail.com';
data _null_;
file mailbox;
%if &syserr ne 0 %then %do;
put '!em_subject! "Failed"';
put "Data already avaialable. Please submit for different";
%end;
%else %do;
put '!em_subject! "Success"';
put "Data created";
%end;
run;
%mend sendmail;
%sendmail

Related

SAS Data Integration Studio: problems with sending mail (log in attach)

I'm an inexperienced user of SAS Data Integration Studio.
I would like to receive an e-mail information about the job status. Using "Status Handling" and action "Send Email" (from job options) is not satisfying to me. First of all - (propably?) i can't attach log to this e-mail.
In SAS Enterprise Guide I developed and tested code for sending email with log in attach and some useful informations (datetime, list of errors) in mail body:
%let mail = "test#test.com";
%let path_error = /home/ ... .log;
filename msg email
to = (&maile)
subject = "SAS Message Test"
attach = "&path_error.";
data _null_;
dttm = put(datetime(),nldatm.);
infile "&path_error.";
input;
file msg;
if _n_ = 1 then do;
put "Date time: " dttm;
put;
put "Full log in attach.";
put "There are some ERRORs and WARNINGs:";
put;
end;
if substr(_infile_,1,5) = "ERROR" then
put _infile_;
if substr(_infile_,1,7) = "WARNING" then
put _infile_;
run;
This code works fine - I get complete mail with list of errors and warnings. In log I can see:
NOTE: 268 records were read from the infile "[...].log"
But how to implement this code (especially reading by infile statement) in DIS?
I have modified the job options:
Precode - puts the log into an external file; log name contains jobname and datetime:
%let path = /home/[...]/log_&etls_jobName._%sysfunc(datetime(), datetime.).log;
proc printto log="&path.";
run;
Postcode - I used code from Enterprise Guide:
%let address = "test#test.com";
%let message = problems with &etls_jobName;
filename sendMail email
to= (&address)
subject= "ETL Process problem: &etls_jobName."
attach= "&path.";
options nosyntaxcheck;
data _null_;
dttm = put(datetime(),nldatm.);
infile "&path.";
input;
file sendMail;
if _n_ = 1 then do;
put "Date time: " dttm;
put;
put "Full log in attach.";
put "There are some ERRORs and WARNINGs:";
put;
end;
if substr(_infile_,1,5) = "ERROR" then
put _infile_;
if substr(_infile_,1,7) = "WARNING" then
put _infile_;
run;
In effect I get e-mail with log in attach, but empty body. In attached log I can see:
NOTE: 0 records were read from the infile
I've got some questions:
Why 0 records???
When I delete input; statement from Postcode and run job I get e-mail with "Date time / Full log in attach / There are some ERRORs and WARNINGs" in body. Why they are deleted where input works in code?
E-mail is not sending without options nosyntaxcheck; in code. Why?
Thanks for the answers.
Regards,
MichaƂ
The normal way for a data step to stop is when it reads past the end of the input (either raw data being read or datasets being accessed).
So move your _N_=1 block to BEFORE the INPUT statement. You might want to change the wording of the message about the errors and warnings. Or wait to write until after you know there are records to be read.
%let address = "test#test.com";
%let message = problems with &etls_jobName;
filename sendMail email
to=(&address)
subject="ETL Process problem: &etls_jobName."
attach="&path."
;
options nosyntaxcheck;
data _null_;
dttm = put(datetime(),nldatm.);
file sendMail;
if _n_ = 1 then do;
put "Date time: " dttm;
put;
put "Full log in attach.";
put "There are some ERRORs and WARNINGs:";
put;
end;
infile "&path.";
input;
if _n_ = 1 then do;
put "There are some ERRORs and WARNINGs:";
put;
end;
if substr(_infile_,1,5) = "ERROR" then put _infile_;
if substr(_infile_,1,7) = "WARNING" then put _infile_;
run;

SAS auto log clear with rsubmit

I used to add dm "out;clear;log;clear;"; to clear the log and prevent the code from pausing for input. However, now I am using WRDS remote connection. This line after rsubmit does not work and the I lost connection to the server because I was not by the computer when the log was full and needed for user input to be cleared. Is there a way to prevent the code from stopping? Here is what I am doing now.
options ls = 78 ps = 66;
********************connect to WRDs;***************************************;
%let wrds = wrds.utexas.edu 4016;options comamid = TCP remote=WRDS;
signon username=_prompt_;
*************************************************************************;
rsubmit;
libname qa"F:\research2\transcripts";
libname cq '/wrds/nyse/sasdata/taqms/cq';
proc upload data=qa.daylist out=daylist; run;
data daylist;set daylist;traday2 = input(put(traday,yymmddn8.),8.);drop traday;rename traday2=traday;run;
options errors=2;
data intraday;run;
%macro temp;
%do i = 1 %to 2215;
.......
dm "out;clear;log;clear;";
%end;
%mend;
%Temp;
One option (which avoids the need to clear the log) is to write the log to an external destination using proc printto (doc link). The syntax is:
proc printto log='/path/to/your.log';
run;

SAS %errmail macro

Although I have the error handling:
filename myfile email
to=&e_mail.
subject= "Error: &number."
type="text/plain";
%macro errmail;
%if &syserr ne 0 %then %do;
options obs= max replace nosyntaxcheck;
data _null_;
file myfile;
put;
put 'ERROR';
put "&syserrortext";
put 'check a log'
run;
%abort cancel;
%end;
%mend errmail;
when I have the error in the proc export:
(&number. is the table in the work)
proc export data=&number.
outfile="/usr/local/backup/&number./&number._%SYSFUNC(TODAY(),DATE9.).txt"
replace
dbms=dlm;
delimiter=';';
run;
%errmail;
ERROR: Physical file does not exist, /usr/local/backup/2116/2116_13MAY2016.txt.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.2116.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
SAS dosen't check a macro and doesn't send an e-mail. I have an error in the path, so I understand the error, but I need an e-mail about that and I want that SAS stops processing because of macro.
Adding some options to the macro can fix it?
this is how I send out my email from SAS
not required to create a file on the system
filename outbox email "email#address.com";
data _null_;
file outbox
to=("email#address.com")
cc=("email#address.com")
subject="SUBJECT OF EMAIL "
/* attach=("C:\sas\results.out" "C:\sas\code.sas") */ ; /* incase you need to attach a file */
put 'Hello! ';
put ' ';
put 'Thank you & let me know if there is anything,';
put "&macrovar."; /*in case you need to put some detail in the email from macro variables make sure to use double quote for that */
put 'SIGNATURE ME';
run;

SAS Check the directory exists

How can I check if directory exists and if not, it would be then: syserr <>0?, I need
%sysfunc(filename(fileref,&dir))
I need the syserr value if exists 0 if not <>0.
thanks for help
A cleaner method to check if a folder exists
%let does_it_exist=%sysfunc(fileexist(&dir));
This returns a value of 0 if the folder does not exist, and a value of 1 if the folder does exist.
There may be a cleaner way of getting SAS to throw an error, but the following worked for me. The general idea is that, if the directory exists, you do something that keeps syserr set to 0; if not, you do something that throws an error.
%let your_path = "...";
%macro your_macro(dir);
%let rc = %sysfunc(filename(fileref, &dir.));
%if %sysfunc(fexist(&fileref)) %then %do;
data _null_;
set _null_;
run;
%end;
%else %do;
data _null_;
set something_that_doesnt_exist;
run;
%end;
%put syserr = &syserr.;
%mend your_macro;
%your_macro(&your_path.);

A macro to send a email

my code to send mail using sas data step. here trying to create a code to send mail.
FILENAME outbox EMAIL ("***********");
DATA _NULL_;
FILE outbox
TO=("************")
FROM=("***********")
SUBJECT=("Example of a SAS E-mail" );
/* ATTACH=(""); */
PUT " ";
PUT "Hello Boss,";
PUT " ";
PUT "Attached are the Daily Operational Reports.";
PUT " ";
PUT "rrt";
RUN;
I don't tend to include email parameters in the datastep itself, but rather in fileref. I have tested the code below with my email address and it worked.
As I said in the comments, you simply need to wrap your datastep in a macro if you want it to run within macro.
You can have positional or keyword parameters. See this source for detailed info. I have used keyword parameters in this example. you call the macro by specifying the keyword and then the value like I showed the last line. If you don't put anything, then the keyword gets ignored.
By the way, the empty keywords result in initialised local macro variables. Let statements like ( %LET FROM=FROM="&FROM") are there to add a string "FROM=" to the beginning of "FROM" macro variable so the filename syntax would be complete. If macro variable such as "FROM" would be passed as is, it would resolve to email address that filename woudn't know what to do with.
%MACRO send_email (TO=,FROM=,SUBJECT=,ATTACHMENT=,BODY=);
%IF &TO ne %THEN
%LET TO="&TO";
%IF &FROM ne %THEN
%LET FROM=FROM="&FROM";
%IF &SUBJECT ne %THEN
%LET SUBJECT=SUBJECT="&SUBJECT";
%IF &ATTACHMENT ne %THEN
%LET ATTACHMENT=ATTACHMENT="&ATTACHMENT";
%IF &BODY ne %THEN
%LET BODY="&BODY";
FILENAME outbox EMAIL &TO &FROM &SUBJECT &ATTACHMENT;
DATA _NULL_;
FILE outbox;
PUT &BODY;
RUN;
%MEND;
%send_email(email=example#example.com);
Peeyush,
Try the code sample below. Make sure to enter your SMTP email host details and the email addresses to and from. You can make the email addresses macro variables if you want additional automation:
%macro emailM;
OPTIONS emailsys = SMTP emailhost = Youremail.Host.net emailport= 25;
FILENAME Mailbox EMAIL 'You#something.com';
attach=("\\somewhere\something\File.xls");
DATA _NULL_;
FILE MailBox TO=("somebody#something.com" "somebody2#something.com")
Subject="Example of a SAS E-mail";
PUT "Hello Boss,";
PUT " ";
PUT "Attached are the Daily Operational Reports.";
PUT " ";
PUT "Thank you";
RUN;
%mend emailM;
%emailM;