Section 1.9: General Tips to Minimize Errors when Writing SAS Programs When learning to manage and analyze data with SAS, as in learning any new software package or programming language, there are steps you can take to make the tasks it a little easier. Many of these approaches will be discussed and elaborated upon in the various chapters of this web-site. Important advice is to first take some time to plan exactly what you want to do with your data. First, write a detailed step-by-step outline of what you want to accomplish. You can always revise the plan as you go, but it helps save time wasted from missing important steps and will in the long result in fewer errors. Other tips: 1. Compose your program from your workplan. Write your SAS program and think of your data analysis as a combination of DATA and PROC steps derived from your analysis plan. As you become more proficient, the SAS commands, saved in a program file, will become part of your workplan and help you understand it better. 2. Within the program itself, make judicious entries of comments and blank lines. Indent program steps to make it easier to follow the flow and logic of your tasks. This formatting will be a tremendous aid in proofreading and editing for errors, as well as in making modifications in the program, or adapting a program for use in another setting. So talk to yourself about what you are doing and enter them as comments. Make your programs user-friendly. 3. Use a program header to keep track of when, where, and why the program was written, what data sets were used, and what data sets were created. A program header is merely a block of comments at the start of the program that contains documentation information. In addition, use titles and footnotes to label your results. To reiterate, reserving the first title or footnote for the program filename helps link results to the program that produced them. Example Program Header: ** Project: Physiology Study **; ** Date: AUG 10 2004 **; ** Filename: phy5.sas **; ** Programmer: Robin High **; ** Input Files: neuro1.xls **; ** Output Files: neuro1.sas7bdat**; A program header may be more or less elaborate than this example, yet the essential information of what the program does and when it was written/modified should be easily retrievable from the file contents. 4. Enter OPTIONS and TITLE statements at the beginning of each program to control the format of your resulting output. The SAS defaults for line size and page size are designed to fit on a computer screen, rather than a page of paper. Printing this way wastes paper as well as breaking up tables and lists unnecessarily. To set output for 8.5 X 11 inch paper, use values in the range of 55 to 60 for pagesize, and 78 to 128 for linesize. If you select a larger number for linesize, you will need to print output in "landscape" format or else select a small size font when printing. This saves paper, as well as enabling more variables to be listed across a page. For example: OPTIONS LINESIZE=80 PAGESIZE=55 formdlim='-' NOcenter; TITLE1 "Physiology Study"; One of the difficulties in learning and using SAS is editing files quickly and easily finding mistakes when they do occur. Here is a list of ideas that may help make editing and writing programs easier. As you read through subsequent sections, the contents of this list should be clarified. * End SAS statements with a semi-colon (probably the most common programming error!) * Quotes always come in pairs of the same type: 'text'; or "text"; and not "text'; or 'text .. ; * Define short variable names (8 characters or less) and associate them with LABELS to provide longer descriptions * Enter one statement per line and end with a semi-colon! * Upper and lower case letters distinguish SAS keywords from variable names or selected options * Leave a blank row between blocks of statements included in each DATA and PROC step * Use a full-screen text editor (pico on DARKWING and GLADSTONE works very well); on Windows SAS, be sure to request the Enhanced Editor * File Management: store your programs and data in subdirectories indicating project type * Have a set of write-protected templates of programs available and then edit and save them as new files * Learn how to apply macro variables and macros (an advanced topic which can offer a huge saving of time and give you much greater flexibility) * Practice, Practice, Practice - experience makes it natural and easy * Learn from your mistakes - Perhaps a quote from a famous person who succeeded in business is perhaps good advice: "The fastest way to succeed is to double your failure rate." -Thomas J. Watson, Sr. Founder of IBM And finally, one of my favorite quotes, that has nothing to with learning SAS in particular; rather, it has a great deal to do with WHY you should learn SAS: "You haven't told me yet," said Lady Nuttal, "what it is your fiancé does for a living." "He's a statistician," replied Lamia, with an annoying sense of being on the defensive. Lady Nuttal was obviously taken aback. It had not occurred to her that statisticians entered into normal social relationships. The species, she would have surmised, was perpetuated in some collateral manner, like mules. "But Aunt Sara, it's a very interesting profession," said Lamia warmly. "I don't doubt it," said her aunt, who obviously doubted it very much. "To express anything important in mere figures is so plainly impossible that there must be endless scope for well-paid advice on how to do it. But don't you think that life with a statistician would be rather, shall we say, humdrum?" Lamia was silent. She felt reluctant to discuss the surprising depth of emotional possibility which she had discovered below Edward's numerical veneer. "It's not the figures themselves," she said finally. "it's what you do with them that matters." Attrib. K.A.C. Manderville, The undoing of Lamia Gurdleneck, in Kendall and Stuart's The Advanced Theory of Statistics.