The MCHP SAS MANUAL - Program Structure Suggestions

         

Home    Contents

GENERAL GUIDELINES:
Windows in SAS
File management

The SAS Program
Program syntax
Debugging tips


 USING SAS PROGRAMMING TO: 
   
1. Prepare the data set 
   Types of data 
   Example programs    
    
2. View the data
   SAS Procedures
  
3. Explore the data  
   Numeric statistics    
   Frequency tables    
    
4. Manipulate the data  
   Basic techniques    
   New variables
  
5. Adding Variables and 
Observations to Data Sets
   The SET Statement
   The MERGE Statement

6. Data Processing
   ARRAY Statement
   Do Loops
   By-Group Processing
   RETAIN Statement
  
NON-PROGRAMMING 
      Alternatives

 
SAMPLE DATA SETS: 
 Height/weight
 Height/weight/region
 Simulated clinical data 
 Simulated Manitoba Health 
    

Structured SAS Code Suggestions (Feb 11, 1996)

The following are some suggestions for SAS programming structure. Some alternatives have also been mentioned.

General suggestions.

  1. 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.

  2. 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.

  3. SAS program files should end with .sas, list files with .lst, and log files with .log.

  4. 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...)
  5. 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.

  6. Data set names should reflect the contents of the data set. A data set label should be added to any permanent SAS data sets.

  7. Try to keep individual lines shorter than 80 characters.

Data step

  1. Data statement should be left justified. If options carry over then line up with initial brackets or indented 8 spaces.

  2. All other SAS statements should be indented at least 3-4 spaces. If code carries over to next line indent another 3-4 spaces.

  3. Only use one statement/line.

  4. New SAS variables should have an appropriate type, and length. A descriptive label should also be added to new variables.

  5. 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 ;
               ...
               ;
            
  6. 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 ;
            
  7. Cards data should be left justified.

  8. Each data step should end with a left justified run statement ** ALT indent run with data step code.

  9. Leave a blank line after each run statement.

  10. Array dimensions, and references should be in curly {} brackets.

  11. 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

  1. Follows same indenting rules as data step code.

  2. All internal code should be indented after the %macro statement.

  3. Clearly comment all your macro code, and variables.
    • %* comments will not show up in the resolved macro code
    • * comments will appear in resolved code

  4. Macros should not be defined, and compiled from within a macro.

Procedures

  1. Proc statement should be left justified. If options carry over to the next line they should be indented 8 spaces.

  2. Use only one statement/line.

  3. 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.

  4. Each procedure should end with a run, and or quit statement.

  5. 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

  1. Justify to the code that is being commented.
  2. Use ** ; type comments within code, or data statements This will allow /** **/ to be used to block out and run test sections.
  3. 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 ;

Contact: Charles Burchill       Telephone: (204) 789-3429

Systems Analyst, Manitoba Centre for Health Policy and Evaluation
Department of Community Health Sciences, University of Manitoba
T155 Old Basic Sci Bldg Winnipeg, Manitoba R3E 0W3      Fax: (204) 789-3910

Last Modified on August 23, 2005