DATA Step, Macro, Functions and more

Create Multiple Files Conditionally

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

Create Multiple Files Conditionally

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.


Accepted Solutions
Solution
‎03-05-2015 08:03 AM
Trusted Advisor
Posts: 1,130

Re: Create Multiple Files Conditionally

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


All Replies
Super User
Posts: 6,945

Re: Create Multiple Files Conditionally

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎03-05-2015 08:03 AM
Trusted Advisor
Posts: 1,130

Re: Create Multiple Files Conditionally

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
Regular Contributor
Posts: 168

Re: Create Multiple Files Conditionally

Little correction on Jag's code.

It should be

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

Trusted Advisor
Posts: 1,130

Re: Create Multiple Files Conditionally

Thanks for the correction, Ram

Thanks,
Jag
Contributor
Posts: 64

Re: Create Multiple Files Conditionally

Thanks a lot Jagadish! Works for me!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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