a guided tour through the sas windowing environment · windowing environment, and will demonstrate...

19
A Guided Tour Through the SAS Windowing Environment Casey Cantrell, Clarion Consulting, Los Angeles, CA ABSTRACT The SAS® system running in the Microsoft Windows® environment contains a multitude of tools that can make the process of learning, and using SAS much easier. The interface simplifies communication with the operating system and provides us immediate and familiar access to our data. The programmer may perform common data management tasks with the click of a mouse, customize the working environment and use a variety of tools common to other Windows applications. This workshop will provide an orientation to the SAS windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files. INTRODUCTION The first versions of SAS®, written primarily in FORTAN and Pl/I, were developed to run on IBM mainframe computers. In the mid 1980’s SAS was rewritten in C, a more flexible and durable language. This was an important change, making SAS what it is today: a highly portable, platform independent information delivery system. SAS not only has the capacity to work with foreign file types, such as SPSS or dBase, but SAS programs themselves may be run on a variety of computing platforms. One may combine data in ORACLE from a mainframe with a db2 file from a PC, generate forecasting information using SAS ETS and write out the results into an Excel file. RUNNING SAS PROGRAMS Regardless of platform, there are at least two and typically three steps involved with running SAS programs: Write the program Submit the program for execution View results When run, a SAS program will generate a log, which contains messages from the system about the execution of the program, and an output listing, which contains results of the program, such as tables or statistics if any are produced. SAS programs may be executed in batch mode or within the SAS interactive windowing environment. When running jobs in batch, the programmer writes the program, then submits the program file to the system for execution. System resources are typically shared with other users. Programs, program logs and any results are saved on the system as separate files. When working interactively, the programmer initiates and works within a SAS session, so all activity takes place in SAS. One may write a program in the SAS program editor or open into the editor a program saved as a file. The program log and any output appear in separate windows. The contents of windows are temporary unless specifically saved to a file outside the session. SAS DISPLAY MANAGER Although this workshop will discuss elements that are common to all SAS programs regardless of operating system, we will pay particular attention to features found in the SAS Display Manager running on Microsoft Windows. 1

Upload: others

Post on 27-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

A Guided Tour Through the SAS Windowing Environment Casey Cantrell, Clarion Consulting, Los Angeles, CA

ABSTRACT The SAS® system running in the Microsoft Windows® environment contains a multitude of tools that can make the process of learning, and using SAS much easier. The interface simplifies communication with the operating system and provides us immediate and familiar access to our data. The programmer may perform common data management tasks with the click of a mouse, customize the working environment and use a variety of tools common to other Windows applications. This workshop will provide an orientation to the SAS windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files. INTRODUCTION The first versions of SAS®, written primarily in FORTAN and Pl/I, were developed to run on IBM mainframe computers. In the mid 1980’s SAS was rewritten in C, a more flexible and durable language. This was an important change, making SAS what it is today: a highly portable, platform independent information delivery system. SAS not only has the capacity to work with foreign file types, such as SPSS or dBase, but SAS programs themselves may be run on a variety of computing platforms. One may combine data in ORACLE from a mainframe with a db2 file from a PC, generate forecasting information using SAS ETS and write out the results into an Excel file. RUNNING SAS PROGRAMS Regardless of platform, there are at least two and typically three steps involved with running SAS programs:

• Write the program • Submit the program for execution • View results

When run, a SAS program will generate a log, which contains messages from the system about the execution of the program, and an output listing, which contains results of the program, such as tables or statistics if any are produced. SAS programs may be executed in batch mode or within the SAS interactive windowing environment. When running jobs in batch, the programmer writes the program, then submits the program file to the system for execution. System resources are typically shared with other users. Programs, program logs and any results are saved on the system as separate files. When working interactively, the programmer initiates and works within a SAS session, so all activity takes place in SAS. One may write a program in the SAS program editor or open into the editor a program saved as a file. The program log and any output appear in separate windows. The contents of windows are temporary unless specifically saved to a file outside the session. SAS DISPLAY MANAGER Although this workshop will discuss elements that are common to all SAS programs regardless of operating system, we will pay particular attention to features found in the SAS Display Manager running on Microsoft Windows.

1

Page 2: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

SAS WINDOWS Although you can customize the SAS windowing environment, when you first initiate a session, your screen will usually look something like the one shown in Figure 1.

