Hello,
I recently learned how to send emails/texts with SAS, and now I'm trying to put that knowledge to use in a data step. Basically, I want to send myself a text mesaage with a list of CustomerIDs that have errors. I have code that checks for errors and want to receive a text only when new CustomerIDs appear in the "error" data set. Here's the code that I'm currently using:
Top of program:
options emailsys=smtp emailauthprotocol=login emailid="xxxxxx@aol.com" emailpw="xxxxxxxx"
emailhost=smtp.aol.com emailport=587;
Code within macro:
DATA NEW_ERRORS;
MERGE OUT.KNOWN_ERRORS (IN=IN1) ERRORS (IN=IN2);
BY CUST_ID;
IF IN2 & NOT IN1 THEN DO;
OUTPUT NEW_ERRORS;
filename mymail email "xxxxxxxx@txt.att.net" subject="CustomerIDs with potential errors" lrecl=256;
file mymail;
put 'list of CustomerIDs with errors separated by commas or spaces.'; *Still need to figure out this part!;
END;
RUN;
proc append base=OUT.KNOWN_ERRORS data=NEW_ERRORS FORCE;
proc sort data = OUT.KNOWN_ERRORS; BY CUST_ID; RUN; *NEED TO SORT DATA FOR SUBSEQUENT MERGES;
What I'm attempting to with the above is take the list of errors over all CustomerIDs (ERRORS) and compare them with the currently known list of CustomerIDs with errors (OUT.KNOWN_ERRORS). If there's a new set of CustomerIDs with an errors I want to 1) receive a text message with only that list of CustomerIDs (new errors) and 2) add those CustomerIDs to the currently known list of errors (OUT.KNOWN_ERRORS). This way when the code is run again, I won't receive another text with the same list of CustomerIDs. Note code works fine in terms of identifying and adding only the new errors to the known list of errors. The issue I'm having is debugging the text message part. When I run the code with no new CustomerIDs added to the list, I get constantly bombarded with text messages. Therefore my DO statement isn't firing the text message code appropriately. Does anyone have any suggestion as to what I'm doing wrong? Am I trying to do too much in one data step? Also, does anyone know how to include the list of new CustomerIDs in the body of my text? I was hoping to do this in a list format with each CustomerID separated by commas or spaces. How would I create such a list? As output to my log? Or do I need to create some sort of macro variable that'll be used in an ODS statement?
Any help or advice would be greatly appreciated. Thank you very much!
-Bill
Why not just split the email as a separate step from the merge?
You could then use macro logic or other code generation methods to only send an email when new items are found.
%let anynew=0;
data new_errors;
merge out.known_errors (in=in1) errors (in=in2);
by cust_id;
if not in1 ;
call symput('anynew','1');
run;
%if &anynew %then %do;
filename mymail email "xxxxxxxx@txt.att.net"
subject="CustomerIDs with potential errors" lrecl=256
;
data _null_;
set new_errors ;
file mymail dsd ;
put custid @@;
run;
%end;
Why not just split the email as a separate step from the merge?
You could then use macro logic or other code generation methods to only send an email when new items are found.
%let anynew=0;
data new_errors;
merge out.known_errors (in=in1) errors (in=in2);
by cust_id;
if not in1 ;
call symput('anynew','1');
run;
%if &anynew %then %do;
filename mymail email "xxxxxxxx@txt.att.net"
subject="CustomerIDs with potential errors" lrecl=256
;
data _null_;
set new_errors ;
file mymail dsd ;
put custid @@;
run;
%end;
Tom,
My apologies for the delayed response. Thanks so much for the above. Your solution worked perfectly!!!
Regards,
Bill
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.