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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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