SAS Work Shop Statistical Programs   
Handout # 1 College of Agriculture


Topics covered: Indentation
Comments

GOOD PROGRAMMING HABITS

1) Program Format
         It is a good idea to use indentation when writing programs (SAS or otherwise). Most
computer software packages are indifferent to indentation, but it will make your programs easier
to read. The 'block' nature of SAS lends itself to indentation very well. Blank lines between
blocks of code also improves readability.
Example 1

BAD:
     DATA NEW; INFILE 'A:MY.DAT'; INPUT NAME $ AGE CLASS $ GRADE $; 
     PROC PRINT; VAR NAME GRADE; PROC SORT; BY  CLASS; PROC PRINT;

GOOD:
     DATA NEW;
          INFILE 'A:MY.DAT'; 
          INPUT NAME $ AGE CLASS $ GRADE $;

     PROC PRINT;
          VAR NAME GRADE;

     PROC SORT;
          BY CLASS;

     PROC PRINT;         

Note that either of these sets of code will produce the same results. The second will be easier to
interpret and debug (find & fix errors) if required. One might think that this is inefficient but it is
worth it in the long run.

Return to TOP ; Return to Outline


2) Comments in Programs

         By using special symbols in SAS we can add to a program comment lines which tell
anyone reading it what the programmer had in mind when they wrote the code. This can be very
useful when programs are passed from person to person or the code is seldom used. The special
symbols tell SAS to skip these lines since they are only for us. If you need to comment out one or
more lines, the format to use these is: /* any text here */. Any text written between the /* and */
will be ignored. This could mean more than one line ispassed over. Note that no semicolon is
required after the closing */.
Example 2


     /*   READ IN DATA FROM EXTERNAL FILE  */

     DATA NEW;
          INFILE 'A:MY.DAT';
          INPUT NAME $ AGE CLASS $ GRADE $;

     /*  PRINT OUT DATA.  (VARIABLES NAME AND
           GRADE ONLY).                       
     */

     PROC PRINT;
          VAR NAME GRADE;

OR

     DATA NEW;                                /* READ IN    */
          INFILE 'A:MY.DAT';                  /* DATA FROM  */
          INPUT NAME $ AGE CLASS $ GRADE $;   /* FILE       */

     PROC PRINT;                              /* PRINT NAME */
          VAR NAME GRADE;                     /* AND GRADE  */
         It is also possible to comment out a single line in a SAS program using the symbol *. This is
handy for debugging (checking for errors) complex programs.
Example 3

     DATA NEW;
           INFILE 'A:MY.DAT';
           INPUT NAME $ AGE CLASS $ GRADE $;
           *DAYS_OLD=AGE*365;

Here the line DAYS_OLD=AGE*365; is skipped during execution. Comments using the *
MUST end with a semicolon.

Return to TOP ; Return to Outline