COMMAND BAR DROP-DOWN MENUSTOOLBAR

ACTIVE WINDOW

Figure 1 – SAS Windows Environment

CURSOR POSITION

• Active window – Although you may open multiple windows, only one will be active at any one time. Commands and actions will work only for the active window, which is typically highlighted in blue on your screen.

• Drop-down menus – Drop-down menus for the active window will appear across the top of your screen. Menus and menu items will change depending on which window is active.

• Toolbar – The toolbar provides icons for actions you perform most often. In addition, you can change the tool icons and/or create your own tools.

• Cursor position – This indicates the position of the cursor in the program editor window.

• Command bar – You may type commands into the command box as an alternative to using menus. The command bar keeps a list of commands previously entered, so you can quickly repeat a command.

2

Page 3: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

Figure 2 – Recalling previous commands

Although three windows appear when you first start your SAS session, five are available. They are:

• Program editor – This is where you will write, edit and submit your programs.

• Log – SAS system message will appear in this window when your program is executed.

• Output window – Listings generated by your program will appear here.

• Explorer window – You may perform various file management tasks, such as opening, moving, and copying files, from this window.

• Results window – This window provides an outline of SAS output generated during the session, making it easy to view, print and save specific tables or listings.

WINDOWS

Figure 3 – Available windows

WORKING WITH THE SAS WINDOWS The Display Manager supports many of the tools common to other Windows applications that allow you modify the layout on your screen. You may open, close, minimize and maximize windows and/or resize and move them to suit your individual preferences.

3

Page 4: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

MINIMIZE

MAXIMIZE

CLOSE

Figure 4 – Tools to resize windows

You may open or reopen any window from the VIEW drop-down menu.

Figure 5 – Using the view menu There are various ways to navigate across windows as well. Clicking anywhere within a window will activate that window. Or you may use the WINDOW drop-down menu (see Figure 3) or the task bars located at the bottom of your screen.

TASK BARS

Figure 6 – Task bars

THE SAS PROGRAM EDITOR The enhanced program editor provides myriad tools to make your job easier. You may expand and collapse individual sections in your program, define your own function keys and record macros to automate redundant editing. The color-coding feature, which allows you to define colors specific to program components, helps locate syntax errors.

4

Page 5: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

In addition to making your programs easier to read, collapsing sections also makes it easy to differentiate steps, since all statements in the step, except for those on the first line, will collapse when this features is used. Remember that steps are processed separately in SAS. You may not mix PROC steps with DATA steps.

COLLAPSED

NOT COLLAPSED

Figure 7 – Collapsing program steps

Although SAS provides a set of pre-defined function keys, these, too are completely customizable. To open the DMKEYS window, type KEYS into the command box. You may type a command into the definition of any unused key or change the definition of an existing function key. If you wish, you can combine commands by separating them with a semicolon.

KEY PERFORMS TWO ACTIONS

Figure 8 – Customizing function keys

Similarly, the keyboard macro tool can be used to automate repetitive editing tasks. The macro can store commands or even keystrokes. In the examples shown in Figure 9, the $ character needs to be added to each format name.

5

Page 6: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

ADD $

Figure 9 – Using the Macro recorder

To do this by hand, one needs to position the cursor at the beginning of the format name, e.g., “NEEDLANG”, type “$”, then move the cursor to the next line and repeat the process. To do this with one key stroke, let’s create a keyboard macro.

First, select “Keyboard Macros” from the TOOLS menu. Then select “Record New Macro”. An image of a cassette tape will appear in the window as will the message (“Recording”) as shown in Figure 11.

Figure 10 – Invoking the Macro recorder

Since the cursor is already positioned at the beginning of each format name, we now need only to enter the necessary keystrokes to change a single format,. We’ll type $, press the down arrow key to move the cursor to the next line, then press the left arrow key to move the cursor back one space.

Figure 11 – Macro recorder in record mode

6

Page 7: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

To stop recording, return to the “Keyboard Macros” menu, and select “Stop Recording”.

Figure 12 – Stopping the Macro recorder

We can now save the macro and give it a name or assign a function key to use it.

Figure 13 – Saving a keyboard macro

