Introductory SAS Exercises


Contents


This section contains a set of exercises to help you get started using SAS.

Supplemental notes to facilitate use of these exercises are available for the following operating system environments:

Invoke the SAS System

Note: In the discussion which follows, you will be instructed to enter commands on the "Command line" of the Program Editor Window. To open a command line:

Alternatively, if you are working in a windowing environment (Macintosh, MS Windows, or the X Windows System) you can execute these commands by making the appropriate selection from the SAS menus; e.g., to execute the submit command, select submit from the Locals menu.

Create and Execute a SAS Program

Type the following SAS statements in the data area of program editor window; i.e., on the lines below the Command line:

DATA; x=1; PROC PRINT; RUN;

Execute the above program by entering the command submit on the Command line of the Program Editor Window or by selecting Submit from the Locals menu or by clicking on the "Running Figure" icon.

Note: Display Manager System (DMS) commands should be entered on the Command line; SAS code statements should be entered on the data lines which appear below the Command line and not on the Command line.

Note: In SAS, PROC PRINT does not actually "print" anything. Instead, this SAS procedure is used to list the data values for each observation in the program output. To obtain a hardcopy of your results, select Print (if available) from the File menu OR enter FILE 'filespec' on the command line of the Output Window, where filespec is the output file specification (e.g., drive, directory, and filename) that you would like to assigned to the file written to disk and then use the operating system's "print" command to generate the paper copy.

To see the SAS LOG (the window containing error and warning messages), enter LOG on the command line or select Log from the Windows menu. It is recommended that you always check the Log for warning regarding possible problems regarding reading your input data and possible errors associated with running your code.

To restore the Program Editor window, enter Program (or PGM) on the command line or select "Program Editor" from the Windows menu. Click inside the Program Editor window and you are ready to enter a new program.

Modifing Previously Submitted Code

You can retrieve the last submitted SAS Program by entering the command RECALL on the command line of the SAS Program editor (the RECALL function is also available as an option on the Locals menu).

Let's modify the prior program by changing the value assigned to 'x' to the value '2'. The program displayed in the SAS Program Editor window should now look something like this:

DATA; x=2; PROC PRINT; RUN;

Submit this program for execution and observe the results.

Saving a SAS Program

Information in any of the SAS windows can be saved to disk for later recall or printing. For example, after you have finished debugging a SAS program to perform the analyses you require, you may wish to save it to disk so that you can usethe same procedures with other data sets. In other cases you may be on an extensive program and you may wish to periodically save it to disk so that the work you have done will not be lost when you exit from SAS. To save the contents of a window, on the command line, enter the command file followed by the full file specification, in quotes, you wish to assign to the file which is to contain the contents of the active window. For example, in the PC environment, to save the contents of the current window to the file c:\work\myprog.sas, you would enter: FILE 'c:\work\myprog.sas'

To save the program you have been working on as the file 'myfirst.sas', enter the following two commands on the Command line of the Program Editor Window, one at a time:

RECALL
FILE  'myfirst.sas'

