18
1 Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS, Kennett Square, PA ABSTRACT The SAS ® Institute has a long history of commitment to openness and ease of use in sharing data between SAS software and other systems. The software industry at large has seen a shift towards a more open data exchange; driven by the ease and low cost of data analysis and reporting tools such as Crystal Reports. The name "Crystal Reports" is a well-known name for many developers using the Microsoft Platform. Crystal Re- ports belongs to a class of programs that package formats and data into a report object. Programmers can set up a library of reports that clients can select and run. Some prepackaged reports prompt users for parameter values. A typical report generator builds queries of one or more databases. Changes in database contents change the overall result of the queries, and thus leading to a change in the contents of the reports. This paper will demonstrate how listings and reports can easily be generated out of SAS datasets using Crystal Reports. INTRODUCTION After reading this paper you should be able to generate reports from your SAS datasets using Crystal Reports as a reporting tool. In addition, the data and sample reports used in this paper address the key aspects of report generation. Crystal Reports is a complete reporting solution that helps create, manage, and distribute reports—via the web or embedded in enterprise applications or exported to a number of popular formats, such as spreadsheet, word pro- cessor, HTML, ODBC, and common data interchange formats. Crystal Reports makes it easy to create simple reports, and, it also has the comprehensive tools you need to produce complex or specialized reports. WHAT SOFTWARE DO I NEED? The SAS ODBC driver included with Version 8 (and Version 7) runs under 32-bit Windows (Windows 95, Win- dows 98, and Windows NT). Other software requirements depend on your hardware configuration and on the data source(s) that you want to access. The environment used to work with the examples in this paper is: Data Source: local SAS data only Configuration: Laptop with Windows XP Professional Requirements: Base SAS 8.2, local data access, base SAS software includes the SAS ODBC driver, but in- stalled the driver separately. OPEN DATABASE CONNECTIVITY (ODBC) ODBC is a standard database access method developed by Microsoft Corporation. ODBC makes it possible to access data from any application, regardless of which database management system (DBMS) is handling the da- ta. The functionality of ODBC involves the insertion of a middle layer, called a database driver, between an appli- cation and the DBMS. The purpose of the database driver is to translate the application's data queries into com- mands that the DBMS understands. To access an ODBC database, you must have the appropriate ODBC driver, in the present scenario it is SAS ODBC Driver. To access data provided by a data source, a connection to the data source must first be established. All data access is managed through that connection. Applications Development NESUG 2010

Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

1

Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS, Kennett Square, PA

ABSTRACT The SAS® Institute has a long history of commitment to openness and ease of use in sharing data between SAS software and other systems. The software industry at large has seen a shift towards a more open data exchange; driven by the ease and low cost of data analysis and reporting tools such as Crystal Reports.

The name "Crystal Reports" is a well-known name for many developers using the Microsoft Platform. Crystal Re-ports belongs to a class of programs that package formats and data into a report object. Programmers can set up a library of reports that clients can select and run. Some prepackaged reports prompt users for parameter values. A typical report generator builds queries of one or more databases. Changes in database contents change the overall result of the queries, and thus leading to a change in the contents of the reports. This paper will demonstrate how listings and reports can easily be generated out of SAS datasets using Crystal Reports.

INTRODUCTION After reading this paper you should be able to generate reports from your SAS datasets using Crystal Reports as a reporting tool. In addition, the data and sample reports used in this paper address the key aspects of report generation.

Crystal Reports is a complete reporting solution that helps create, manage, and distribute reports—via the web or embedded in enterprise applications or exported to a number of popular formats, such as spreadsheet, word pro-cessor, HTML, ODBC, and common data interchange formats. Crystal Reports makes it easy to create simple reports, and, it also has the comprehensive tools you need to produce complex or specialized reports.

WHAT SOFTWARE DO I NEED? The SAS ODBC driver included with Version 8 (and Version 7) runs under 32-bit Windows (Windows 95, Win-dows 98, and Windows NT).

Other software requirements depend on your hardware configuration and on the data source(s) that you want to access.

The environment used to work with the examples in this paper is:

Data Source: local SAS data only

Configuration: Laptop with Windows XP Professional

Requirements: Base SAS 8.2, local data access, base SAS software includes the SAS ODBC driver, but in-stalled the driver separately.

