STAT 330


Introduction to SAS

To run SAS in the Assignment Lab you should turn on the computer if necessary, and let it run through its initial set-up. Then find STAT in the menu, either by using the down arrow key or the mouse. Then press the Enter key, or, if you used the mouse, click the upper left-hand button. Repeat the above to select the correct course number from the menu that appears next. You will then be asked to enter your ID. Your Assignment Lab ID is the first letter of your first name, followed by the first three letters of your surname. For example, mine is RLOC. Then enter your password. Yours is the first seven digits of your SFU ID. You should some time soon, explore the menus to find a way to change your password to something more secure. Once you are signed on, a menu will appear. (This takes quite a while --- in the meantime you will see a message about ``preparing Hard Drive for Windows''.) Using the arrow keys or the mouse, select Windows from the menu. Wait for the windows display to appear, and use the mouse to move the cursor to the SAS icon inside the window toward the lower right part of the screen. Depress the left-hand button on the mouse twice in fairly rapid succession. This will activate SAS for windows. A small hour glass will replace the cursor momentarily if you have succeeded. However, it may revert back to the arrow and appear to be hung up. Be patient. SAS is a large program that may take up to several minutes to get running under heavy simultaneous demand.

If you have a UNIX account with access to Beaufort, you can run SAS on that machine as well. Experienced SAS users often find the UNIX environment more convenient.

Once you get SAS running on a PC, you should have a screen with three coloured windows, called child windows. Two will be plainly visible. These are the log window in grey at the top, and the program editor window in blue at the bottom. You may see a green section between these. This is the output window, hidden in the background behind the other two. When you run a successful set of SAS commands, the green output window will automatically pop to the front.

Practice first moving from window to window. A failsafe way to do this is to use the mouse to move the cursor to the word, Windows, at the top of the screen. Then depress the left-hand button on the top of the mouse. This will make a small menu of windows appear. Move to the one you want, and click the left-hand button. The selected window should become highlighted. Try it with all three. Note how the output window completely hides the others with its large size.

We shall also want to take advantage of some menus for each of the windows. If they are not already displayed, move the mouse to the word, Options, at the top of the screen. Then depress the left-hand button on the top of the mouse. Move the cursor to the item, Preferences in the menu that has just popped up, and click the left-hand mouse button. Then repeat this process, ``clicking'' on Popup and then Save.

Now activate the program editor window. You can use this window to enter your program, and can include the data in the program as well. However, it is typically more convenient to use a separate data file. We shall first use the program editor window to create and store such a file.

Use the mouse if required to position the cursor near the upper left-hand corner of the blue, program editor window, and click the left-hand button. You should aim for the left-hand end of the second line. If the next step fails, then move the cursor down slightly. Enter the following lines of data, moving to the next line down each time with the enter key. To save space, the data are printed below in two sets of two columns each. Your screen should contain one set of two, long columns.

             M  13.3                           F 30
             F  23.2         As one            M 21
             M  24           set of            M  8
             F  22                             F 12
             M  20           t   c             F 23
             M  19           w   o             F 21.7
             F  21           o   l             M 12
             F  26               u             M 18
             M  31           l   m             M 22
             M  20           o   n             M 12
             F  28           n   s             M 16
             F  16           g

Check your work. Use the ``page-up'' and ``page-down'' keys (or the scroll bar if you know how to use it) if necessary. (WARNING: if, when you push page-up or page-down you just get numbers typed check to see if the green light labelled num lock is on; if so find the key marked num lock on the number pad and push it once to make that light go off.) If you find a mistake, use the arrow keys or mouse to move the cursor to the error, and correct it. You can use the delete or backspace keys to delete an error, and can enter an entire new line if you want. Try making a few changes, even if your data entry has been perfect.

