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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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