Wednesday, March 28, 2012

A SAS Server Page macro

In recent posts I've talked about a few things that can be a bit easier by packaging them in a macro. In Chapter 5 of SAS® Server Pages: Generating Dynamic Content, I describe a macro that I call sasServerPage that package functionality that I've found that I used repeatedly.

For example, in my blog post Processing External Files with PROC STREAM, I talked about using %let statements to deal with HTML Entities that might be used in your input SAS Server Pages. My sasServerPage macro includes the following %let statements for the most commonly used entities:

%local quot amp apos lt gt nbsp copy reg;
%let quot = "
%let amp = &
%let apol = '
%let lt = <
%let gt = >
%let nbsp =  
%let copy = ©
%let reg = ® 

Doing this for a complete list of HTML (and XML) Entities is propably overkill. But one of the advantages of sample macros is they can be edited/updated as you need to.

Likewise the same blog post also talks about using %include to define the input SAS Server Page. That is something else that can be packaged and simplified using macro parameters. For example, three of the parameters for my macro are used to specify:
  • srvrpgs: The fileref for the aggregate location where my input SAS Server Pages live
  • page: The filename in the aggregate location for the input SAS Server Page
  • defaultExtension: An optional parameter that specifies the default extension on the file name to use if no extension is specified. The default is html. This allows you, for example, to specify HelloWorld, instead of HelloWorld.html as the value of page.
And to address the possibility that you might have different maintenance levels of SAS on different boxes, you could use logic like:

   %if %index(&sysvlong,9.03.01M0)
    or %index(&sysvlong,9.03.01M1) %then
   %do; /* streamDelim not supported before M2 */
       %let streamDelim = %sysfunc(datetime(),z18.);
       %let streamDelim = _&streamDelim;
   %end; /* streamDelim not supported before M2 */ 

and then the following can be added to your PROC STREAM statement (again using macro logic):

   proc stream . . . . resetDelim="&streamDelim";

so the macro will work for 9.3 releases before TS1M2.

Chapter 5 also describe a SAS program that can be invoked as either a Stored Process or a SAS/IntrNet Application Dispatcher program to make it easier to invoke SAS Server Pages in Web environment. That SAS program calls the sasServerPage macro with the parameter values specified in the HTML form, URL (with default values assigned). This allows you to not have to have a distinct program for each and every SAS Server Page you want to produce. So, for example, the follow two URLs illustrate this for the HelloWorld SAS Server Page:
Note how each of these links passes as a parameter the name of the input SAS Server Page.

And now I suspect everyone wants to see the complete source. Unfortunately it is not quite ready for that. I am blogging about the sasServerPage macro and Stored Process/IntrNet program so I can start to use them in some real-world SAS Server Page examples that I will start blogging about shortly.