SAS Server Pages

From sasCommunity
Jump to: navigation, search

This page is a sample section from from the book Building Web Applications with SAS/IntrNet®: A Guide to the Application Dispatcher.

The technique discussed here works with both the SAS/IntrNet Application Server as well as the SAS 9 Stored Process Server.

Please do not edit this page as this text reflects what is in the book. Feel free to comment on the discussion tab.

Section 7.6 SAS Server Pages

The use of the externalHTML macro discussed in Section 7.5 suggests that Application Dispatcher programs can be driven by HTML templates. These templates include macro variable references and macro calls that customize the HTML and generate data-driven content. Conceptually, such templates are server pages, i.e., SAS Server Pages.

Note: The HTML template driven application examples presented here work for both the Application Dispatcher as well as the SAS Process Server in SAS®9.

SAS Server Pages contain the HTML text along with commands interpreted by SAS to generate additional data-driven HTML content. The fact that macro variables are resolved and macros are executed by the RESOLVE function provides the functionality needed for SAS Server Pages. The key point is that any macros invoked are used as a text generation facility; but instead of generating SAS code, they generate HTML content. SAS macros can access data set information—both attributes and data—by using the %sysfunc macro function in conjunction with the SCL functions that access data sets. This fact allows macros to generate data-driven HTML content without requiring the generation of any SAS code. To demonstrate how SAS Server Pages work, a sample application is provided that allows the user to page through a SAS data set. This example implementation of SAS Server Pages enables the user to select the following:

  • the SAS data library
  • the SAS data set (from the selected library)
  • variables to include

To run the example in this section, go to the sample environment and select Chapter 7. Then select SAS Server Pages. The results of running this example can be seen in Figures 7.9 through 7.12, later in this chapter.

7.6.1 The SAS Server Page Macro

The first tool needed is a macro that can be invoked directly by the Application Server and that processes a specified SAS Server Page (i.e., an HTML template as discussed above). A macro similar to the externalHMTL macro (discussed earlier), sasServerPage, is included in the sample environment. The sasServerPage macro and the externalHTML macro are different in the following ways:

  • sasServerPage is invoked directly by the Application Server by specifying sasServerPage as the value of the _program name/value pair. For the example presented here, the value of _program is saspress.chapter7.sasServerPage.macro.
  • sasServerPage has no parameters in the macro definition. The type of template and the template location or name are passed in as the name/value pairs Type and Template.
  • sasServerPage cannot specify a fileref (there is no calling program available to define the fileref).

The following two name/value pairs must be specified when invoking the sasServerPage macro:

  • a value for Type
  • a value for Template

These two parameters have the same definition as the corresponding parameters for the externalHTML macro. For the SAS Server Pages link, the values provided are as follows in the saspress.chapter7.sasServerPage.macro.

    . . . .
    &type=catalog&template=saspress.template7.liblist.source

The source code for the macro is shown below:

 
%macro sasServerPage;

 %if %upcase(&type) = CATALOG %then
       %str(filename _srvrpge catalog "&template";); (1)
 %else %str(filename _srvrpge "&template";);

 data _null_;
  infile _srvrpge truncover; (2)
  file _webout;
  input infile $32755.;
  infile = resolve(infile);
  put infile;
 run;

%mend sasServerPage;

proc catalog c = work.sasmacr; (3)
 copy out = saspress.chapter7;
 select sasServerPage / et=macro;
run;
quit;
  1. The templates can be stored either externally or as SAS catalog entries.
  2. The template contents are read in a line at a time; any included macro variable references and macro calls are resolved or executed. The resolved content is written directly to _webout. Note that macro calls must be contained on a single input line.
  3. Store the macro source and the compiled macro in the same catalog. When this code is submitted to SAS for execution (the macro definition and the PROC CATALOG step), the sasServerPage macro will be compiled, stored in WORK.SASMACR, and then copied to a SAS catalog entry. The compiled macro can be invoked directly by the Application Server. Defining macros in catalog entries this way is a Best Practice.

7.6.2 Sample Server Page: List Libraries

The results of processing the following SAS Server Page provides the user with a list of libraries to select from (see Figure 7.9).

Saspress60282_Figure_7-9.JPG.

Figure 7.9 List of Available Libraries Generated Using SAS Server Pages.

