Published on :
Utility CREATION_INTERNE

SAS Email Notification

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
The script begins by clearing the 'output' and 'log' DM (Display Manager) windows. It then defines three macros: 'startT' to capture the start date and time, 'endT' for the end date and time, and 'email' to format and send a notification email. The 'email' macro configures SMTP options and uses an EMAIL fileref to send the message. The script then calls 'startT' and 'endT', executes a simple DATA step 'Hello world!', and finally sends a notification email via the 'email' macro.
Data Analysis

Type : CREATION_INTERNE


The data manipulated in this script (dates, times, email messages) are dynamically generated or are literals. There is no reading of external data or persistent SAS datasets.

1 Code Block
Commandes DM
Explanation :
These commands clear the content of the 'output' and 'log' Display Manager (DM) windows in the SAS environment.
Copied!
1dm 'output;clear';
2dm 'log;clear';
2 Code Block
MACRO STARTT
Explanation :
This macro defines two macro variables, &startdate and &starttime, which contain the current formatted date and time at the moment of its execution. It uses a DATA _NULL_ step because no dataset is created.
Copied!
1%macro startT;
2DATA _null_;
3 tempdate=put(date(),weekdate32.);
4 temptime=put(time(),hhmm5.);
5 call symput("startdate",tempdate);
6 call symput("starttime",temptime);
7RUN;
8%mend;
3 Code Block
MACRO ENDT
Explanation :
Similar to the startT macro, this macro defines the &enddate and &endtime macro variables to record the end date and time of execution.
Copied!
1%macro endT;
2DATA _null_;
3 tempdate=put(date(),weekdate32.);
4 temptime=put(time(),hhmm5.);
5 call symput("enddate",tempdate);
6 call symput("endtime",temptime);
7RUN;
8%mend;
4 Code Block
MACRO EMAIL
Explanation :
This macro configures system options for sending emails via SMTP, specifying the sender (EMAILID) and the server (EMAILHOST). It creates an 'mymail' fileref of type EMAIL, defines the recipient (TO) and the email subject using the 'dt' parameter passed to the macro. A DATA _NULL_ step is then used to write the body of the message to the file linked to 'mymail', including the previously captured start and end time information.
Copied!
1%macro email(dt);
2 /* Set up the options for the email. */
3 OPTIONS EMAILSYS=smtp EMAILID="kfong @cornerstone.com"
4 EMAILHOST=CRDCEXCH10.cornerstone.com;
5 FILENAME mymail
6 EMAIL
7 TO = ("kfong @cornerstone.com")
8 SUBJECT="&dt. finished"
9 ;
10 /* Send the email and define the message to send. */
11 DATA _NULL_;
12 FILE mymail;
13 PUT "Hello,";
14 PUT " ";
15 PUT "The code for &dt. has finished running.";
16 PUT "Start: &StartTime. &StartDate. ";
17 PUT "End: &EndTime. &EndDate. ";
18 RUN;
19%mend;
5 Code Block
Appel de macros
Explanation :
These lines call the StartT and EndT macros to initialize the macro variables for the start and end date and time.
Copied!
1%StartT;
2%EndT;
6 Code Block
DATA STEP
Explanation :
This simple DATA step writes the string "Hello world!" to the SAS log. It serves here as an example of code to be executed between the start and end timestamps.
Copied!
1DATA _null_;
2 put "Hello world!";
3RUN;
7 Code Block
Appel de macro
Explanation :
This line calls the Email macro with 'test' as a parameter, which triggers the sending of the notification email with the subject 'test finished' and the execution time information.
Copied!
1%Email(test);
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Created by: Kevin Fong, Date Created: 2/4/14