September 2006 Technical Tip – Writing form letters with SAS
Did you know you can write form letters with SAS? All you need is knowledge of a few tricks with SAS' FILE and PUT statements. In our example, we will read a customer name and address file and produce letters inviting these customers to visit our time share resort. We begin with the end; that is, here is how our letter should appear. Note the city name is embedded in the text of the letter.
469 N 400 E
DeSoto, TX 75115
Congratulations! You are one of a select group from DeSoto
to be invited to visit our luxurious resort! Call 800-TIMESHR
immediately for details!
See you soon!
Here is the SAS code to produce the letter:
Download file here.
filename formltr 'c:\temp\formltr.txt';
input @ 1 nbr $5.0
@ 6 lname $10.0
@16 fname $10.0
@26 addr $15.0
@41 city $10.0
@51 state $2.0
@53 zip $5.0 ;
11224Binford Dan 469 N 400 E DeSoto TX75115
12111Arias Ida 4028 Elmo Loop Merced CA95340
32555Ryan Richard 914 Fifth St Normal IL61761
41499Hilmer Debbie 21175 Felipa Buena ParkCA90620
55123Josephson Peggy 248 Michigan Jamestown NY14701
61626Havlik Cheryl 551 Washington Whittier CA90605
77271Carpenter Lois 326 Beach Berwyn IL60650
81288Black Kathy 618 S Anza Pasadena CA91106
81997Foote April 635 Burns Carol StrmIL60187
94993Dixson Richard 1021 Brown Chicago IL60612
retain lm 5; /* left margin */
file formltr print notitles;
x = trim(fname) || ' ' || trim(lname);
put /// @lm x;
put @lm addr;
x = trim(city) || ', ' || state || ' ' || zip;
put @lm x;
x = 'Dear ' || trim(fname) || ',';
put /// @lm x;
put / @lm 'Congratulations! You are one of a select group from ' city;
put @lm 'to be invited to visit our luxurious resort! Call 800-TIMESHR';
put @lm 'immediately for details!';
put / @lm 'See you soon!';
put / @lm 'Sincerely,';
put //// @lm 'Bill Qualls';
put @lm 'VP Promotions';
The key points in this example are:
- I have defined a variable lm for use as the left margin; for example put @lm x. Having such a variable will simplify shifting the letter to the left or right as needed.
- The print option on the file statement, when used in conjuction with the _page_ constant causes the form feed character to appear. If you open the output file with Notepad and use the Terminal font, the form feed symbol will appear as the female ( ♀ ) symbol. With other fonts, you are likely to see just a box.
- The forward slash symbol ( / ) is used to skip a line. If you use a slash (only) on a put statement, you are likely to get one more blank line than intended because SAS goes to a new line for every put.
- The trim function and concatenation operator ( || ) are used to format text fields to be shown adjacent to each other. Without these you will get extra spaces you probably don't want.
We hope you will consider Caliber Data Training when you are in need of high quality SAS training.
Go to the articles index.
Written by Bill Qualls. Copyright © 2006 by Caliber Data Training 800.938.1222