Note: Information displayed in any SAS Window can be deleted by using the CLEAR command. For example, to delete results displayed in the Output window, enter the command OUTPUT on the command line of the Program Editor window (alternatively, you can select Output from the SAS Windows menu and then enter CLEAR on the command line of the Output window.

Additional Simple SAS Programs

Enter and execute (submit) the following SAS program:

DATA;
     x=3;
PROC PRINT;

Note: The SAS program was executed and a temporary (in memory) SAS data set was built; however, the results of PROC PRINT were not displayed. SAS requires that a "RUN;" command be included in order to execute the last DATA or PROC step in a Program Editor screen. Otherwise, SAS assumes you have not yet finished entering all associated commands and is waiting for further input.

To execute the last PROC or DATA step, include RUN; in the program editor window and then enter submit on the command line.

SAS is very flexible and relatively forgiving. Enter and execute the following program:

DAT

             ;
                     y

      =         4;   PROC
    PRINT
;RUN        ;

Note: It is NOT recommended that you code your data in this fashion, nor are you likely to do so. This example is only presented to illustrate that SAS is not particular about the way data is entered. It is recommended, however, that you adopt a consistent style of indenting and spacing to make you code easier to read.

Note: Here SAS ran the program even though the DATA was not spelled out fully in the code:

However SAS is not forgiving about all misspellings or incomplete commands. Observe the error message generated by the following statements (note PROC PRINT here is misspelled as PROC PRNT):

DATA ;
   y = 5 ;
PROC PRNT;
RUN;
Error messages of the form "ERROR: Procedure ProcName not found." may also indicate that a component of the SAS System has not been installed. For example, if PROC REG were not found, SAS/STAT has not been loaded and you should contact the system administrator or owner of the machien upon which you are trying to run SAS and ask that they install the SAS/STAT component.

Obtaining Descriptive Statistics for a Set of Variables

Enter the following code in the Program editor window SAS program:

DATA one ;
     INPUT a b c ;
     CARDS ;
     1 2 3
     4 5 6
     7 8 9
PROC MEANS ;

Execute the above program by entering the command submit on the command line.

Note: The SAS program was executed and a temporary (in memory) SAS data set was built; however, the results of PROC PRINT were not displayed. Recall that the "RUN;" command is needed to execute the last DATA or PROC step. Enter the command RUN; in the program area of the Program editor, execute the submit command, and then observe the results.

Read in a Saved SAS Program

SAS can also read in and execute stored programs. Earlier we had save one of our programs as "myfirst.sas"; to bring this file into the program editor window, enter the following on the command line of the Program Editor window:

INCLUDE 'myfirst.sas'

Note: Alternatively, if you are using a Windows based environment, you could use the Open (Read file) option of the file menu to load this file into the Program Editor window.

Now execute the program in the usual fashion and observe that the results are the same as those you had obtained earlier.

We could also invoke the commands contained within a SAS program file without physically copying the file into the Program Editor window by using the %INCLUDE statement. To illustrate use of this statement:

  1. Use the CLEAR command in the OUTPUT, LOG, and Program Editor windows to delete any text they currently contain.

  2. Enter the following in the program area of the Program editor window:
    %INCLUDE 'myfirst.sas' ;  RUN ;
    

  3. Execute the program by typing submit on the command line of the Program Editor window.

  4. Observe the results in the Output and Log windows.

    Using A Previously Defined Data Set in a Procedure

    Recall that had defined a data set with the name 'one' in Obtaining Descriptive Statistics for a Set of Variables. Although this data set may not now be the curently active data set, we can reference it on SAS procedure statements with the DATA= option. For example, to obtain frequency counts for the variables in data set one, we could execute the following SAS program:

    PROC FREQ DATA=one; RUN;
    

    Assigning Commonly Used Functions to Keys

    If you invoke a command from the command line frequewntly, it is often convenient to assign this command to a function key or keyboard shortcut to reduce the amount of repetitive typing. To see a lisitng fo the currently defined key definitions, on the command line, enter:

    KEYS
    

    To assign a new definition simply enter it adjacent to one of the listed undefined keys or type over the definition of an existing definition. It is also possible to assign a sequence of commands to one key. For example, in the above exercises, after we had executed a SAS program, the output window would open and cover the LOG and Program Editor windows. To see the contents of the LOG window, we would enter the Display Manager System (DMS) command LOG, and, then then to return to the Program Editor window we would enter the DMS command Program (which can be abbreviated as PGM). To assign both of these functions to the F12 key, open the KEYS window and type the following in the command definition area adjacent to the F12 key:

    log;pgm
    

    Exit from the SAS Display Manager System

    Exit from SAS by entering BYE on the command line or by selecting Exit from the File menu.

    Virginia Tech Computing Center--Distributed Information Systems
    Last updated: December 8, 1997