OPEN DATABASE CONNECTIVITY (ODBC) ODBC is a standard database access method developed by Microsoft Corporation. ODBC makes it possible to access data from any application, regardless of which database management system (DBMS) is handling the da-ta. The functionality of ODBC involves the insertion of a middle layer, called a database driver, between an appli-cation and the DBMS. The purpose of the database driver is to translate the application's data queries into com-mands that the DBMS understands. To access an ODBC database, you must have the appropriate ODBC driver, in the present scenario it is SAS ODBC Driver. To access data provided by a data source, a connection to the data source must first be established. All data access is managed through that connection.

Applications DevelopmentNESUG 2010

Page 2: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

2

ODBC ARCHITECTURE

The ODBC architecture has four components:

• Application. Performs processing and calls ODBC functions to submit SQL statements and retrieve results.

• Driver Manager. Loads and unloads drivers on behalf of an application. Processes ODBC function calls or passes them to a driver.

• Driver. Processes ODBC function calls, submits SQL requests to a specific data source, and returns results to the application. If necessary, the driver modifies an application's request so that the request conforms to syntax supported by the associated DBMS.

• Data source. Consists of the data the user wants to access and its associated operating sys-tem, DBMS, and network platform (if any) used to access the DBMS.

• The following illustration shows the relationship between these four components.

Fig. 1.0

In order to create a report with Crystal Reports:

- Install SAS ODBC Driver

- Create the SAS ODBC Connection

- Create the Report

Applications DevelopmentNESUG 2010

Page 3: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

3

INSTALL SAS ODBC DRIVER The SAS ODBC Driver for the SAS System allows third party ODBC compliant Windows applications, such as Microsoft’s Excel or Visual Basic or Crystal Reports, open access to SAS data sets. The SAS ODBC Driver is a multi-tiered driver meaning that it does not directly access SAS data sets but rather communicates with either a local SAS ODBC server or a remote SAS/SHARE server.

The SAS ODBC Driver supports both local and network DDE and TCP/IP for connection to local ODBC or remote SAS/SHARE servers.

Download the SAS ODBC driver from www.sas.com, under Service & Support select Downloads, under SAS Sys-tem Software Products, select ODBC Drivers, select SAS ODBC Driver (Release 8.2 or Release 9) and follow download instructions.

After downloading the file, run SASODBC.EXE to install the driver. The SAS ODBC Driver files will be installed to the Windows System directory. The default install location for the SAS ODBC Driver files is:

- V8.2 - C:\Program Files\SAS Institute\ODBC

For complete information about SAS ODBC Driver installation, refer to SAS Institute Inc. “TS-626 Configuring the SAS V8/V9 ODBC Drivers”.

CREATE THE SAS ODBC CONNECTION The following example is based on a Windows XP Professional installation. Other Windows versions may differ slightly in icon location or icon naming.

To create a data source using the SAS ODBC driver:

Fig. 1.1

Double-click on the “Data Sources (ODBC)” icon. The name of the icon may differ a bit for other versions of Windows

Applications DevelopmentNESUG 2010

Page 4: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

4

Fig. 1.2

Fig. 1.3

To add a new data source, se-lect the System DSN tab and Click on the “Add…” button.

Scroll down to SAS ODBC Driver and select “SAS” then click “Finish”. This opens SAS ODBC Driver Configuration dialog.

Applications DevelopmentNESUG 2010

Page 5: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

5

Fig. 1.4

Fig. 1.5

Data Source Name: En-ter a name for the data source that you want to access. Description (Optional): In the field, you can supply a description of the data source

Server: Define one or more servers (as described in the Servers tab), and then come back to the General tab to make a selection from the Server list. You must specify a server for every data source

Records to Buffer: keep the default settings

SQL Options: The default settings for these options work best with the most ODBC-compliant applications. However, you can override the defaults by selecting any of the SQL Options listed. Select the box next to the desired option.

Define SAS Server: In the “Name” field, enter a name for the SAS server that you are defining. If you supply a one-part name such as SAS, the SAS ODBC driver infers that the server is local. If you supply a two-part name such as SU-GI30.SAS, the driver assumes that the server is remote.

Password: Enter the password in the Password field if it is necessary to access the server.

If the server is a SAS ODBC server (used to access local data sources), then Local (Single User) is displayed in the SAS Server Type field. If the server is a SAS®/SHARE server (used by multiple users), SAS®/SHARE (Multi-User) is dis-played Select “Configure”. A confi-guration dialog appears.

Applications DevelopmentNESUG 2010

Page 6: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

6

Fig. 1.6

Fig. 1.7

Local Options: To invoke a local SAS ODBC server, the SAS ODBC driver uses the information in this dialog. If an instance of SAS ODBC server is already running, the driver finds it and connects to it. Each field in this dialog contains default values which can be changed as required.

