I have the below dataset:
Name
Jack Chirikjian
Dean Rosenthal
Dean Rosenthal
Dean Rosenthal
John Adams
John Adams
I need a code which will conditionally create text (tab delimited files), each containing the names.
E.g: File1.txt
Jack Chirikjian
File2.txt
Dean Rosenthal
Dean Rosenthal
Dean Rosenthal
File3.txt
John Adams
John Adams
Also, I need to email these files as attachments to their respective emails. Eg Jack will get an email with file1.txt, Dean will get with File2.txt and so on. (assume I have the emails addresses)
Greatly appreciate the inputs.
consider that you have the email address as well in the dataset. Then this code should be helpful
data have;
input Name &$30. email :$20.;
cards;
Jack Chirikjian jag@gmail.com
Dean Rosenthal de@gmail.com
Dean Rosenthal de@gmail.com
Dean Rosenthal de@gmail.com
John Adams jo@gmail.com
John Adams jo@gmail.com
;
proc sql;
select distinct name, email into : name1-:name&sysmaxlong, :email1-:email&sysmaxlong from have;
quit;
%put &name1 &name2 &email2;
%macro email;
%do i = 1 to 3;
DATA _NULL_;
set have;
where name="&&name&i";
FILE '~\&&file&i.txt';
PUT name;
RUN;
FILENAME MailBox EMAIL "&&email&i"
SUBJECT='Mail message with txt attachment'
attach="~\&&file&i.txt";
data _null_;
file mailbox;
put 'hi';
run;
%end;
%mend;
%email;
Thanks,
Jag
data _null_;
file "filex.txt" filevar=filnam; * filex.txt is just a placeholder;
set have;
by name;
retain counter 0;
if first.name
then do;
counter + 1;
filnam = 'file' !! trim(put(counter,3.)) !! '.txt';
end;
put name;
run;
consider that you have the email address as well in the dataset. Then this code should be helpful
data have;
input Name &$30. email :$20.;
cards;
Jack Chirikjian jag@gmail.com
Dean Rosenthal de@gmail.com
Dean Rosenthal de@gmail.com
Dean Rosenthal de@gmail.com
John Adams jo@gmail.com
John Adams jo@gmail.com
;
proc sql;
select distinct name, email into : name1-:name&sysmaxlong, :email1-:email&sysmaxlong from have;
quit;
%put &name1 &name2 &email2;
%macro email;
%do i = 1 to 3;
DATA _NULL_;
set have;
where name="&&name&i";
FILE '~\&&file&i.txt';
PUT name;
RUN;
FILENAME MailBox EMAIL "&&email&i"
SUBJECT='Mail message with txt attachment'
attach="~\&&file&i.txt";
data _null_;
file mailbox;
put 'hi';
run;
%end;
%mend;
%email;
Thanks,
Jag
Little correction on Jag's code.
It should be
%do i = 1 %to 3; *percentage symbol was missing in initial code.
Thanks for the correction, Ram
Thanks a lot Jagadish! Works for me!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.