Once you are satisfied with the data, you can file them away. To do this, click on the right mouse button while the mouse cursor is located in teh Program Editor window. This will make a menu ``pop up''. Select (with the left mouse button) File from this menu. Then select Save as, followed by Write to file. A new window will appear. Type `` a:raw.dat''. This will tell SAS to use the file name, raw.dat, on the `a' drive (the one with your floppy disk in it), to store your data.

You can now type in SAS commands for analyzing the data. (The data are a shuffled version of the data set on pages 23 and 176 of the SAS System for Elementary Statistical Analysis, and are measurements of percent body fat for 13 males and 10 females in a fitness center.) We shall ask SAS to sort these data by sex, and to make some summary statements for each group.

First, you will want to clear the text from the Program Editor window. Make the menu pop up in that window again. Click on the Edit item when the menu pops up, and then click on Clear text. Now, move the cursor to the start of the first line in the Program Editor window, and type lines of code so as to make the following screen display. Note that a SAS line always ends with a semi-colon.

   options pagesize=60 linesize=80;
   data bodyfat;
   infile 'a:raw.dat';
   input sex $ fatpct;
   proc print;
   run;

If you did not already erase the numbers from the subsequent lines, you can do so now by typing blanks on top of them, or by marking text by dragging the mouse over segments to be deleted with the left-hand button depressed, and then using the delete key to delete the marked portions. Check your entered lines, and edit where needed. Their purpose is as follows:

  1. The options line sets the output to a size suitable for printing on a standard printer.
  2. The data line provides a label. It is mandatory in any SAS analysis.
  3. The infile line tells SAS the name of the file from which to read data. SAS can also read data directly from a program listing with a cards command, or from a previously created SAS file with a special file extension and format. The file name must be enclosed in single quotes. If you forget one of these, SAS will treat all the text following the unmatched quote as character text, not as SAS statements. This includes all subsequent commands submitted later. To get out of this mess, you need to submit a new, single quote. Try to avoid this frustrating error.
  4. The input command tells SAS what variables it is to read from the file. The symbol, $, after the first variable name specifies that it is what is called a character variable. This means that it contains symbols that are used solely to identify an attribute like sex, treatment used, etc. The format of the input command has considerable flexibility and power. The above is the simplest version.
  5. Proc is a SAS abbreviation for a procedure. The print procedure invoked here prints a copy of the data to the output window.
  6. The run command tells SAS to execute the instructions in the previous set of lines.

Once you are satisfied with the program lines, click on the Locals menu item on the Program Editor window, and select the item, Submit to instruct SAS to process these lines. Watch the grey window. It should display a log of your session, including any possible errors. It may take a while. Again, be prepared to wait. If you did not make an error, you should see the green window pop into the foreground. If not, and the contents of the grey window go by too rapidly, you can move the cursor to it (by using the mouse or Windows menu). Then use the page-up key or the scroll bar on the right-hand border of the window to review the previous components of the log. (You use the mouse to activate the scroll bar. Experiment with it if you have never used a scroll bar before, and see if you can get it to work.)

If you made an error, you will have to try again. You can do this by recalling the previously submitted lines. This in turn is done by activating the Program Editor window, and clicking on Locals from its header, and then Recall from the menu that appears. Then try to correct the error. (This is undoubtedly the most frustrating part of using SAS. If a more experienced user owes you a favour or two, you might want to call in some assistance. The SAS manuals are so extensive, that it is hard for the beginner to know where to look for help. In particular, see the comments on unbalanced quotes in item 3 above.)

Assuming that you did not make an error, you should see the data printed in the output window. You can now proceed to the sorting and production of summaries. Move the cursor to near the upper left-hand corner of the Program Editor window, and type lines of code to create the following display:

   proc sort data=bodyfat;
   by sex;
   proc means data=bodyfat;
   by sex;
   var fatpct;
   run;

Check these lines for accuracy and edit where necessary. Their purpose is as follows:

  1. Proc sort is the procedure for sorting data.
  2. The by statement specifies that the sorting is to be done according to the values of the variable, sex.
  3. Proc means produces basic descriptive statistcs.
  4. The by statement instructs SAS to do the calculations for each sex separately.
  5. The var statement tells SAS which variable the summaries are to be computed for.
  6. These are terminated by a run command to instruct SAS to execute the entire set of commands.
Once you are satisfied with the program lines, use the Submit option from the Locals menu in the Program Editor window to submit them.

Unless you goofed, the green output window will have again popped to the foreground. Examine the results. The page-up and page-down keys or scroll bar will help you move up and down through the display.

Now return to the program editor window, and type in the following lines:

   proc chart data=bodyfat;
   vbar fatpct/group=sex;
   title 'Crude Side-by-Side Histograms';
   run;
Then Submit{ them using the Locals menu.

You should by now be able to guess the role of each of the above statements. See if you can predict the results of changing in the second line, (i) vbar to hbar, and (ii) deleting the optional portion, /group=sex. Check your intuition by recalling the former statements to the programme editor, modifying the second line, and re submitting the lines to SAS. To save the results in a file, use the menu bar on the Output window. Click on File, Save as, and then Write to file. Then type the name, a:results. This will save the output in the file named results on your floppy disk in drive `a'.

You can print a copy of the output using the Print option from the File menu on the Output window, as long as you have a printing card for using the local printer. You can also take the diskette with you to amother DOS machine with a printer, and print it there.

You can, of course, also file a copy of your commands, edit them elsewhere, and then, with the Open option in the File menu on the Program Editor window, incorporate them into a subsequent analysis. This is one of the major advantages of SAS. It makes it relatively easy to do repetitive, identical or very similar analyses. A later exercise will give practice in this skill.

When you are finished running SAS, click on the File menu on either the Program Editor or Log window, and select Exit .... Confirm your choice when asked. Then select the File menu from the Program Editor window, and from it, Exit Windows.... Again, confirm your choice. Finally, use the mouse to move to the LOGOUT section, and click the left-hand mouse button, twice if required, and then confirming this choice if asked.

Pages with examples of individual procedures
  1. Doing Simple Linear Regression vi proc glm< /a>. This example also shows normal probability plots and how to save the residuals as a SAS data set.
  2. Doing Multiple Linear Regression vi proc gl m. More residual plots.
  3. Doing one-way ANOVA vi proc anova. Also illustrates multiple comparison procedures.
  4. Doing two way ANOVA vi proc anova. More multiple comparisons.

Manuals

SAS produces an intimidating set of manuals. You may find the following useful:

  1. SAS System for Elementary Statistical Analysis.
  2. SAS Companion for the Microsoft Windows Environment, Version 6, First Edition.
  3. SAS Companion for the UNIX Environment and Derivatives.
  4. SAS/STAT User's Guide, Volumes 1 & 2. (Comprehensive discussion of the procedures for doing statistical analyses.)
  5. SAS/GRAPH Software: Introduction. (Good introduction to the more comprehensive, two-volume user's guide for creating high-quality graphs.)

Written by C. B. Dean and edited by R. Lockhart on Oct 1, 1995


Richard Lockhart
Thu Sep 5 15:37:30 PDT 1996