Writing and executing a SAS Program

Writing a SAS Program

Now that we have looked at various steps in creating a SAS program, the next step is to write one. A SAS statement may begin in any column on a line. However, for the sake of clarity, lines starting with DATA or PROC statements begin in column 1; all other statements are indented in the following SAS programs. In the following example, SAS is asked to create a data set named grades after reading the data steps. The data are embedded within the SAS program. You may use the Notepad or your preferred editor to create the following program, grade1.sas, and save it to a disk.

A SAS program to read the data, and execute some basic descriptive statistics, in a simple form would be:

DATA grades;
   INPUT id 1-2 sex $ 3 test1 4-5 test2 6-7 test3 8-9;
DATALINES;
01f838591
02f657268
03f909490
04f878082
05f788680
;
RUN;

PROC PRINT;
   VAR sex test1 test2 test3;

PROC FREQ DATA=grades;
   TABLES sex test1 test2 test3;
RUN;

If your data contain at least one blank space after each variable value, you can skip the column specifications in the INPUT statement. In this case the program will read the variable ID from columns 1-2 and SEX from column 4, and test1 from column 6-7, test2 from column 9-10, and test3 from column 12-13. The procedure PRINT will print out the variables specified with the VAR (abbreviation for VARIABLES) statement.

DATA grades;
   INPUT id sex $ test1 test2 test3;
   DATALINES;
01 f 83 85 91
02 f 65 72 68
03 f 90 94 90
04 f 87 80 82
05 f 78 86 80
;
/* Other Command lines */
RUN;

This type of format is called free format. Missing values cannot be left blank in this type of data input as the SAS System will fill that blank with the succeeding variable value. Always assign some value to missing values when you use free format. Usually periods "." are used to represent missing values in SAS. With fixed format, you may leave the missing values blank.

Suppose you decided that you want to keep your data file as an external file, then make the following changes to the above program:

DATA grades;
   INFILE 'a:\grade.dat';
   INPUT id 1-2 sex $ 4 test1 6-7 test2 9-10 test3 12-13;
RUN;

PROC PRINT;
   VAR sex test1 test2 test3;

PROC FREQ DATA=grades;
   TABLES sex test1 test2 test3;
RUN;

Replace a:\grade.dat with your own parameter. The INFILE command points out the location of the data file during program execution. Note that the DATALINES statement followed by the data lines are not needed anymore in the above example because the data file is stored as an external file.

Executing a SAS Program

Suppose that you saved the above program into a file, grade1.sas, on your A drive. If you have saved your data file, grade.dat, as an external file let us assume that file is also on drive A.

This opens the SAS AWS window as described in the earlier section of this document. To retrieve the command file, grade1.sas, you created and saved earlier from the SAS AWS window:

  1. select File/Open

Note: If you haven't already created and saved the command file you may move the cursor to the EDITOR window and type in the lines. A dialog box appears which enables you to make an appropriate file selection. Once the file is selected click OK. The file will be opened into the EDITOR window. Next, tell SAS which commands you wish to execute. In SAS you can run an entire program file at once or just portions of it. Select the commands you want by either clicking and dragging over those commands, or select the entire program file by:

  1. select Edit/Select All in the Editor

To run the program you can click the Submit button, right click and open the pop-up menu in the EDITOR window, or go to the main menu and:

  1. select Run/Submit

The program will run and LOG and OUTPUT windows will appear (if there are errors no OUTPUT window will appear).

If there are errors, return to the EDITOR (select Windows/Editor or click anywhere in the EDITOR window), edit the appropriate commands, and resubmit the job.

Note that you do not have to select the text before you use the submit command. If you submit the program without selecting any command specifically, SAS will run the whole program.

If you are entering the command line from the EDITOR window you may execute each command line, instead of waiting to enter the whole program, as described above. You can switch windows using the SAS AWS Windows pull-down menu and making appropriate selections.

You may print the contents of the LOG and OUTPUT window by selecting File/Print from the SAS AWS window menu. You may also save the contents in any of the windows by selecting File/Save from the pop-up menu.

FREQ Statement

PROC FREQ;
   TABLES var1 var2 var1*var2;

This statement produces tables showing distribution of variable values. In the above example SAS will display variable values for var1 and var2, and the combined frequency distributions for var1 and var2. For example, if you wanted to get the gender breakdown for the test scores discussed previously, you would use the following command:

PROC FREQ;
   TABLES sex;

The output generated by this command would look like this:


                                           Cumulative  Cumulative

                SEX   Frequency   Percent   Frequency    Percent

                -------------------------------------------------

                f            5      50.0           5       50.0

                m            5      50.0          10      100.0

MEANS Statement

PROC MEANS;
   VAR var1 var2;

This statement computes descriptive statistics for specified variables. If the VAR statement is omitted, descriptive statistics for each variable in the data set will be calculated.

Again, referring back to the grades data, if you wanted to generate some basic descriptive statistics for the students' test scores, the commands and output would look like this:

PROC MEANS;
   VAR test1 test2 test3;

                The SAS System     10:00 Thursday, November 2, 1995 



       Variable   N          Mean       Std Dev       Minimum       Maximum

       --------------------------------------------------------------------

       TEST1     10    80.1000000    10.4504120    60.0000000    90.0000000

       TEST2     10    82.6000000     8.4616783    72.0000000    96.0000000

       TEST3     10    81.2000000    10.6854002    64.0000000    93.0000000

       --------------------------------------------------------------------

CORR Statement

PROC CORR;
   VAR var1 var2;

A correlation analysis is performed to quantify the strength of association between two numeric variables. For example, if you wanted to see if there were a correlation between student test scores, the commands and output would look like this:

PROC CORR;
   VAR test1 test2 test3;

                     Correlation Analysis



  Pearson Correlation Coefficients / Prob > |R| under Ho: Rho=0  / N = 10



                     TEST1             TEST2             TEST3



   TEST1           1.00000           0.75692           0.91522

                    0.0               0.0113            0.0002



   TEST2           0.75692           1.00000           0.86857

                    0.0113            0.0               0.0011



   TEST3           0.91522           0.86857           1.00000

                    0.0002            0.0011            0.0

ENDSAS statement

By this statement you indicate that there are no more data steps or procedure steps to be read or processed. ENDSAS should be followed by a semicolon (e.g., ENDSAS;). However, an ENDSAS statement during a SAS for Windows session will exit the SAS session and take you to the Microsoft Windows environment.


Next: Sample Data Sets
Up: Table of Contents
Prev: Writing a SAS Program: the PROC Step