BookmarkSubscribeRSS Feed
mlogan
Lapis Lazuli | Level 10

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

 

 

5 REPLIES 5
andreas_lds
Jade | Level 19

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

s_manoj
Quartz | Level 8

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

Kurt_Bremser
Super User

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;
Ksharp
Super User

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2180 views
  • 3 likes
  • 6 in conversation