SAS E-mail alert

When you have a long code to run you may not want to just sit and wait, maybe have a walk around and be notified when the code finishes to execute? That is possibe if you have a phone that you can check your mail on and your SAS server is configured to send e-mails. You may also use this code to inform users that the report has generated and they may pick it up from a certain location.

Here is a small piece of code that will distribute an information about the frequencies from a cetrain dataset:

/* prepare tables with frequencies to put into email’s body */

PROC FREQ DATA=work.my_dataset;
tables x / OUT = work.my_dataset_freq OUTEXPECT SPARSE;

/* store frequencies as macro variables */
%LET rec_count = 0;
RETAIN cnt 0;
SET work.my_dataset_freq END=last;
cnt + 1;
CALL SYMPUT(‘var’||TRIM(LEFT(PUT(cnt,8.))),CATX(‘ ‘, X, COUNT));
IF last THEN CALL SMPUT(‘rec_count’, PUT(cnt, 8.));

/* build an email */

%macro email;

SUBJECT = “** My daily scheduled report **”;

FILE mail;
PUT “Please do not reply to this email.”;
PUT ” “;
PUT “This email was send automatically from SAS.”;
PUT “The SAS code generating the report finished.”;
PUT “——————————————————–“;
%DO i = 1 %TO &rec_count;
PUT “&&var&i”;
PUT “——————————————————–“;
PUT “Thanks”;
%mend email;