| CIRCA Micro Services This handout can be found online at: http://www.circa.ufl.edu/handouts/windows/sas.html |
SAS for Windows:
|
SAS for Windows is a software system for data analysis and report writing,
which can include statistical, graphical, mathematical and econometric
procedures. This session guides you through an example using the SAS Display
Manager to create a data file and then to write a SAS program to manipulate
the data.
The SAS system is large and complex. To use SAS effectively, you will need
at least the SAS Language Guide, the SAS Procedures Guide,
and the SAS Companion for the Microsoft Windows Environment, Version
6. This handout assumes you are familiar with Microsoft Windows. If
you are not, then read our handout entitled Using Microsoft Windows
in the CIRCA Labs.
You should always restart or reboot a lab computer before beginning
your work. This protects your disks from computer viruses. To reboot the
computer, press the (Ctrl), (Alt), and (Del) keys once simultaneously.
Wait for a blue screen that asks if you want to remove non-CIRCA programs
from the computer. Answer Yes. When prompted to hit a key, do so.
When you reach the menu screen, select the option, Exit to DOS.
The DOS prompt, C:\user> , should appear. If the DOS prompt does not
appear, ask a CIRCA Operator to help you.
Note: The lab computers have a screen saver installed which will
blank the monitor after a period of inactivity. If this happens, press
any key to view your screen again.
To start SAS for Windows, you must first start Microsoft Windows 3.1. Type
win at the DOS prompt and press the return key. As Microsoft Windows
3.1 starts, you will be asked to select a screen size. Choose the screen
size you prefer. When Microsoft Windows finishes loading, the program group
icon, Applications should be open. If it is not open, open it. Inside
this window is the program icon, SAS System for Windows. Start this
program.
SAS programs are usually written, tested, and run within the SAS Display
Manager. The Display Manager is comprised of windows. The three main windows
are the Program Editor window, the Log window, and the Output window. Each
of these windows is opened when you start a SAS session. During a SAS session,
a program is entered through the Program Editor window. When the program
executes, a list of the actions taken by the program is displayed in the
Log window. Any output from these procedures is displayed in the Output
window.
Each window contains a menu bar and each menu bar is used to issue commands.
Always use the menu bar in the window that the cursor is in. If you do
not, you may change information in the wrong window. If you do not have
a menu bar in every window, be sure to read the next section entitled Preferences.
There are several ways to issue commands in the SAS environment. In the SAS Main window, select the Preference option from the Options menu. This option allows you to set up the SAS environment to suit your needs. For first time users, use the Menu Bar option. Once you have chosen what works best for you, select the Save button and the windows will be set up for you.
Suppose you had the test scores of several students and you wanted to run the SAS procedure called MEANS to determine the maximum, minimum, and mean statistics of these scores. To do this, you will have to create a data file to hold the student scores and a set of program statements to manipulate the data. The Program Editor can create and edit SAS data files and program statements.
A data file needs to be made to hold the students' names and test scores.
You can create a data file by using the Program Editor. Press (F5) to verify
that the cursor is in the Program Editor window. Use the arrow keys to
make sure that the cursor is at the beginning of the first line. Type four
lines of data with one blank space between each item on the lines. Pressing
the Enter key moves the cursor to the next line. The lines of the data
file should look like the lines in the box below.
Later in the session, it will be necessary to know which items are stored
in what columns. So take note: the student names are in columns 1 through
5, the first test scores are in columns 7 and 8, and the second test scores
are in columns 10 and 11. Be sure that your data is in the correct columns.
SAS is sensitive to data in the wrong columns. Making a mistake in the
data file will cause the program to execute improperly.
While in the Program Editor window, follow these steps to save the data to a file.
A message saying that 4 lines were written to an external file should
appear at the top of the window. The data was stored in a file named scores.dat
on the C drive in the directory, user. If you wanted to save the file on
a diskette in the A drive, you will need to change the drive before selecting
the OK button. To change to the A drive, select the arrow next to
the drivers window and choose the A drive icon from the list.
After saving the data file, clear the Program Editor window so that you
can enter your SAS statements. To clear the window, follow these instructions.
The first step in creating a SAS program is the DATA step. The DATA
step is a series of SAS statements that build a new internal data set from
the data file. These statements place the incoming data into a format that
the PROC statements can use. If your original data file is stored in a
separate DOS file from your SAS statements (the usual situation), you must
include at least the statements DATA, INFILE, and INPUT in the DATA Step.
The Program Editor can create these statements. SAS statements are not
case sensitive but the key words are capitalized for emphasis in this example.
The DATA statement will be the first statement in the DATA step. This statement
is used to name the new data set that is created. The new data set will
be called students. Enter the following statement in the Program Editor
window.
DATA students;
The word, DATA, is a SAS statement keyword. The word, students, is the
name for the new data set, and the semicolon ends the statement.
The next statement is the INFILE statement. The purpose of this statement
is to inform the program where to retrieve the data. Enter the following
statement in the Program Editor window.
INFILE 'C:\user\scores.dat';
The word, INFILE, is a SAS statement keyword. The word, scores.dat,
is the file name of the data file. The file name must be enclosed in single
quotes. The file name should include the DOS path to guarantee a successful
read of that file.
The last statement in the DATA step is the INPUT statement. It specifies
the format of how the data will be read. SAS is capable of reading data
in different formats. The most common formats are to read the information
by columns or separated by commas or spaces. This example uses the columns
format. Enter the following statement in the Program Editor window.
INPUT name $ 1-5 test1 7-8 test2 10-11;
Remember the columns in which the data are stored? The column format
of the INPUT statement is the variable name followed by the column range
for each item that is read from the data file. The $ following name indicates
that the value is character rather than numeric. This is a valid specification
for any value that would not be used in numeric calculations.
Each line in scores.dat will be read according to the format of the INPUT
statement and will be output to a SAS observation with the three current
values for the variables name, test1, and test2. The observation is automatically
added to the new SAS data set called students.
Now that you have the statements to create a SAS data set, you can specify
procedures to use the data set. Many SAS PROCs, as they are called, can
be run by specifying only the PROC statement that names the prewritten
procedure. For our session example, the PROC PRINT and the PROC MEANS statements
will be used. Other PROC statements specify the PROC and one or two other
statements and/or options. See the online help for a particular PROC to
determine which statements are required.
The PROC PRINT statement displays the data set, students. The PROC MEANS
statement displays the maximum, minimum, and mean statistics of the test
scores. Enter the following statement in the Program Editor window.
PROC PRINT;
PROC MEANS;
The RUN statement tells SAS to execute the statements that precede it. It is a good idea to use run statements after each DATA step and PROC statement. Each should be entered as:
RUN;
Once all of the RUN statements have been entered, the lines in the Program
Editor window should look like the lines in the box below.
Examine your statements carefully. Does each SAS statement start with a
keyword and end with a semicolon? If you forget to type a semicolon at
the end of a SAS statement, the next statement will be interpreted as a
continuing option of the one before (incorrect or unknown option) and you
will see error messages in the Log window. You can move your cursor to
any part of the text area in the Program Editor window and make changes.
Now, The program is ready to be submitted for execution. To submit your program, follow these directions.
Do not type until the hourglass design disappears. The hourglass icon is shown to let you know that the program is still executing. If the program ran correctly, you will see the Output window. If the Output window does not appear, select Output from the Globals menu.
The output from the execution of the statements will create an internally
formatted SAS data set. The data set will only exist for this session,
unless you specify otherwise. The Output window should contain the output
of the PROC PRINT and PROC MEANS statements. Use the scroll bar to see
the first page of the output.
The PROC PRINT output should look like the text in the box below.
The PROC MEANS output should look like the text in the box below.
If the information in your Output window is different, skip to the section
entitled The Log window.Saving
and printing the output
The contents of the Output window can be printed by using the following
steps.
The contents of the Output window can also be saved in a DOS file for later printing. To do so, follow these instructions.
Select Log from the Globals menu to view the Log window.
The Log window shows the statements submitted and notes about the SAS data
set created. It also shows any errors in the program statements. If you
have errors see the section entitled Errors on the last page.
If you have no errors, it is best to clear the Log and Output windows.
The Log and Output windows are cumulative. The next program executed will
have its output and log information appended to the previous information
in these windows. To clear the Log and Output windows, follow these instructions.
We recommend that you always save your program statements in a DOS file after any major modifications, or before you end your SAS session. Note that once the statements are submitted, they disappear from the Program Editor window. They have not been deleted. They can be retrieved to correct any errors or to rerun the program. To retrieve the program statements, follow these instructions.
To save the program statements, follow these instructions:
Note that the file extension .sas denotes SAS statements, while the file extension .dat denotes a data file and the .lis extension denotes an output file.
To end your SAS session, select Exit from the File menu.
The program will verify the exit request. Select the OK button and
SAS System for Windows will end.
You should return to the Program Manager window in Microsoft Windows 3.1
. To end the windows program, Select Exit from the Files
menu. The Windows environment program will verify the exit request. Select
the OK button and Microsoft Windows 3.1 will end.
You should see the DOS prompt. List the files in your current directory
by typing dir at the DOS prompt. The files scores.dat, scores.sas
and scores.lis should appear in the directory. These files contain your
original data, your SAS program, and SAS output.
For those who want to use the function keys as a short cut to using the menus, a table listing the keys and their functions is shown below. For a complete list, select Keys from the Help menu within SAS for Windows.
|
Key |
Command on |
Function |
|
F1 |
help |
Invokes on-line help window |
|
F4 |
recall |
Brings back last-executed statements |
|
F5 |
pgm |
Moves cursor to Program Editor window |
|
F6 |
log |
Moves cursor to Log window |
|
F7 |
output |
Moves cursor to Output window |
|
|
zoom off, submit |
Unzooms, submits statements for execution |
|
F9 |
keys |
Opens the Keys window |
If your program has errors, you do not have to start all over. You can
read the Log window to find out where your program had problems executing.
It is probably a mistyped word in your program statements or data file.
In either case, clear the Output and Log windows.
If the problem is in the statements, read the section entitled, Saving
the statements, to learn how to retrieve your statements. Once
you have made the changes then resubmit your statement and follow the example
from there.
If the problem is in your data file, read the section entitled Saving
the statements to save your statements and clear the Program Editor
window. Next, open the data file, make the necessary changes and save the
file. Again, clear the Program Editor window and open the statements file.
Start your session at the section entitled Submitting
the SAS program.