/*------------------------------------------------------------*
 * NAME:       %MAKETBL                                       *
 * PURPOSE:    %MAKETBL is a SAS macro that generates a       *
 *             translation table for use in the SORTSEQ=      *
 *             option of PROC SORT.                           *
 * AUTHORS:    Arjen Raateland - FEA, Helsinki, Finland       *
 *             Biff Beers      - SAS Institute, Cary,         *
 *                               North Carolina, USA          *
 * USAGE:      %maketbl(charlist, tblename) ;                 *
 * PARAMETERS:                                                *
 * - CHARLIST: a list of characters in the desired sorting    *
 *             order. SAS Macro quoting rules apply. For      *
 *             example, if the characters ("), ('), or (,)    *
 *             are to be listed, they must be enclosed in     *
 *             the macro function %BQUOTE. No more than 200   *
 *             characters can be listed.                      *
 * - TBLENAME: the name of the translation table to be        *
 *             generated.                                     *
 * NOTES:                                                     *
 *           - This macro can be used independently of the    *
 *             underlying character set. However, the         *
 *             underlying character set is assumed to be a    *
 *             single-byte character set.                     *
 *           - Characters that are listed in CHARLIST should  *
 *             appear in the list only once, but not every    *
 *             character in the underlying character set must *
 *             be listed.                                     *
 *           - Characters that are NOT listed in CHARLIST and *
 *             that occur in the underlying character set     *
 *             before all the characters that are listed      *
 *             remain before those characters in the          *
 *             resulting sorting sequence.                    *
 *           - Other characters not listed in CHARLIST appear *
 *             after all the listed characters in the         *
 *             resulting sorting sequence.                    *
 *------------------------------------------------------------*/
%macro maketbl(charlist, tblename);

  /*----------------------------------------------------------*
   * For each character listed in CHARLIST, this DATA step    *
   * records the position of the character in the underlying  *
   * character set and the desired sorting weight of the      *
   * character based on its relative position in the list.    *
   *----------------------------------------------------------*/

  data listed(keep=pos weight);
    charlist="&charlist";
    len=length(charlist);
    minpos=256;
    do i=1 to len;
      char=substr(charlist,i,1);
      pos=rank(char);
      if pos