Here's what worked for me. Of course, double check your lengths on your cards on the email input.
Data test; Input id$ sales email$33.; Cards; aa 1000 aaa@gmail.com bb 2000 bbb@gmail.com xx 9000 xxx@gmail.com ; Run; FILENAME outmail EMAIL /* Use filename and define outmail variable */ SUBJECT="TEST" /* Email title */ FROM= "sender@domain.com"; /*Who from*/ options mprint mlogic symbolgen; %macro email(); %let cnt=1; %do %until(&cnt > &sysnobs); /*do until the cnt is gt # of obs in your dataset*/ data _null_; /*creates variables from your dataset for obs=cnt*/ set test (firstobs=&cnt obs=&cnt); call symputx('id',id); call symputx('sales',put(sales,dollar12.2)); call symputx('email',email); run; data _null_; /*send the email. play around with the put statements as needed.*/ file outmail TO=("&email"); put "&id &sales"; run; %let cnt = %eval(&cnt + 1); /*increments the count up 1*/ %end; %mend email; %email
Here's what worked for me. Of course, double check your lengths on your cards on the email input.
Data test; Input id$ sales email$33.; Cards; aa 1000 aaa@gmail.com bb 2000 bbb@gmail.com xx 9000 xxx@gmail.com ; Run; FILENAME outmail EMAIL /* Use filename and define outmail variable */ SUBJECT="TEST" /* Email title */ FROM= "sender@domain.com"; /*Who from*/ options mprint mlogic symbolgen; %macro email(); %let cnt=1; %do %until(&cnt > &sysnobs); /*do until the cnt is gt # of obs in your dataset*/ data _null_; /*creates variables from your dataset for obs=cnt*/ set test (firstobs=&cnt obs=&cnt); call symputx('id',id); call symputx('sales',put(sales,dollar12.2)); call symputx('email',email); run; data _null_; /*send the email. play around with the put statements as needed.*/ file outmail TO=("&email"); put "&id &sales"; run; %let cnt = %eval(&cnt + 1); /*increments the count up 1*/ %end; %mend email; %email
Searching the web for 30 seconds brings up this:
filename reports email 'Jim'; data _null_; file reports; infile cards eof=lastobs; length name dept $ 21; input name dept; /* Assign the TO attribute */ put '!EM_TO!' name; /* Assign the SUBJECT attribute */ put '!EM_SUBJECT! Report for ' dept; put name ','; put 'Here is the latest report for ' dept '.'; /* ATTACH the appropriate report */ if dept='marketing' then put '!EM_ATTACH! mktrept.txt'; else put '!EM_ATTACH! devrept.txt'; /* Send the message */ put '!EM_SEND!'; /* Clear the message attributes */ put '!EM_NEWMSG!'; return; /* Abort the message before the */ /* RUN statement causes it to */ /* be sent again. */ lastobs: put '!EM_ABORT!'; datalines; Susan marketing Jim marketing Rita development Herb development ; run;
Data test;
infile cards dlm=" ";
Input id$ sales email$ 40.;
Cards;
aa 1000 xyz@gmail.com
bb 2000 raj@gmail.com
xx 9000 ggg@gmail.com
;
proc sql;
select count(distinct email) into :emailcnt from test;
%let emailcnt = %cmpres(&emailcnt);
select distinct email into :email1 - :email&emailcnt from test;
quit;
%macro mail;
%if &emailcnt gt 0 %then
%do i = 1 %to &emailcnt;
filename Madhu email
from = "Insert User Email Id Here"
subject = "Test"
to = "&&email&i"
type = "text/html";
data _null_;
file Madhu;
set Test(where = (email="&&email&i"));
put ID SALES ;
RUN;
%end;
%mend;
%mail;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.