BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasmaverick
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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;

Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
RamKumar
Fluorite | Level 6

Little correction on Jag's code.

It should be

%do i = 1 %to 3; *percentage symbol was missing in initial code.

Jagadishkatam
Amethyst | Level 16

Thanks for the correction, Ram

Thanks,
Jag
sasmaverick
Obsidian | Level 7

Thanks a lot Jagadish! Works for me!

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1200 views
  • 3 likes
  • 4 in conversation