SAS/IntrNet: Directing Procedure Output to the Web

Một phần của tài liệu web development with sas by example, 2nd edition (2006) (Trang 139 - 147)

You can also use the ODS MARKUP statement to specify a user-defined template, as shown in Chapter 3. In that case, include something like the following code instead of the ODS HTML3 statement:

Example 6.5 Using ODS MARKUP to Customize Procedure Output

/* prepend XHTML template */

libname userlib c:\Documents and Settings\frederick\

My Documents\My SAS Files\9.1 ods path (prepend) userlib.templat;

/* redirect output to client */

ods markup body=_webout tagset=xhtml;

In this case, the XHTML template illustrated in Example 3.19 has been loaded into the SASUSER.TEMPLAT item store, as explained in Chapter 3. The template is then prepended to the ODS markup statement, so that the custom tagset will be used to format the resulting XHTML.

In practice, the page would almost certainly be called from an HTML form with a list of the available regions in order to make sure that the values are spelled correctly. In order to get the list, you could use the Application Dispatcher again, or you could choose to run a query against the SAS data set using htmSQL. How to do this is the topic of the next chapter.

Chapter 6 SAS/IntrNet: the Application Dispatcher 125

References

SAS Documentation

URL references are current as of the date of publication.

ƒ Application Server Debugging –

http://support.sas.com/rnd/web/intrnet/dispatch/debuginp.html

ƒ Configure Application Server –

http://support.sas.com/rnd/web/intrnet/dispatch/appsrv.html

ƒ Configure Broker – http://support.sas.com/rnd/web/intrnet/dispatch/bconfig.html

ƒ Create Default Service –

http://support.sas.com/rnd/web/intrnet/dispatch/defsvc.html

ƒ Programming with the Application Dispatcher –

http://support.sas.com/rnd/web/intrnet/dispatch/procomp.html

ƒ Sample Applications –

http://support.sas.com/rnd/web/intrnet/dispatch/sampapp.html

ƒ Using ODS with the Application Dispatcher –

http://support.sas.com/rnd/web/intrnet/dispatch/ods.html

SAS Publications

ƒ Buffington, Stacy, and Doyle McDonald. 1999. “Collecting Data Via the Internet with SAS/IntrNet and SAS/SHARE Software.” Proceedings of the Twenty- Fourth Annual SAS Users Group International Conference. Cary, NC: SAS Institute Inc.

ƒ Rafiee, Dana. 2001. “Dynamic Web-Based Applications Using SAS/IntrNet Software.” Proceedings of the Twenty-Sixth Annual SAS Users Group International Conference. Cary, NC: SAS Institute Inc.

ƒ Rose, Roderick A. 2001. “The Basics of Dynamic SAS/IntrNet Applications.”

Proceedings of the Twenty-Sixth Annual SAS Users Group International Conference. Cary, NC: SAS Institute Inc.

ƒ Rosenbaum, Andrew. 2002. “A Look at the Development Process for a SAS/IntrNet Application.” Proceedings of the Twenty-Seventh Annual SAS Users Group International Conference. Cary, NC: SAS Institute Inc.

ƒ Slaughter, Susan J., Sy Truong, and Lora D. Delwiche. 2002. “ODS Meets SAS/IntrNet.” Proceedings of the Twenty-Seventh Annual SAS Users Group International Conference. Cary, NC: SAS Institute Inc.

ƒ Timbers, Vincent. 2002. “Connecting the SAS System to the Web: A Hands-on Introduction to SAS/IntrNet Application Dispatcher.” Proceedings of the Twenty-Seventh Annual SAS Users Group International Conference. Cary, NC:

SAS Institute Inc.

ƒ Ullah, Ahsan. 1999. “Customize your WEB output using SAS/IntrNet Software and WEB Publishing Tools.” Proceedings of the Twenty-Fourth Annual SAS Users Group International Conference. Cary, NC: SAS Institute Inc.

C h a p t e r 7

SAS/IntrNet: htmSQL

