As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


The multipleNames Macro

From sasCommunity
Jump to: navigation, search

When building Web applications it is possible for a given parameter to have multiple values (e.g., a select tag which allows multiple choices, or a series of checkboxes with the same name). When these values are passed to a SAS/IntrNet Application Dispatcher program or to a SAS Stored Process, the SAS server creates multiple macro variables by suffixing a number to the original name. The server also creates a macro variable that contains the number of values by suffixing a 0 to the original name.

However, if no value are selected or if only value value is selected, the server has no way of knowing that the variable could have had multiple values. This presents a challenge to Application Dispatcher or Stored Process programs as they have to handle each of the following cases:

  • no value is selected which means no macro variables are created.
  • only one macro variable is created - the variable with a suffix of 0 which should contain the number of values is not created.
  • multiple values are created.

The multipleNames macro (below) addresses this by ensuring that the same macro variables are created regardless of how many values are selected.

  • when no value is selected, the suffix 0 variable is set to 0 and the macro variable without the suffix is assigned a null value
  • when only value is select, the suffix 0 variable is set to 1 and both the original macro variable and the macro variable with a suffix of 1 in its name is created (with the same value).
  • when multiple values are created, the macro does not change them

The macro can also collapse all the values into a single variable. This can be useful when the values are to be used in, for example, and IN clause in a WHERE statement.

The macro has two parameters:

  • names: a blank delimited list of names that can potentially have multiple values
  • singleList: a non-blank value causes the macro to collapse all the values into a blank delimited list. A value of Q or q causes the list of values to be quoted.
%macro multipleNames
      (names=,     /* the list of macro variable names */
       singleList= /* combine all the values into a single macro var */
      );
 
/*-------------------------------------------------------------------*/
/*            Web Applications with SAS/IntrNet(R):                  */
/*            A Guide to the Application Dispatcher                  */
/*          by Don Henderson, Henderson Consulting Services          */
/*       Copyright(c) 2006 by SAS Institute Inc., Cary, NC, USA      */
/*                   SAS Publications order                          */
/*                   ISBN 978-1-59994-189-9                          */
/*-------------------------------------------------------------------*/
/*                                                                   */
/* This material is provided "as is" by SAS Institute Inc and        */
/* Henderson Consulting Services Inc.  There are no warranties,      */
/* expressed or implied, as to merchantability or fitness for a      */
/* particular purpose regarding the materials or code contained      */
/* herein. Neither the Institute nor Henderson Consulting Servcies   */
/* are responsible for errors in this material as it now exists or   */
/* will exist, and neither the Institute nor Henderson Consulting    */
/* Services provide technical support for it.                        */
/*                                                                   */
/* The code is based upon code in the Henderson Consulting Services  */
/* Toolkit and is described in Section 5.2.4 of the subject book.    */
/*                                                                   */
/*-------------------------------------------------------------------*/
/* Date Last Updated: September 2006                                 */
/*-------------------------------------------------------------------*/
/* Questions or problem reports concerning this material may be      */
/* addressed to the author:                                          */
/*                                                                   */
/* SAS Institute Inc.                                                */
/* Books by Users                                                    */
/* Attn: Don Henderson                                               */
/* SAS Campus Drive                                                  */
/* Cary, NC   27513                                                  */
/*                                                                   */
/* If you prefer, you can send email to:  sasbbu@sas.com             */
/* Use this for subject field:                                       */
/*     Comments for Don Henderson                                    */
/*                                                                   */
/* Alternatively, the author may be contacted by selecting the       */
/* Contact Me link for the Sample Environment available on the web   */
/* at http://hcsbi.com/IntrNetAppDev                                 */
/*-------------------------------------------------------------------*/
 
 %local i name instance quote;
 
 %let i = 1;
 %let name = %scan(&names,&i,%str( ));
 
 %do %while (&name ne );   /* loop over all the names */
 
     %global &name &name.0 &name.1; /* make sure the name as well as the
                                       Suffix 0 and Suffix 1 names exist */
 
     %if %length(&&&name.0) = 0 %then
     %do;  /* Suffix 0 name does not exist */
         %if %length(&&&name) ne 0 %then %let &name.0 = 1;
         %else %let &name.0 = 0;
         %let &name.1 = &&&name; /* both &name1 and &name can be used */
     %end; /* Suffix 0 name does not exist */
 
     %if %length(&singleList) ne 0 %then
     %do;  /* combine all the values as the value of the original HTML name */
         %if %upcase(%substr(&singleList,1,1)) = Q %then %let quote = 1;
         %else %let quote = 0;
         %if &quote and &&&name.0 ne 0 %then
         %do;
             %let &name = %sysfunc(quote(&&&name));
             %let &name.1 = %sysfunc(quote(&&&name.1));
         %end;
         %do instance = 2 %to &&&name.0;
             %if &quote %then %let &name = &&&name %sysfunc(quote(&&&name&instance));
             %else %let &name = &&&name &&&name&instance;
         %end;
     %end; /* combine all the values as the value of the original HTML name */
 
     %let i = %eval(&i + 1);
     %let name = %scan(&names,&i,%str( ));
 
 %end;
 
%mend multipleNames;