DATA Step, Macro, Functions and more

Inserting data into letter

Reply
Regular Contributor
Posts: 244

Inserting data into letter

Hi,

I want to create letters to all students using my sas dataset. Can someone help how I can do it please. 

 

data have;
input Name $ Class $ ST_ID $ Course $ Score date;
informat date ddmmyy8.;
format date date9.;
cards; 
John Grade10 101 Eng 20 01/01/82
Mary Grade11 105 Bio 25 25/11/99
Paul Grade11 105 Math 18 23/09/00
Jenny Grade11 105 Eng 15 11/12/01
;
RUN;

 

Here is my output letter that I want to print in pdf.

 

Dear Name,
Your Course Grade is Score.

You will be attending to the graduation on Date.

 

Thank You,

 

School Principal

 

 

Valued Guide
Posts: 580

Re: Inserting data into letter

Could be solved by using report writing interface inside ods pdf or rtf. Sorry, no time to provide code.

Contributor
Posts: 32

Re: Inserting data into letter

Hi 

 

I think the following code might reach your requirement.

I have assumed st_id as student Id and that would be unique for each student, so I have written a conditional statement with st_id; to create letters os student specific 

 

data have;
input Name $ Class $ ST_ID  Course $ Score date;
informat date ddmmyy8.;
format date date9.;
cards; 
John Grade10 101 Eng 20 01/01/82
Mary Grade11 102 Bio 25 25/11/99
Paul Grade11 103 Math 18 23/09/00
Jenny Grade11 104 Eng 15 11/12/01
;
RUN;
ods pdf file = '\path\filename.pdf';
data want(keep = letter);
set have;
if ST_ID = 101 then
letter = "Dear student"||name||
		 "Your"||Course||"grade is"||score||
		 "You will be attending to the graduation on"||put(date,date9.);
if letter ne " ";
run;
ods pdf close

Thanks and Regards,

manoj

Super User
Posts: 10,278

Re: Inserting data into letter

Start by creating a test pdf document, eg like this:

%let student=John;
%let course=Eng;
%let score=20;
%let date=01/01/82;

ods pdf file='$HOME/sascommunity/letter.pdf';

ods text="Dear &student.,";
ods text="Your &course. grade is &score..";
ods text="You will be attending to the graduation on &date..";
ods text="";
ods text="Thank you,";
ods text="";
ods text="School Principal";

ods pdf close;

Then, attach the file to the email:

filename out email
  attach='$HOME/sascommunity/letter.pdf'
  to="&student.@school.edu"
  subject='Graduation'
;

data _null_;
file out;
put 'Your graduation letter';
run;

filename out clear;

As you can see, all variable parts (text, email address) are kept in macro variables.

Once you have checked this works, wrap it into a macro:

%macro email_to_student(student,course,grade,date);

/* code from above */

%mend;

Once you've tested this with a manual macro call, use it in call execute:

data _null_;
set have;
call execute('%nrstr(%email_to_student(' !! strip(name) !! ',' !! strip(course) !! ',' !! put(score,2.) !! ',' !! put(date,ddmmyy8.) !! '));');
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 10,784

Re: Inserting data into letter

I remembered @Cynthia_sas posted some kind of code a couple of years ago. Maybe she could post it again for you.

SAS Super FREQ
Posts: 9,371

Re: Inserting data into letter

Ask a Question
Discussion stats
  • 5 replies
  • 118 views
  • 3 likes
  • 6 in conversation