SAS/IntrNet Data Services 127 htmSQL Directives 128

Generating Tables with htmSQL 130 Creating Forms with htmSQL 133 Issues with CGI Applications 134 References 135

SAS/IntrNet Data Services

One of the most common (and most useful) Web programming tasks is generating dynamic Web pages based on database queries. In addition to the Application Dispatcher, which provides server-side computing services, SAS/IntrNet also offers the htmSQL query tool. This CGI-based utility is the simplest and most powerful database access tool currently available. This chapter offers some easy htmSQL examples that suggest the power and flexibility of this tool.

In order to use htmSQL, it is essential to be familiar with the syntax of the SAS SQL procedure. SQL, the Structured Query Language, is the industry standard for access to relational databases. SAS has provided a super-set of this standard that includes a query language for SAS data libraries. There are many good tutorials available on SQL in general and on the SAS implementation in particular; the reader is referred to the many good online resources, printed books, and manuals.1

1 For a list of several available SQL tutorials, see http://dir.yahoo.com/Computers_and_Internet/

Programming_and_Development/Languages/SQL/Tutorials/.

In addition to knowledge of PROC SQL syntax, htmSQL also requires some familiarity with setting up a SAS/SHARE server. (See Chapter 4, “Remote Access to SAS,” for an introduction to using SAS/SHARE.) In the examples that follow, the SAS data library referenced is located on a UNIX server that is connected to the Web server via a TCP/IP connection. Remember that all database systems, including SAS, require a database engine running as a background process. In this case, the SAS/SHARE server is on a separate platform, with a different operating system, from the Web server. Using the magic of TCP/IP, SAS/IntrNet can run PROC SQL queries against this database engine and display the results in the client Web browser.

htmSQL Directives

The htmSQL tool, like the Application Broker, is a CGI program located on the Web server. (There is an htmSQL.cfg file, analogous to the broker.cfg file, but it is unlikely that you will ever need to modify the default values installed.) You invoke htmSQL by specifying a URL such as one of the following:

ƒ UNIX/Linux: http://<server-name>/cgi-bin/htmSQL/sample.hsql

ƒ Windows IIS: http://<server-name>/scripts/htmsql.exe/sample.hsql

Note that although the htmSQL tool is located in the cgi-bin (or scripts) directory, the Web page itself is stored with an extension of .hsql in the regular document root directory. The default file extension for a Web page containing htmSQL directives is not .html but .hsql. In comparison to the Application Broker, which requires the _service and _program parameters, htmSQL simply opens the .hsql file from the specified location. In other words, unlike the Application Broker, you do not have to make any modifications to htmSQL to get it to run. Just create a Web document, store it in a public HTML directory, and reference it as shown in the preceding example. (Note also that you should not put a question mark (?) before the name of the .hsql page.)

The big difference between an HTML page and an .hsql page is that the latter includes commands that process SQL statements and the resulting queries. These commands are referred to as htmSQL directives. There is a short list of directives to learn, although the specific syntax can get a little complicated. (See http://support.sas.com/rnd/web/intrnet/

htmSQL/syntax.html for a complete list of directives.)

Chapter 7 SAS/IntrNet: htmSQL 129

All directives require an opening and closing tag for each, enclosed in {} brackets. The following four are most often used:

ƒ {query} defines the name of the SAS/SHARE server session; all other htmSQL directives must be contained between {query} and {/query} directives.

ƒ {Sql} contains the SQL query.

ƒ {Eachrow} contains HTML tags to format the query results.

ƒ {Norows} contains HTML text that is displayed if no data is returned by the query.

Within the query section, variable reference directives are used to specify the values to be inserted. When htmSQL encounters a variable reference within an eachrow section, it replaces the tag with the current row value of the named variable or variables. If the reference is before the eachrow section, the values are that of the first row; if after, then the values are that of the last row.

Variable reference directives are of the form {&varname[value]}. Although these may look like macro references, do not confuse the two. Note also that unlike the other htmSQL directives, variable reference directives have no closing tags. The three possible formats for variable reference directives are shown in Table 7.1:

Table 7.1 htmSQL Variable Reference Syntax

Directive Reference

{&varname[n]} the nth value that the variable contains, where n >= 1 {&varname[m..n]} all values from the mth value to the nth value where

m <= 1 < n and the variable contains two or more values {&varname[*]} all values that the variable contains

For example, in the following SQL statement, {sql}

select fname, mname, lname from employee.names {/sql}

the values of the {&varname} directive would be resolved as follows:

ƒ {&varname[1]} = fname

ƒ {&varname[2..3]} = mname lname

ƒ {&varname[*]} = fname mname lname

The most important optional arguments on a variable reference directive are BEFORE=, BETWEEN= and AFTER=; these specify the HTML formatting tags to appear before, between and after the variable values. htmSQL also defines a number of automatic variable references, including date and time variables and SQL-related variables. These have the prefix sys, as in the following examples. The use of date-time and SQL-related

variables is also illustrated in Example 7.1. (A complete list of the automatic variables is available at http://support.sas.com/rnd/web/intrnet/htmSQL/autovar.html.)

ƒ sys.time – the current time of day, based on the Web server system clock

ƒ sys.colname[value] – the name of one or more columns in the query, where [value] can be one of the three choices shown previously for the {&varname}

directive

ƒ sys.qrow – the number of the current row

Generating Tables with htmSQL

In general, the Application Dispatcher can be used to display procedure output and htmSQL the results of SQL queries. As any good PROC SQL programmer knows, however, you can do an awful lot with SQL. The following example replicates the results of Example 6.4 in the previous chapter, using SQL statements instead of PROC

REPORT.

Example 7.1 Summarizing Data with htmSQL

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"

"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">

<head>

<title>SAS IntrNet Examples: htmSQL</title>

<style type="text/css">

body { text-align: center; } caption { font-weight: bold; } h1 { color: blue; } h3 { color: red; }

td { text-align: right; } p.foot { font-weight: bold; } </style>

</head>

<body>

{query server="hygelac:5011"

sapw="user" userid="sas" password="sasuser" } {sql}

select product,

sum(sales) as total label="Total Sales" format=dollar8.

from sashelp.shoes where region='{&region}' group by product

{/sql}

<h1>Shoe Sales by Product</h1>

<hr/>

Chapter 7 SAS/IntrNet: htmSQL 131

{norows}

<h3>No rows selected. Check that the region parameter has been specified correctly.</h3>

{/norows}

<table border="1" cellpadding="4" cellspacing="0"

align="center">

<caption>Region: {&region}</caption>

<tr>

{label var="{&sys.colname[*]}"

before="<th>"

between="</th><th>"

after="</th>"}

</tr>

{eachrow}

<tr>

{ &{&sys.colname[*]}

before="<td>"

between="</td><td>"

after="</td>" } </tr>

{/eachrow}

</table>

{/query}

<hr/>

<p class="foot">Data are current as of {&sys.time}{&sys.ampm}

{&sys.month} {&sys.monthday}, {&sys.year}

</p>

</body>

</html>

The output of this program is very close to that produced using the Application

Dispatcher with PROC REPORT; the difference is that the summarization is done in SQL and the formatting with HTML. The URL for this page is

http://hunding/scripts/htmSQL.exe/example7-1.hsql?region=Africa The Web server in this case is IIS on the Windows platform, while the SAS/SHARE server is on a separate computer that is running the Linux version of the UNIX operating system.

The htmSQL application expects to find the document source in an Apache directory, not in scripts or cgi-bin. Remember, the htmSQL utility is located in the cgi-bin

directory; in this example C:\Inetpub\scripts on the Web server, while the source file Example7-1.hsql is in the document root directory, C:\Inetpub\wwwroot.

Một phần của tài liệu web development with sas by example, 2nd edition (2006) (Trang 139 - 147)

Tải bản đầy đủ (PDF)

(361 trang)