1.6 - Guidelines for Formatting and Commenting SAS Programs

1.6 - Guidelines for Formatting and Commenting SAS Programs

Regardless of the programming language used, there is a basic set of good programming practices to which any good programmer will adhere. Two good programming practices concern the formatting and commenting of your programs. Therefore, let's close this lesson by reviewing some guidelines for formatting and commenting your SAS programs. Throughout this course (and beyond!), you should plan on adhering to the following guidelines:

/********************
This SAS program blah, blah, blah, ..........
*********************/
*Convert Fahrenheit to celsius;

As mentioned earlier, comment statements allow you to document your program without affecting processing. A delimited comment, which begins with a forward-slash-asterisk (/*) and ends with an asterisk-forward-slash (*/), is useful for creating large blocks of comments. All text within the delimiters are ignored. An alternative type of comment begins with an asterisk (*) and ends with a semicolon (;). Examples:

Although SAS allows for free-formatted code, a good SAS program will be well organized.

/******************
Filename: /home/yourname/sas/temp.sas
Written by: Your Name
Date: January, 9, 2023
This program calculates the average number of days that the tempreture falls below freezing in State College, PA
Input: C:\data\temps.dat
Output: average number of days below freezing by month stored in C:\data\temps.ssd
******************/

Every SAS program should start with a main block of comments, emphasized by asterisks. The block of comments should include the filename, by whom the program is written, the date on which the program was written, and text that clearly describes the main purpose, input, and output of the program. Example:

Every critical DATA step or PROC step should be preceded by a block of comments, emphasized by asterisks, which describe the primary purpose of the step. The block of comments should also include any critical information, such as variable names, and the input and output of the block of code.

temp_f = 1.8*temp_c+32; *Convert celsius to fahrenheit;

Comments that pertain to a single line of code are useful, e.g. for describing what an expression is calculating, describing a new variable and how it is calculated, why the dataset is subset on a particular set of values, and so on. Example:

At least one line should separate any PROC or DATA steps within your SAS program.

PROC PRINT data=stat480.temps;
RUN;
DATA temps;
    set stat480.temps;
    if month in ('April','May','June');
RUN;

To help offset blocks of code, it is useful to capitalize PROC PROCNAME, DATA, and RUN. Examples:

PROC PRINT data=stat480.temps;
    title 'Tempretures in April, May, June';
    var month loc time_hr time_min am_pm temp_c temp_f;
RUN;

Any code contained within a DATA step or PROC step should be indented at least two spaces to improve legibility. Keywords within the DATA steps or PROC steps, such as title, variable, and table, should be easily identified.

In this lesson, we just basically got you up and running on the SAS System. In the next lesson, we'll spend time learning how to read your data into the SAS data sets that the SAS System understands.


Legend
[1]Link
Has Tooltip/Popover
 Toggleable Visibility