<HTML>
<HEAD><TITLE>Sample SAS Server Pages Application</TITLE></HEAD>
<BODY>
<H1>Demonstrate Use of SAS Server Pages</H1> 
Demonstrates how macros can be used inside of HTML text to
customize it to include data values.
<p>This template allows the user to select which library
contains the data to be paged through. Upon clicking the
Submit button, the user is given a choice of
data sets in the selected library.
<hr>
<FORM ACTION="&_url"> 
<INPUT TYPE="HIDDEN" NAME="_PROGRAM" VALUE="&_program">
<INPUT TYPE="HIDDEN" NAME="_SERVICE" VALUE="&_service">
<INPUT TYPE="HIDDEN" NAME="_DEBUG" VALUE="&_debug">
<INPUT TYPE="HIDDEN" NAME="type" VALUE="catalog"> (1)
<INPUT TYPE="HIDDEN" NAME="template" VALUE="saspress.template7.memlist.source)">

<H2>Select the Library:</H2> (2)
%generateOptionTag(data=sashelp.vslib,var=libname,where=upcase(libname) not in ("APSWORK" "MAPS" "SASHELP" "WORK"))) (3)

<P><INPUT TYPE="SUBMIT" VALUE="Get List of Data Sets">
<P>The option tag above was generated by embedding a macro call:
<P>&#160;&#160;&#160;&#160;&#037;generateOptionTag(data=sashelp.vslib,var=libname,where=upcase(libname) not in ("APSWORK" "MAPS" "SASHELP" "WORK"))) (4)
<P>in the template. This macro generates no SAS code.
Instead the macro is used as simple text generation
tool to generate an option tag containing the values
from the input data set.
<P>in the template. This macro generates no SAS code.
Instead the macro is used as simple text generation
tool to generate an option tag containing the values
from the input data set.
<P>The WHERE parameter is used to exclude certain
libraries that will typically not be of interest.
<P>Note the use of html entities for spaces and the %
sign on the macro call (so that the generated text contains
the macro call, instead of the results of the macro call).
</FORM>
</BODY>
</HTML>
  1. Macro variable references will resolve the values defined by the Application Server. Type specifies a catalog entry for the server.
  2. A macro (discussed below) that generates the HTML for a select list will be executed. This produces a list of available SAS data libraries from SASHELP.VSLIB.
  3. The WHERE parameter is used to exclude certain libraries that will typically not be of interest.
  4. As explained in the template text, another format for HTML entities (&#160; for a non-breakable space and &#037; for a % sign) is used to prevent SAS from attempting to resolve the HTML entity &nbsp; (a non-breakable space) as well as forcing %generateOptionTag to be seen as text rather than a macro call.

Note: Virtually all of the text in this page is HTML (and could have been created with an HTML editor). It can be edited to include any necessary macro variable references and macro calls and then stored as a SAS Server Page.

7.6.3 A Macro to Generate Data-Driven SELECT Tags

The generateOptionTag macro was used in the above SAS Server Page in order to generate a list of available libraries. This simple macro can be used to generate an HMTL select list based on the values in any SAS data set. The parameters for the macro are as follows:

  • DATA - the name of the data set which contains the values to be displayed.
  • VAR - the name of the variable containing the values.
  • LABEL - the name of the variable containing the labels for the values. If not provided, the label defaults to the value specified for Var.
  • NAME - the name for the SELECT tag. If no value is provided, the name defaults to the value of Var.
  • WHERE - an optional WHERE clause to subset the values included in the SELECT tag.
  • SELECTED - an optional value that is compared to the values from the data set to set the selected option.
  • otherOptions - a parameter whose value is used as-is in the SELECT tag. This parameter allows the SELECT tag to be customized as needed (e.g., an onClick or size attribute, etc.)

The macro source is described below:

%macro generateOptionTag
      (data=,
       var=,
       label=,
       name=,
       where=,
       selected=,
       otherOptions=
      ); 

 %local dsid varnum varlabel value display;
 %if %length(&where) ne 0 %then %let where = (where = (&where));
 %if %length(&name) = 0 %then %let name = &var;

 %let dsid = %sysfunc(open(&data&where)); (1)
 %let varnum = %sysfunc(varnum(&dsid,&var));
 %if %length(&label) gt 0 %then %let varlabel = %sysfunc(varnum(&dsid,&label));
 %else %let varlabel = &varnum;
 %let vartype1 = %sysfunc(vartype(&dsid,&varnum));
 %let vartype2 = %sysfunc(vartype(&dsid,&varlabel));

<select name = "&name" &otherOptions>
 %do %while(%sysfunc(fetch(&dsid)) = 0);
     %let value = %qsysfunc(getvar&vartype1(&dsid,&varnum));
     %let display = %qsysfunc(getvar&vartype2(&dsid,&varlabel));
<option value="&value" 
%iif(%bquote(&value)=%bquote(&selected),selected)>&display
 %end;
</select> (2)

 %let dsid = %sysfunc(close(&dsid));

%mend generateOptionTag; 

Note: This macro is provided as an example only. It is not intended to be used as a robust tool (e.g., it does no error checking to verify that the data set or variable exists). However, it can be used as the foundation for a macro tool that meets your application needs.

  1. The %SYSFUNC function is used to enable access to data via the SCL data access functions.
  2. The only text generated by the macro is the <select> and </select> tags that surround the loop that reads all the observations in the data set as well as an <option> tag for each value in the data set.

7.6.4 Sample Server Page: List the Data Sets in the Selected Library

The next SAS Server Page lists the data sets in the selected library and is displayed as a result of the user clicking the Submit button after selecting a data library. The selected data library is available as a macro variable (the value of the SELECT tag from the previous page). See Figure 7.10.

Saspress60282-Figure_7-10.JPG

Figure 7.10 List of Available Data Sets in the Selected Library Generated Using SAS Server Pages

<HTML> (1)
<HEAD>
<TITLE>Sample SAS Server Page Application</TITLE>
</HEAD>
<BODY>
<H1>Demonstrate Use of SAS Server Pages</H1>
Demonstrates how macro can be used inside of
HTML text to customize it to include data values.
<p>This template allows the user to select the
data set from a previously selected library. Upon
clicking the Submit button, the user is
allowed to select the variables to display, along
with the number of observations to display at a time.
<hr>
<FORM ACTION="&_url"> (2)
<INPUT TYPE="HIDDEN" NAME="_PROGRAM" VALUE="&_program">
<INPUT TYPE="HIDDEN" NAME="_SERVICE" VALUE="&_service">
<INPUT TYPE="HIDDEN" NAME="_DEBUG" VALUE="&_debug">
<INPUT TYPE="HIDDEN" NAME="libname" VALUE="&libname">
<INPUT TYPE="HIDDEN" NAME="type" VALUE="catalog">
<INPUT TYPE="HIDDEN" NAME="template" VALUE="saspress.template7.varlist.source">

<H2>Select the Data Set from &libname:</H2>
%generateOptionTag(data=sashelp.vstable,var=memname,name=data,where=libname="&libname") (3)

<P><INPUT TYPE="SUBMIT" VALUE="Select Variables">
</FORM>
</BODY>
</HTML>
  1. As in the prior SAS Server Page, virtually all of the text in this page is HTML.
  2. As in the template above, macro variable references will resolve the values defined by the Application Server. Type specifies a catalog entry for the SAS Server Page and template is name of the next SAS Server Page (to select the variables).
  3. The generateOptionTag macro generates a select list of the available data sets in the selected library. The WHERE clause subsets the data to just the observations in sashelp.vstable for the previously selected library.

7.6.5 Sample Server Page: List the Variables in the Selected Data Set

The final SAS Server Page in the example application displays a list of variables and allows users to select which variables they wish to see. When the Submit button is clicked, a modified version of the data set paging program is executed. See Figure 7-11.

Saspress60282-Figure_7-11.JPG

Figure 7.11 List of Variables in the Selected Data Set Generated Using SAS Server Pages.

<HEAD>
<TITLE>Sample SAS Server Page Application</TITLE>
</HEAD>
<BODY>
<H1>Demonstrate Use of SAS Server Pages</H1>
Demonstrates how macro can be used inside of
HTML text to customize it to include data values.
<p>This template allows the user to define which
variables to include. Upon clicking the Submit
button, the user is allowed to page through the
selected data set.
<hr>
<FORM ACTION="&_url">
<INPUT TYPE="HIDDEN" NAME="_PROGRAM" 
       VALUE="%programName(pgm=runtemplate_proc_print,type=source)"> (1)
<INPUT TYPE="HIDDEN" NAME="_SERVICE" VALUE="&_service">
<INPUT TYPE="HIDDEN" NAME="_DEBUG" VALUE="&_debug">
<INPUT TYPE="HIDDEN" NAME="DATA" VALUE="&libname..&data">

<H2>Select Variables from Data Set &libname..&data</H2>
%getVarsAsCheckbox(data=&libname..&data) (2)
<P>All variables included if none are selected.

<H2>Number of Observations Per Page</H2>
<INPUT TYPE="RADIO" NAME="atATime" VALUE="5">5 (3)
<INPUT TYPE="RADIO" NAME="atATime" VALUE="10">10
<INPUT TYPE="RADIO" NAME="atATime" VALUE="15">15
<INPUT TYPE="RADIO" NAME="atATime" VALUE="20">20
<INPUT TYPE="RADIO" NAME="atATime" VALUE="25">25
<P><INPUT TYPE="SUBMIT" VALUE="Page through &data">
</FORM>
</BODY>
</HTML>
  1. The %programName macro points to the program which will run PROC PRINT, allowing the user to page through the data.
  2. Another macro (discussed below) generates checkboxes for the variables in the selected SAS data set.
  3. HTML for a radio box is included to specify the number of observations to display at a time.

7.6.6 A Macro to Generate Checkboxes for Variables in a SAS Data Set

The getVarsAsCheckbox macro generates HMTL checkbox tags for all the variables in a specified SAS data set. The parameters for the macro are as follows:

  • DATA - the name of the data set.
  • NAME - the name for the generated checkboxes. If no value is provided, the name defaults to the value of Var.
  • CHECKED - a non-blank value indicates that the checkboxes are to be selected by default. The default value for this parameter is blank.

The macro source is described below:

%macro getVarsAsCheckbox 
       (data=, 
        name=var, 
        checked=)
        );
 %local dsid nvars varname varlabel i br;

 %if %length(&checked) ne 0 %then %let checked = checked;

 %let dsid = %sysfunc(open(&data)); (1)
 %let nvars = %sysfunc(attrn(&dsid,NVARS));

 %do i = 1 %to &nvars;
     %let varname = %sysfunc(varname(&dsid,&i));
     %let varlabel = %sysfunc(varlabel(&dsid,&i));
     %if %length(&varlabel) = 0 %then %let varlabel = &varname;
 &br<INPUT TYPE="CHECKBOX" NAME="&name" VALUE="&varname" &checked>&varlabel
     %let br = <BR>; (2)
 %end;

 %let dsid = %sysfunc(close(&dsid));

