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:
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.
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:
INFILE 'c:\temp\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 c:\temp\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 C:\TEMP\ directory. If you have saved your data file, grade.dat, as an external file let us assume that file is also on C:\TEMP directory.
To retrieve the program file, grade1.sas, you created and saved on C:\TEMP directory, launch SAS and follow as below:
- 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:
- 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:
- 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.
You may print the contents of the LOG and OUTPUT window by selecting File/Print from the menu. You may also save the contents in any of the windows by selecting File/Save from the pop-up menu.
Next: Sample Data Sets
Up: Table of Contents
Prev: Writing a SAS Program: the PROC Step