USING THE SAS EDITOR TO WRITE PROGRAMS You may edit your SAS program using any text editor and then import it into your SAS session, but most SAS Windows programmers find it easier to write programs in the Program Editor itself. The “cut and paste’ features familiar to Windows users work in the SAS editor and many common editing actions are available from the “EDIT” drop-down menu as well. To begin working in the program editor, click anywhere in the window titled “Editor”. Alternately you may type “Prog” in the command line or access the window through a menu. Remember, although you may have several windows open, you may only work in one window at a time. The name of the active window will be highlighted in blue. Figure 14 shows a simple program that creates a temporary SAS file named “pups”. The file will contain 5 records or observations and 4 variables: name, sex, weight1 and weight6. As indicated by the $, name and sex will be character variables.

7

Page 8: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

Figure 14 – Simple SAS program Again, there are various ways to execute your program. You may select SUBMIT from the Run menu, type “Submit” in the command window or press the Submit icon in the toolbar.

Figure 15 – Using the Run menu

SUBMIT COMMAND SUBMIT ICON

Figure 16 – Issuing the SUBMIT command or using the SUBMIT icon

8

Page 9: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

THE PROGRAM LOG WINDOW All too often, programmers look for output, assuming that their program ran successfully if listings were generated. This will inevitably get you into trouble, so make it a habit to review the program log first. In this example, there were no syntax errors, but let’s look more closely at the information the log provides. Does the output file contain the number of records and number or variables we expect?

Figure 17 – The SAS program log

The program shown in Figure 18 failed because of a spelling error.

Figure 18 – Spelling error as shown in the program log To correct the mistake, we will bring the program back into the Program Editor by typing “recall” into the command box, or by using the “Recall last submit” item under the RUN menu.

INFORMATION ABOUT DATA SET

Figure 19 – Recalling the last program executed

9

Page 10: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

ERROR

CORRECTION

Figure 20 – Correcting the error

THE OUTPUT AND RESULTS WINDOWS Now that we have built a SAS system file, we can use any of the numerous procedures available in SAS to get information from it. Most, but not all PROCs generate output that we can view in the Output window. Let’s run a PROC PRINT to get a listing of the file. When we submit the program, the Output window will automatically become the active window and will appear in the foreground on our screen. Note that the Results window now contains an object labeled “Print: The SAS System”. Although we can scroll through the output in the Output window, this can become quite tedious once we’ve run multiple programs, since the listings will remain there until we clear the contents. The Results window provides an outline of the output generated during our session, making it easy to find a particular item.

PRINT OUTPUT

Figure 21 – Output listed in the Results window

Let’s run another procedure to see how this works.

The program shown in Figure 22 generates frequencies for the variables in our file. Since there are only four variables, we can see them all quite easily. If however, we had hundreds of lines of output and wanted to locate a particular table, we could do this quickly using the Results window.

Figure 22 - PROC FREQ

10

Page 11: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

Figure 23 – Output generated from PROC FREQ

Clicking on the item labeled “Freq: The SAS System” displays a list of every table generated by that PROC. Clicking on any item in the list will take us to that specific table in the Output window.

Figure 24 – Output generated from PROC FREQ

To clear the contents of any window, select “Clear all” from the Edit drop-down menu, or type “clear” into the command box. Make certain the window you wish to clear is the active window by clicking anywhere in that window. Remember the active window will move to the front of the screen and its name bar will be highlighted.

Figure 25 – Clearing contents of Output window from EDIT menu

FREQUENCY TABLES

11

Page 12: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

COMMAND BOX

Figure 26 – Clearings contents of Output window from command line

SAS SYSTEM FILE NAMES All SAS data files have two part names. The first part refers to the LIBRARY, or folder where the physical file is, or will be, stored. The second part is the FILENAME, which identifies the data set in the folder. When used in a program, the two parts are separated by a dot, i.e. LIBRARY.FILENAME. SAS system files may be temporary, meaning they exist only for the duration of the program or session, or they may be stored as permanent files, which may be used later. Temporary files are stored in a library called WORK that SAS defines automatically when the session is started. When our sample program ran successfully, the SAS log contained the following NOTE: The data set WORK.PUPS has 5 observations and 4 variables.

:

NOTE

Figure 27 – Temporary SAS file written to WORK directory

12

Page 13: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