%mend getVarsAsCheckbox;

Note: This macro is provided as an example only. It is not intended to be used as a robust tool (e.g., it does no error checking to verify that the data set or variable exists). However, it can be used as the foundation for a macro tool that meets your application needs.

  1. The %SYSFUNC function is used to enable access to data via the SCL data access functions. The variable names and labels are determined. If there is no label, the variable name is used.
  2. The only text generated by the macro is the <input> tags for each variable in the data set. The variable name is used as the value and label is the display value. The checkbox tags each start on a new line (because of the
    tag).

7.6.7 Program to Page Through the Selected Data Set

The final component of this sample is the program that pages through the selected data set. It is substantially the same as what was discussed in Section 7.5. The complete program is included below with the changes annotated. The results of running this program can be seen in Figure 7.12.

Saspress60282-Figure_7-12.JPG

Figure 7.12 Paging Through PROC PRINT Output.

%setDefaultValue(firstobs,1)
%setDefaultValue(atATime,5)
%setDefaultValue(data,sashelp.class)
%multipleNames(names=var,singleList=y) (1)
%setDefaultValue(Var,_All_)

%let obs = %sysfunc(max(&firstobs + &atATime - 1,1));
%let previous = %sysfunc(max(%eval(&firstobs - &atATime),1));
%let next = %eval(&firstobs + &atATime);

data _null_;
/* this step is used to just make sure the maximum obs is not exceeded */
 if 0 then set &data nobs=nobs;
 call symput('datasetObs',compress(put(nobs,8.)));
 obs = min(&obs,nobs);
 call symput('Obs',compress(put(obs,8.)));
 stop;
run;

 
ods listing close; /* no listing output*/
ods html file = _webout (title="Observations &firstobs-&obs of &datasetObs from &data") style = sasweb;
proc print data = &data(firstobs=&firstobs obs=&obs) label noobs; 
 var &var; (2)
 title "Observations &firstobs-&obs of &datasetObs from &data";
run;
ods html close;

%externalHTML(type=catalog, (3)
              source=saspress.template7.paging_form_controls.source)
             )
  1. The multipleNames macro collapses all the selected values for VAR into a single name. The setDefaultValue macro assigns a default, _All_ if none were selected.
  2. The only change to the PROC PRINT is the addition of the VAR statement.
  3. The template to generate the form controls, including one for VAR, is used.

This example, like the one described in Section 7.5.1, can serve as the foundation for a general purpose tool that allows users to page through any SAS data set.


Copyright 2006, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. Reproduced with permission of SAS Institute Inc., Cary, NC