|
The following are some suggestions for SAS programming structure.
Some alternatives have also been mentioned.
General suggestions.
- Maintain one case (upper or lower) mixing cases
with out reason makes code difficult to read.
As a side note: on systems that allow upper and
lower case most programmers use lower case - it is
generally easier to read.
- Every program should have an introductory comment.
/******
File name: Date:
Author:
Description:
Study:
If applicable the following should also be added.
Principal Investigator:
Input Data:
Output Data:
Variables Generated:
External files:
*******/
The introductory comment may be enclosed in a box.
- SAS program files should end with .sas,
list files with .lst, and log files with .log.
- Code so you, and others can understand your code.
Remember
Occam's Razor.
(After William of Ockham (1300-1349? English philosopher)
a philosophical or scientific principle according to which the
best explanation of an event is the one that is the simplest,
using the fewest assumptions, hypotheses, etc...)
- If possible all %include files, formats, macros and other general
code should go at the top of the
program, or be referenced in the initial comment.
- Data set names should reflect the contents of the data set. A
data set label should be added to any permanent SAS data sets.
- Try to keep individual lines shorter than 80 characters.
Data step
- Data statement should be left justified.
If options carry over then line up with initial brackets
or indented 8 spaces.
- All other SAS statements should be indented at least 3-4 spaces.
If code carries over to next line indent another 3-4 spaces.
- Only use one statement/line.
- New SAS variables should have an appropriate type, and length.
A descriptive label should also be added to new variables.
- Do statements
- Do is lined up with prior code.
- The do block is indented 3-4 spaces.
- The end statement is lined up with do.
** ALT indent end with do block.
data iterate1 ;
input x ;
exit=10 ;
do i=1 to exit ;
y=x*normal(o) ;
if y>25 then i=exit ;
output ;
end ;
cards ;
...
;
- If-Then-do/Else statements
- If statement is lined up with prior code.
- If block is indented 3-4 spaces. ** ALT Do command may be left
justified on separate line.
- else is lined up with associated if statement
- end statement is line up with if (or else).
** ALT indent end with if block.
if answer=9 then do ;
answer=. ;
put 'INVALID ANSWER FOR' id= ;
end ;
else do ;
answer=answer10 ;
valid+1 ;
end ;
More SAS CODE ;
- Cards data should be left justified.
- Each data step should end with a left justified run statement
** ALT indent run with data step code.
- Leave a blank line after each run statement.
- Array dimensions, and references should be in curly {} brackets.
- Keep declarative statements together.
- Retain, Length at top of program.
- Label, Drop at bottom of program.
** ALT Some programmers prefer to use drop statements at the
point in the program where a variable is no longer needed.
Macro code
- Follows same indenting rules as data step code.
- All internal code should be indented after the %macro
statement.
- Clearly comment all your macro code, and variables.
- %* comments will not show up in the resolved macro code
- * comments will appear in resolved code
- Macros should not be defined, and compiled from within a macro.
Procedures
- Proc statement should be left justified.
If options carry over to the next line they should be
indented 8 spaces.
- Use only one statement/line.
- Indent procedure statements 3-4 spaces. If the statement is
longer than one line then each subsequent line should be indented at
least 8 spaces.
- Each procedure should end with a run, and or quit statement.
- Leave a blank line after each run statement.
proc format data=jumbo.data ;
where slice='1' ;
tables a*b c*d / noprint out=temp ;
run;
proc chart data=interm.grades ;
block section / midpoints='Mon' 'Wed' 'Fri'
group=sex
sumvar=grade type=mean ;
title 'Comparing the Mean for GRADE among Sections' ;
run;
Comments
- Justify to the code that is being commented.
- Use
** ; type comments within code, or data statements
This will allow /** **/ to be used to block out
and run test sections.
- Comments apply to next line or block of code.
Test code
If you want to add test code to your program such as put _all_ ;
it should be left justified. This will make it much
easier to see and remove the code later ;
|