The MCHP SAS MANUAL - Program Syntax

         

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 
    

SAS PROGRAM SYNTAX

SAS programs are normally developed within the Program Editor window. Nothing entered in this window is processed, or read by SAS, until the program is submitted, or executed (unlike commands). A SAS session lasts until the user exits the SAS program.

Upon submission, the program disappears from the Program Editor Window and any messages will appear in the Log window. After SAS has finished processing the program, the Output window, with the requested output, should be displayed on top of the other windows. If there were problems in the processing, the Log window might instead be displayed. In either case, though, the first thing that should be done is to check the SAS log for messages before reviewing the output. Note that the lines in the log output and the page numbers in the output generated in the Output window are numbered sequentially and cumulatively for the entire SAS session. They are reset with each new SAS session.

If there are problems, the submitted program can be brought back into the Program Editor window with a Recall command, changes can be made (saving the revised program), and the program can be re-submitted. The Output and Log windows should be cleared first (Clear command) so that new material is not mixed in with old material (all material generated in both Log and Output windows are kept cumulatively throughout the SAS session). If desired, the contents of any of the three windows can be saved using the File/Save As commands.

A SAS program consists of SAS statements which are constructed using SAS language, several key characteristics of which are described below, followed by an example program:

  • Case. Lower case is typically used in writing SAS programs although SAS currently recognizes upper case in a program (upper case was used in the example program below simply to denote SAS keywords). If any values in a data set are in upper case, however, references to such values within a SAS program must also be upper case. References to values and variables must also distinguish, where relevant, numbers from letters. For example, the number "0" must be distinguished from the letter "O" within a SAS program for it to be read accurately (the "0" in the variable "diag01", for example, is numeric).

  • Naming conventions. Names of both data sets and variables currently can be up to a maximum of 32 characters long (starting with a letter or underscore) and can include numbers.

  • Keywords - are used to specify the tasks to be carried out by SAS (e.g., SET, RUN). Keywords should not be used as variable or data set names.

  • Statements - can be longer than one line and can begin anywhere on the line (alternatively, one line can have several SAS statements). ALL SAS statements must end with a semicolon (";"), while indentation of statements is optional.

  • Steps - represent broad categories of tasks within which most programming is done in SAS. Typically used are:
    • DATA steps (e.g., for creating data sets, for creating new variables, etc.) and
    • PROC steps (e.g., for creating tables, graphs, formats, etc.

    Each DATA and PROC step should end with a "RUN;" statement.

  • Comment lines - are useful for program documentation or for temporarily making SAS statements non-executable. They instruct SAS to ignore material contained within:
    • /* and */
      This can generally be used almost anywhere (e.g., within a SAS statement).
      /* This is an example. */

    • * and ;
      This can NOT be used within a SAS statement. To comment out existing SAS statements (which already end with a ";"), simply add * at the beginning of a statement.
      * This is an example. ;

Example of SAS Program Syntax

PROC FORMAT;
  VALUE 'M' = 'Male'
        'F' = 'Female';
RUN;
This PROC step is an example of how to create labels for a gender variable consisting of M/F values. PROC, FORMAT, VALUE, and RUN are the SAS keywords. There are 3 statements in this 4-line program (the VALUE statement, ending with ";" is 2 lines long).

  /* create a SAS data set*/
DATA new1;
  /* read a SAS data set*/
  SET original;  
   * keep only women ;
  IF gender='F'; 
RUN;

This DATA step is an example of creating a temporary data set called "new1" from a temporary SAS data set created earlier in the SAS session called "original"; it instructs SAS to keep only females in the "new1" data set. The two types of comments are also illustrated.

PROC FREQ DATA=new1;
  TABLES gender;
  RUN;
This PROC step instructs SAS to read the "new1" data set, and to create a table showing the distribution of the gender variable.

Program Development

A document prepared by the Manitoba Centre for Health Policy on program development provides suggestions on how to structure SAS code and what might be included in the program. This sections builds on that document.

SAS code can be entered consecutively within the Program Editor window to create a large program, or the code might reside in other files that SAS can be instructed to find and process. The above 3 components in the table, for example, could be in 3 different files on a floppy disk called study.fmt, prog.sas, and analyses.sas. Two ways in which SAS could read and process the files are:

  1. Open each file into the Program Editor window until all 3 are present in the window. All code from each file will be seen in the window. Note that SAS programs can be submitted in portions (each of the above 3 components could be submitted separately) or all at once, combining a number of DATA and PROC steps.

  2. Within the Program Editor window, use %include to process each file, i.e., submit the following 3-line program:
    %include 'a:\study.fmt';
    %include 'a:\prog.sas';
    %include 'a:\analyses.sas';
        
An important distinction between the two approaches is that the first approach allows the exact code which generated the results to be seen in the log. %include is used more typically when code is used repeatedly or when the user is familiar with the file(s) being included.

Home
General Guidelines: File Management
NEXT
Debugging tips

Contact: Charles Burchill       Telephone: (204) 789-3429
Manitoba Centre for Health Policy
Department of Community Health Sciences, University of Manitoba
4th floor Brodie Centre
408 - 727 McDermot Avenue
Winnipeg, Manitoba R3E 3P5       Fax: (204) 789-3910
Last modified on Monday, 12-Sep-2005 11:55:17 CDT