Since we did not use a two part name in our program, SAS wrote the file to the default WORK library. Consequently, our file will be erased when we terminate our SAS session. THE EXPLORER WINDOW The Explorer window provides various tools for working with SAS data sets. Although it is particularly useful for managing multiple files, we’ll use it now to look at the data set created above. First click on the Libraries icon in the Explorer window. This will open a window showing the libraries currently available to us. Since our file was written to the Work library, click on that icon to see the contents of the library.

WORK LIBRARY

Figure 28 – Active libraries displayed in the Explorer window

The icon displayed indicates the file is a SAS system file.

SAS FILE

Figure 29 – Contents of WORK library Click on the “Pups” icon to open the file. Note that the menu bar now has a different set of icons available. We are currently in Table View mode, which enables us to see the entire data set.

13

Page 14: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

Figure 30 – VIEWTABLE

Let’s explore further. Figure 31 shows options available in the “View” menu that control how the file is displayed.

Figure 31 – Options available from the View menu

Clicking the Column Attributes icon displays information about each variable in the file, as shown in Figure 32.

.

Figure 32 – Column Attributes window

TABLE VIEW

SORT

FORM VIEW

TABLE ATTRIBUTES

EDIT MODE

BROWSE MODE

COLUMN ATTRIBUTES

14

Page 15: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

We can add labels, formats and informats from this screen.

Figure 33 – Adding a format from the Column Attributes window We can also

change label fonts or colors if we wish.

Figure 34 – Changing font and color from the Column Attributes window

Clicking the Table Attributes button displays information about the entire file.

Figure 35 – Table Attributes window

15

Page 16: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

Form View displays records one at a time.

Figure 36 – Form View window

The Edit and Browse buttons control access to the file. The default mode is Browse. You must toggle the Edit mode icon to sort or make changes to the data.

Figure 37 – Edit mode

Several editing actions are available from the “Edit” pull down menu.

Figure 38 –File menu Edit options To sort the file, select the appropriate sort order key to reach the Sort screen where you then the select sort order variables.

ASCENDING

DESCENDING

Figure 39 – Selecting sort order

16

Page 17: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

To select variables you want to sort by, highlight the variable name in the “Available” box, then press the right arrow key to move it into the “Selected” box. To remove the variable from the Selected list, press the left arrow key.

Figure 40 – Selecting sort order variables SAS LIBRARIES SAS system files may be temporary or they may be stored as permanent files. Temporary files exist only for the duration of the program or session, while permanent files may be used later. To create a permanent file, you need to define a SAS library, which tells SAS where you want to store your SAS files. To define your library interactively, click on the New Library icon in the toolbar or right click on the Libraries icon in the Explorer window.

Figure 41 – Defining SAS libraries

Right click the Libraries icon, then select New.

SELECT

NEW LIBRARY ICON

LIBRARIES ICON

Figure 42 – Using the Libraries icon to define a SAS library

17

Page 18: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

Either method will bring up the dialogue box pictured in Figure 43.

Type the desired library name into the “Name” box. Library names cannot exceed 8 characters, must begin with a letter or underscore and may include only letters, numbers or underscores.

Figure 43 –New library dialogue box

You may type in the path information, or click the Browse key shown in Figure 43 to locate the appropriate folder interactively as shown in Figure 44

Figure 44 – Using the Browse option to define a SAS library folder

If you want the library defined each time you start a SAS session, check the “Enable at startup” box.

Figure 45 – Automatically defining a SAS library at startup

18

Page 19: A Guided Tour Through the SAS Windowing Environment · windowing environment, and will demonstrate how to edit and submit programs, manage output, and build and save SAS system files

CONCLUSION In this workshop, we have demonstrated the use of several tools available in the SAS Display Manager. SAS has integrated features common to other Windows applications into an environment that provides a sophisticated and intuitive editor, quick and easy data management tools and a logical system for organizing output. The programmer who understands and uses these tools will certainly experience increased productivity. REFERENCES SAS Institute (1999) SAS Companion for the Microsoft Windows Environment, Version 8, Cary, NC: SAS Institute Inc.

TRADEMARK SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other brand and product names are trademarks of their respective companies. AUTHOR CONTACT Casey Cantrell Clarion Consulting 4404 Grand View Blvd. Los Angeles, CA 90066 [email protected]

19