Using the Programming Workspace: SAS Enterprise Guide 4.1 ®
®
For Use with the SAS Certification Prep Guide: Base Prog...
31 downloads
759 Views
954KB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
Using the Programming Workspace: SAS Enterprise Guide 4.1 ®
®
For Use with the SAS Certification Prep Guide: Base Programming for SAS 9 ®
®
The correct bibliographic citation for this manual is as follows: SAS Institute Inc. 2006. Using the Programming Workspace: SAS® Enterprise Guide® 4.1. Cary, NC: SAS Institute Inc.
Using the Programming Workspace: SAS® Enterprise Guide® 4.1 Copyright © 2006, SAS Institute Inc., Cary, NC, USA All rights reserved. Produced in the United States of America. Your use of this e-book shall be governed by the terms established by the vendor at the time you acquire this e-book. U.S. Government Restricted Rights Notice: Use, duplication, or disclosure of this software and related documentation by the U.S. government is subject to the Agreement with SAS Institute and the restrictions set forth in FAR 52.227-19, Commercial Computer Software-Restricted Rights (June 1987). SAS Institute Inc., SAS Campus Drive, Cary, North Carolina 27513. 1st printing, March 2006 SAS Publishing provides a complete selection of books and electronic products to help customers use SAS software to its fullest potential. For more information about our e-books, e-learning products, CDs, and hard-copy books, visit the SAS Publishing Web site at support.sas.com/pubs or call 1-800-727-3228. ®
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.
Using the Programming Workspace: SAS Enterprise Guide 4.1 Introduction ..................................................................................................................................................................2 Exploring the SAS Enterprise Guide Workspace ........................................................................................................2 What Is SAS Enterprise Guide? .............................................................................................................................2 View the Workspace...............................................................................................................................................3 View the Project Explorer .....................................................................................................................................4 View the Project Designer and Process Flow Window .........................................................................................4 View the Task Status Window ................................................................................................................................5 Create a Project for This Tutorial .........................................................................................................................5 Working with SAS Libraries and Data Sets.................................................................................................................6 Open the Server List and View SAS Libraries .......................................................................................................6 Add a SAS Data Set to the Project.........................................................................................................................8 View Data Properties ............................................................................................................................................8 Exploring the Programming Windows ......................................................................................................................10 About the Code Editor Window ...........................................................................................................................11 About the Log Window.........................................................................................................................................11 About HTML and Text Results.............................................................................................................................12 Set Code Editor and Results Options...................................................................................................................13 Creating and Running SAS Programs.......................................................................................................................14 Write and Rename a SAS Program......................................................................................................................15 Run the Program and View the Results ...............................................................................................................17 View the Log ........................................................................................................................................................18 Edit and Rerun a Program ..................................................................................................................................19 Export a SAS Program to an External File .........................................................................................................20 Correcting Errors in SAS Programs ..........................................................................................................................21 Detect Syntax Errors............................................................................................................................................21 Correct Syntax Errors in Programs.....................................................................................................................22
2 SAS Enterprise Guide 4.1
Introduction Before you begin learning the details of SAS programming, you need to be familiar with the software you are going to use. This tutorial contains a series of tasks that show you how to use SAS Enterprise Guide as a programming interface. You can perform these tasks in the software as you read. Because they build on each other, you should do all the tasks in this tutorial in sequence. In this tutorial, you learn to •
use the various windows in the workspace
•
open SAS data sets
•
write and submit SAS programs
•
edit and resubmit SAS programs
•
detect and correct errors in SAS programs
Before beginning this tutorial, you should read Chapter 1, Basic Concepts, in the SAS Certification Prep Guide: Base Programming for SAS9. In Chapter 1, Basic Concepts, you will learn how SAS programs are structured and processed. If you are using the SAS windowing environment rather than SAS Enterprise Guide, you should read the companion tutorial, Using the Programming Workspace: SAS Windowing Environment, instead of this tutorial.
Exploring the SAS Enterprise Guide Workspace In this section you learn about how SAS Enterprise Guise works and about main windows in the workspace.
What Is SAS Enterprise Guide? SAS Enterprise Guide is a Windows client application that serves as an interface to SAS. You use SAS Enterprise Guide to write and submit SAS programs and SAS Enterprise Guide sends them to SAS for processing. When SAS finishes processing the code, it sends the results and log messages back to SAS Enterprise Guide to display.
Using the Programming Workspace 3
SAS can either be on the same computer with SAS Enterprise Guide, or it can be on a remote server. The connection between SAS Enterprise Guide and SAS is set up by your administrator.
View the Workspace If it is not already open, start SAS Enterprise Guide now so you can use the software as you learn more about each window. By default, SAS Enterprise Guide displays the docked view of the following windows in the workspace: Project Explorer, Project Designer, and Task Status. There are additional windows that you open as you work, and other windows that open automatically.
4 SAS Enterprise Guide 4.1
Next, let's take a closer look at each window in the workspace.
View the Project Explorer In SAS Enterprise Guide, the work that you do is saved in projects. A project is a collection of related data, tasks, code, and results. Each time you start SAS Enterprise Guide, you must either create a new project or open an existing project. The Project Explorer displays a hierarchical view of the items in a project. After you submit the code or run a task on your data, the log and any associated results appear as items under the associated code or data item in the project. You can collapse and expand items in a project to hide or display the contents. You can also use pop-up menus to perform actions on items in the project (such as viewing properties, renaming items, or deleting items).
View the Project Designer and Process Flow Window The Project Designer contains one or more Process Flow windows. An empty Process Flow window opens when you create a new project. As you add data, run tasks, and generate output, your work is automatically displayed in the Process Flow window. Each Process Flow window displays a process flow diagram (PFD) for the project. The PFD shows the relationship between items in your project. For example, in the process flow diagram below, you can see that the SofaSales program created a data set, Listing output, and HTML output.
Using the Programming Workspace 5
View the Task Status Window When you run a program in SAS Enterprise Guide, the Task Status window displays messages from SAS about the processing status of each step in the program.
Create a Project for This Tutorial In this task, you create a new project in SAS Enterprise Guide. You will use this project to store your work through the remainder of this tutorial. 1.
Select File Æ NewÆ Project.
2.
Select File Æ Save Project As. In the Save Project To window, click Local Computer.
6 SAS Enterprise Guide 4.1
3.
In the Save As window, navigate to your preferred storage location and type sales in the File name box. Then click Save.
Now that you have set up a project and you are familiar with the windows in your workspace, let's do some work with SAS data sets and libraries.
Working with SAS Libraries and Data Sets In Chapter 1, Basic Concepts, in the SAS Certification Prep Guide: Base Programming for SAS9, you learned that a SAS data set is a file that contains descriptor information and data values. You also learned that SAS files are stored in SAS libraries. In this section of the tutorial, you learn to •
open the Server List and view SAS libraries
•
add a SAS data set to the project
•
view data properties.
Open the Server List and View SAS Libraries SAS data sets are stored in libraries. The libraries that are currently defined for your SAS session are listed under Libraries in the Server List. Although you can define your own libraries using SAS code, in this tutorial you use the default SAS libraries.
Using the Programming Workspace 7
In this task, you open the Server List and view the active SAS libraries. 1.
Select View Æ Server List. SAS Enterprise Guide can access SAS on local or remote servers, and the servers that you have access to are determined by settings in the Administrator portion of the software. In some installations there may be several servers listed in this window.
2.
Double-click Local to open the local server. The first time you open the server, it might take a few moments for SAS Enterprise Guide to establish a connection with SAS.
3.
Double-click Libraries to view the active libraries. By default, SAS defines several libraries when the SAS session begins: Sashelp a permanent library that contains sample data and other files that control how SAS works at your site. This is a read-only library. Sasuser a permanent library that contains SAS files and the Profile catalog that stores your personal settings. This is also a convenient place to store your own files. Work a temporary library for files that do not need to be saved from session to session. In addition, SAS Enterprise Guide might define libraries to use, such as the Local library.
4.
To view the contents of a library, double-click the library name (or right-click the expand icon next to the library name). A list of the SAS data sets contained in the library is displayed.
8 SAS Enterprise Guide 4.1
Add a SAS Data Set to the Project In this task, you add a data set from a SAS library to your project. You can use data without adding it to the project. As you learn SAS programming in the SAS Certification Prep Guide: Base Programming for SAS9, you will learn ways to access data though SAS programming statements. 1.
In the Server List, double-click the SASHELP library and scroll down to find the data set Prdsale.
2.
Double-click Prdsale to open the data set and add it to the project. Notice that when you move your mouse pointer off the Server List, it is hidden on the side of the workspace. You can reopen the Server List by clicking or by moving your mouse pointer over the Server List button.
3.
A shortcut to Prdsale now appears in the Project Explorer window and the data set opens in the data grid.
View Data Properties In this task, you view the properties of a data set. As you learned in Chapter 1, Basic Concepts, in the SAS Certification Prep Guide: Programming for SAS9, the descriptor portion of a SAS data set contains information about the data set, such as the number of observations and variables in the data set, and attribute information (name, type and length) for each variable. 1.
In the Project Explorer window, right-click Prdsale and select Properties from the pop-up menu. The Properties for PRDSALE window opens.
2.
Select General in the selection pane to view the data set's general properties. Notice that the number of columns and rows are listed.
Using the Programming Workspace 9
3.
Select Columns in the selection pane. In the Name column, notice that all the variables for the data set are listed.
Remember that variable (column) names • can be 1 to 32 characters long • must begin with a letter (A–Z, either uppercase or lowercase) or an underscore (_) • can continue with any combination of numbers, letters, or underscores.
10 SAS Enterprise Guide 4.1
4.
Now look at the Type column. There are two types of variables in SAS: character and numeric. Character variables are listed as character in the Type column, and numeric variables are listed as numeric, currency, or date in the Type column. The Length attribute is related to the variable's type. Remember that character variables • can contain letters, numbers, and special characters • use a blank to represent missing values • can be up to 32,767 characters long Numeric variables • can contain only numeric values (the digits 0 through 9, +, -, ., and E for scientific notation) • use a single period (.) to represent missing values • have a default length of 8. Numeric values (no matter how many digits they contain) are stored as floating point numbers in 8 bytes of storage, unless you specify another length.
5.
Finally, look at the Label column. A label is descriptive text of up to 256 characters. Labels are used instead of variable names in some reports.
6.
Click Cancel to close the Properties window.
Exploring the Programming Windows As you learn SAS programming, you will use the Project Explorer window to manage your work, the Code Editor window to write and submit programs, the Log window to view messages about how SAS processed your programs, and the HTML or Listing windows to view the results of your programs. Let's take a closer look at each of these windows.
Using the Programming Workspace 11
About the Code Editor Window You use the Code Editor window to enter, edit, and submit SAS programs. You can open a new Code Editor window by selecting File Æ New Æ Code. The default name for the Code Editor window is Coden. If you rename the code item in the Project Explorer window, the new name appears in the window title bar. Because the title of the window changes each time you insert new code, the general term Editor window is used in the SAS Certification Prep Guide: Base Programming for SAS9 to refer to the Code Editor window.
Text that you enter into the Code Editor window is automatically saved when you close the window. After you submit the program, you can reopen the code that you have submitted by double-clicking the code item in the Project Explorer window. You can also open a SAS program that you have stored and add it to your project. You can open a stored SAS program by using one of the following methods: •
selecting File Æ Open Æ Code and locating the program
•
using the Server List to access a server and then locating the program in the Files folder
About the Log Window A log is generated each time you submit code in SAS Enterprise Guide. The Log window displays messages about your SAS session and about any SAS programs that you submit. You can open the Log window by double-clicking the log item in the Project Explorer window.
12 SAS Enterprise Guide 4.1
About HTML and Text Results If your program includes procedure code that produces output, the results open automatically in SAS Enterprise Guide. By default, all programs that are submitted using SAS Enterprise Guide have programming statements added so that HTML output is created. For example, the following PROC PRINT output is in the default HTML format that is created by SAS Enterprise Guide.
Text output or traditional SAS listing output from the same PRINT procedure is shown in this display. You will learn how to specify this type of output in the next task.
Using the Programming Workspace 13
Set Code Editor and Results Options In this task, you set options for the Code Editor and for the type of results you want to produce. 1.
Select Tools Æ Options. In the selection pane, click SAS Programs. Click Editor Options.
2.
In the Indentation area, select Automatic if it is not already selected. Set other Editor options preferences. For example, if you prefer to display line numbers in the margin of the Code Editor when writing code, select the Show line numbers check box.
14 SAS Enterprise Guide 4.1
For more information about setting options in the Code Editor, press F1 from the Code Editor window. 3.
Click OK to close the Enhanced Editor Options window.
4.
In the selection pane, click Results General.
5.
If it is not already selected, select the Text output check box to add text output to the results when SAS Enterprise Guide processes code. (The HTML check box should also be selected, as shown in this display.)
6.
Click OK to save these options.
You have added a data set to your project and prepared your workspace. In the final section of this tutorial, you write and submit a SAS program and view the output and log.
Creating and Running SAS Programs In this section of the tutorial, you learn to •
write and rename a SAS program
•
run the program and view the results
•
view the log
•
edit and rerun a program
•
export a SAS program to an external file.
Using the Programming Workspace 15
Write and Rename a SAS Program In this task, you open a Code Editor window and type in a SAS program. Then you explore a few of the features of the Code Editor window. 1.
Select File Æ New Æ Code.
2.
The first new code window is named Code and subsequent windows are named Coden. To rename the code, right-click the code item in the Project Explorer window and select Rename from the pop-up menu. You are about to write a program to list data on sales and sofas, so type the name SofaSales.
3.
Type the following program into the Code Editor: data sasuser.mysalesdata; set sashelp.prdsale; if product="SOFA"; run; proc print data=sasuser.mysalesdata; run;
In this program, the DATA step reads the SAS data set Prdsale in the Sashelp library and creates a new SAS data set, Mysalesdata, in the Sasuser library. The new table contains only rows for which the value of the column Product is SOFA. The PROC PRINT step creates a report that lists all the data in the Mysalesdata data set.
16 SAS Enterprise Guide 4.1
When you press the ENTER key at the end of each line, you automatically indent the next line by the amount of space that the current line is indented. You can also indent using the TAB key. 4.
In the Code Editor, notice that SAS keywords are color-coded and the program steps are separated into expandable blocks. Click the minus sign in the margin to collapse the DATA step section. Then click the plus sign to expand the section again.
5.
In lengthy programs, it is helpful to be able to to bookmark a line of code so it is easy to find. You can do this by placing the cursor in the line of code that you want to mark and pressing CTRL+F2. A vertical rectangle in the margin indicates that the line is bookmarked. You can remove the bookmark by placing the cursor in the same line of code and pressing CTRL+F2 again.
6.
To find text in the Code Editor window, select Edit Æ Find or press CTRL+f.
Using the Programming Workspace 17
The Code Editor has many other features that make programming easier. To open the help for the Code Editor, press F1 when you are using the Code Editor window.
Run the Program and View the Results Now that you are familiar with the Code Editor, let's submit your program to SAS for processing. In this task, you submit your program and view the output. 1.
Select Code Æ Run SofaSales on Local or click the Run tool ( ) on the toolbar. Watch the Task Status window for a message that the DATA step is running. The first time you submit a SAS program, it might take slightly longer to process the code because the connection with SAS must be established. Programs that you submit subsequently will process faster.
2.
The HTML – SofaSales window opens automatically after the program runs. View the HTML output from this program.
3.
Take a look at the Project Explorer window. Under the SofaSales code item there are now items for the log, data, and results. Notice that there are items for both HTML and Listing. This is because you specified both HTML and Text output when you set results options. You will need to view Listing output for some programming statements that you will learn in the SAS Certification Prep Guide: Base Programming for SAS9.
18 SAS Enterprise Guide 4.1
4.
In the Project Explorer window, double-click Listing – SofaSales to open the Text output. Scroll through the window and notice that the PRINT procedure has printed all the observations from the Mysalesdata data set. Close the Listing window.
View the Log Each time you submit a program, SAS generates a log that contains messages about processing and any errors that might occur. In this task, you view the SAS log for the program you submitted. 1.
In the Project Explorer window, double-click Log.
2.
Notice that some of the messages in the beginning of the log pertain to code that SAS Enterprise Guide submits to set options and create HTML output.
Using the Programming Workspace 19
To find log messages that pertain to the code that you submitted, scroll through the Log window until you see the beginning of your code. In the Log window shown below, the submitted code begins on line 15.
3.
Notice that there are separate messages for the DATA and PROC steps. The messages for the DATA step tell you the number of observations that SAS read from the Prdsale data set and the number of observations and variables in the new data set Mysalesdata. The messages from the PROC step tell you the number of observations that were read during the PRINT procedure. There are no error messages. You will learn how SAS processes DATA steps and PROC steps in Chapter 6, Understanding DATA Step Processing, in the SAS Certification Prep Guide: Base Programming for SAS9.
Edit and Rerun a Program In this task, you edit your code to make these changes to the output: •
replace the default “The SAS System” title
•
use the column label rather than the column name
•
remove the Obs column
1.
In the Project window, double-click SofaSales to reopen the code that you submitted.
2.
To replace the default title, add the following TITLE statement before the RUN statement for the PRINT procedure: title “Product Sales Report”;
3.
To remove the Obs column and use column labels, add the LABEL and NOOBS options to the PROC PRINT statement as follows: proc print data=sasuser.mysalesdata label noobs;
20 SAS Enterprise Guide 4.1
4.
Your program should look like the one below.
5.
Select Code Æ Run SofaSales on Local. When you edit and resubmit code, SAS Enterprise Guide prompts you to choose whether or not you want to overwrite the previous results. If you overwrite the results, the original code and the log are overwritten by the edited code and new log. If you do not overwrite the results, a new code item is added to the project and the original code is unchanged.
6.
Click Yes in the prompt dialog box to overwrite the original results of the SofaSales program. When processing is complete, look at the HTML output to verify your changes.
As you can see, the Obs column is gone and column labels appear as headings rather than column names. Also, the new title appears at the top of the report.
Export a SAS Program to an External File In this task, you export your SAS program to an external file. 1.
In the Project Explorer window, click the SofaSales item, and select File Æ Export Æ Export SofaSales. In the Export SofaSales To window, select Local Computer.
Using the Programming Workspace 21
2.
In the Export window, select a location and click Save.
When you export code, you are exporting a copy of the code and saving it independently of your project.
Correcting Errors in SAS Programs Programming errors are inevitable, but the Code Editor has features that can help you detect errors before you submit your code. You will learn more about editing and debugging your programs in Chapter 3, Editing and Debugging SAS Programs, in the SAS Certification Prep Guide: Base Programming for SAS9. In this section of the tutorial, you learn to •
detect syntax errors
•
correct syntax errors
Detect Syntax Errors In the Code Editor, color coding for program elements, quoted strings, and comments can help you find programming errors before you run programs. Some syntax errors, such as a missing quotation mark, cause SAS to misinterpret the statements in your program. When you submit a program with unmatched quotation marks, you might not receive an error message in the log. This is because SAS Enterprise Guide automatically submits an ending quotation mark for you. However, you will not get valid results. It is best to learn to detect syntax errors before you submit your programs so that you avoid unexpected results. The Code Editor window uses color coding to help you recognize syntax errors. See the following table for suggestions about using the Code Editor to find syntax errors.
22 SAS Enterprise Guide 4.1
Syntax Error
How to Detect the Error in the Code Editor
Undefined or misspelled keyword
Select Tools Options, and select SAS Programs in the selection pane. Click the Editor Options button, and click the Appearance tab in the Enhanced Editor Options window. Set the file elements Defined keyword, User defined keyword, and Undefined keyword to unique color combinations. When SAS recognizes a keyword, the keyword changes to the defined colors. You'll be able to easily spot undefined keywords by looking for the colors that you selected for undefined keywords.
Unmatched quoted string
Look for one or more lines of the program that are the same color.
Unmatched comment
Look for one or more lines of the program that are the same color.
Unmatched DOEND pair
Place the cursor within a DO-END block and press Alt+[.
Text that follows a quotation mark remains the same color until the string is closed with a matching quotation mark.
Text that follows an open comment symbol ( /* ) remains the same color until the comment is closed with a closing comment symbol ( */).
The cursor moves first to the DO keyword. If one of the keywords is not found, the cursor remains as positioned. When both of the keywords exist, pressing Alt+[ moves the cursor between the DO and END keywords.
Unmatched parenthesis or bracket
Place the cursor on either side of the parenthesis or bracket. Press Ctrl+].
Missing semicolon (;)
Look for keywords that appear in normal text.
The cursor moves to the matching parenthesis or bracket. If one is not found, the cursor remains as positioned.
Correct Syntax Errors When you submit a program, SAS scans each step for syntax errors, and then processes the step (if no syntax errors are found). SAS then goes to the next step and repeats the process. Syntax errors, such as misspelled words in SAS statements, sometimes cause SAS to stop processing the step in which the error occurred. When a program that contains an error is submitted, messages regarding the problem appear in the Log window. When a syntax error is detected, the Log window does the following: •
displays the word ERROR
•
identifies the possible location of the error
•
gives an explanation of the error
For code that does not execute properly, SAS Enterprise Guide also displays a red "X" on the code item in the Project Explorer window.
Using the Programming Workspace 23
In this task, you modify the SofaSales program to intentionally cause an error so that you can see what happens. 1.
In the Project Explorer window, double-click SofaSales to open the program.
2.
In the PROC statement, change print to prin, as shown below.
3.
Select Code Æ Run SofaSales on Local. When prompted to overwrite the previous results, click No. This causes a new code item to be added to the project.
4.
Notice that a red X appears on the new code item in the Project Explorer window, alerting you that an error has occurred. Scroll through the Log window to find the error message.
5.
Open the code that contains the error and correct it by changing prin to print. Then run the program again. This time, click Yes to replace the previous results.
Congratulations! You have learned to use SAS Enterprise Guide for programming. If you want to save the project that you created during this tutorial, select File Æ Save sales. You are ready to move on to Chapter 2, Referencing Files and Setting Options, in the SAS Certification Prep Guide: Base Programming for SAS9.