Libraries Page: Each data source can include multiple data li-braries. Provide information about each library that you want to access.

Name: enter a name for an existing physical SAS data library that you want to access.

Host File: enter the physical name of the library. This must be a valid pathname for the operating sys-tem that your data library is stored on. For example, for a library that is stored on a PC in the Windows envi-ronment, c:\data\costacct would be a valid pathname.

Host File: enter the name of the SAS engine that is required for writ-ing to and reading from this library.

Select [<<Add<<] to save your library information. The Library Name is added to the Libraries list at the left. To include another data li-brary with your data source, repeat above steps.

Applications DevelopmentNESUG 2010

Page 7: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

7

Fig. 1.8

CREATING THE REPORT Step 1: Click the Start Page tab if the Start Page is not already visible in Crystal Reports.

The New Reports area of the Start Page contains a number of wizards to guide you through the creation of specific types of reports. Since you will be learning reporting concepts here, you can skip the wizards and build your report from scratch. After you have completed this report, you may want to build some reports using the wizards to decide which method of report construction you are most comfortable with. Step 2: In the New Reports area, click Blank Report. The Database Expert dialog box appears.

Saving Data Source Defini-tion: Select [OK] to save data source definition and return to the Data Sources dialog (ODBC Data Source Administrator). Newly de-fined data source now appears in the list of Data Sources on the left.

Applications DevelopmentNESUG 2010

Page 8: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

8

Step 3: Click the “+” next to the “ODBC (RDO)” folder to open up the “Data Source Selection” dialog.

Selecting a data Source

The Database Expert provides an integrated tree view of all data sources you can use with Crystal Reports. In the Database Expert, Click the “+” next to the “Create New Connection” folder to open up the folder and list the various data sources it contains.

Next click the “+” next to the “ODBC (RDO)” folder, in the ex-panded view, to open up the “Data Source Selection” dialog.

Highlight the “Data Source Name” from the list. In this scenario NE-SUG2007 and click “Finish”.

Applications DevelopmentNESUG 2010

Page 9: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

9

Step 4:

Step 5:

Expand the NESUG2007 node of the NESUG2007 connection to see a list of tables. Select AES table and click the [>] arrow to add it to the Selected Tables list, and then click [OK]. The Design tab of the report de-signer and Field Explorer appears.

To insert a field Expand the Data-base Fields node in the Field Ex-plorer dialog box and expand table and Highlight a field name by clicking the name once. When you highlight a field name, you can review a subset of the values for that field as well as the field type and size by right-clicking the field and selecting Browse Data from the shortcut menu. Drag the selected field onto the De-tails section of the report. An object frame appears with the Arrow cursor as you drag the field onto the report.

Applications DevelopmentNESUG 2010

Page 10: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

10

There are many options in the Crystal Reports and as this paper is more of an overview we will be able to cover a few of the important options here. For complete information on reports generation using Crystal Reports refer to Crystal Reports XI Release 2 User's Guide.

DESIGN TAB Mostly used part of the Crystal Reports is the Design tab. It designates and labels the various sections of the re-port. You can do the initial formatting, place objects in the sections where you want them to appear, specify sort-ing, grouping, and totaling needs, and so forth. When a field is placed on the report on Design tab, it refers by a frame; it does not associate with the data. This, allows adding and deleting fields and other objects, move them around, set up complex formulas, and more, without connecting to network resources needed to gather the data.

DESIGN TAB AREAS When you first begin creating a report, Crystal Reports automatically creates five areas in the Design tab.

Report Header: This section is generally used for the report title and other information you want to appear at the beginning of the report.

Page Header: This section is generally used for information that you want to appear at the top of each page.

Details: This section is used for the body of the report, and is printed once per record. The bulk of the re-port data generally appears in this section.

Applications DevelopmentNESUG 2010

Page 11: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

11

Report Footer: This section is used for information you want to appear only once at the end of the report (such as grand totals) and for charts and cross-tabs that include data for the entire report. Page Footer: This section usually contains the page number and any other information you want to appear on the bottom of each page. If a group, summary, or subtotal is added to the report, the program creates two additional sec-tions: Group Header: This section typically holds the group name field, and can be used to display charts or cross-tabs that include data specific to the group. It is printed once at the beginning of a group. Group Footer: This section generally holds the summary value, if any, and can be used to display charts or cross-tabs. It is printed once at the end of a group. When a group, summary, or subtotal is added, the Group Header area appears directly above the Details area and the Group Footer area appears directly below the Details area. If you set up additional groups, the program creates new group areas between the Details area and the existing Group Header and Group Footer area(s).

PREVIEW TAB To preview a report before printing it, click Print Preview on the Standard toolbar. The program gathers the data, makes the necessary calculations, and displays the report in the Preview tab. With the data in place, you can review the spac-

Applications DevelopmentNESUG 2010

Page 12: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

12

ing and formatting of your report and see the actual results of all your summaries, formula calculations, and record and group selections.

GROUP EXPERT Using Group Expert you can group fields on report and can modify existing groups. For example, in medical history list-ing data can be grouped by Site and Patient. That way, you can group together all medical history records of a patient together.

SORT EXPERT Using Sort Expert you can define the sort order of the records in a report. You can add and remove a sort field and de-fine the sort direction (ascending or descending) for the data in your report. For example, in medical history listing data is sorted Site and Patient both in ascending order.

FORMULA WORKSHOP: In general you would place the appropriate fields on the report from Field Explorer. Sometimes, however, you need to put data on the report that does not exist in any of the data fields or you want to derive a field based on existing fields. In such cases, you need to create a formula.

Applications DevelopmentNESUG 2010

Page 13: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

13

For example, in the Medical history listing the field CONT has numeric values “1”, “2”, and “.” but on report this field should be displayed with character values “None”, “Ongoing”, and “ ”. This can be achieved by using a formula field which has been derived form MEDHX.CONT field.

Another example is, to display AGE in Demographics listing, which is not available in database. Use DateDiff, Crystal Reports built in function, with the "d" or "y" interval type parameter to find the number of days between two dates BIRTHDT and VISDT. You may also use DateDiff with “yyyy” interval parameter to find the number of years difference between two dates.

Formula Fields: CONT and MHNONE are formula fields. These fields are derived from existing fields based on certain conditions using formulas

Applications DevelopmentNESUG 2010

Page 14: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

14

MORE FORMULA EXAMPLES: Few more examples are listed here. Please refer Crystal Reports XI Release 2 User's Guide for further information on Crystal Reports built in functions. Example 1:

Example 2:

Applications DevelopmentNESUG 2010

Page 15: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

15

EXAMPLES OF FINISHED LISTINGS

Applications DevelopmentNESUG 2010

Page 16: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

16

Applications DevelopmentNESUG 2010

Page 17: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

17

PRINTING, EXPORTING, AND VIEWING REPORTS Finished reports can be distributed using a variety of methods (printing, faxing, exporting). Printing a report You can print reports by selecting Print on the File menu. The Print dialog box appears. Choose the appropriate set-tings, and then click OK. The Printing Report dialog box appears showing the progress of your print job. Another way to do this is to click the Print button on the Standard toolbar. Exporting a report Finished reports can be exported to a number of popular formats, such as spreadsheet, word processor, HTML, ODBC, and common data interchange formats. This makes the distribution of information easier. The exporting process requires you to specify a format and a destination. The format determines the file type, and the destination determines where the file is located. For more details, refer to Crystal Reports XI Release 2 User's Guide. Viewing reports In general, the Crystal Report Viewers are page viewers that let you see complete pages of your Crystal reports. The Crystal Reports Viewer XI provides you with an easy way to view and share your Crystal reports (.rpt files). You can download this viewer for free from Business Objects website.

Applications DevelopmentNESUG 2010

Page 18: Producing Listings and Reports Using SAS® and Crystal Reports · Producing Listings and Reports Using SAS® and Crystal Reports Krishna (Balakrishna) Dandamudi, PharmaNet - SPS,

18

CONCLUSIONS I hope this paper has given you a few insights on creating reports out of SAS datasets using Crystal Reports as a re-porting tool in general. Maybe you even got a few ideas about adopting this method in larger scale. Most impor-tantly, I hope you see clearly that SAS data files are truly portable and universal.

REFERENCES SAS Institute Inc., “TS-626 Configuring the SAS V8/V9 ODBC Drivers”, SAS Institute Inc. Business Objects, “Crystal Reports XI Release 2 User's Guide”, Business Objects,

ACKNOWLEDGMENTS 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.

CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at:

Krishna (Balakrishna) Dandamudi PharmaNet - Specialized Pharmaceutical Services 415 McFarlan Road, Suite 201

Kennett Square, PA 19348-2412 Work Phone: 610.444.4722 Fax: 610.444.4663 E-mail: [email protected]

Applications DevelopmentNESUG 2010