Creating an Alternate Index for an ESDS

You can create an alternate index for an ESDS.
The following example shows the IBM AMS IDCAMS job that is needed to create an alternate index for an ESDS. An IDCAMS job can be submitted using JCL or in a SAS macro variable. The following is an example of TSO DEFINE commands in a macro variable. The example, Creating an Alternate Index for an Existing KSDS, shows how to submit an IDCAMS job from JCL.
An alternate index cannot be created if the ESDS is created with the REUSE statement.
   
    /* Remove alternate index if it exists. */
x "delete ('dsname.esds.myindex') purge alternateindex";

   /* Define an alternate index in the cluster 'dsname.esds'.   */
   /* Use the two-letter state code as the alternate index key. */
%let def = %str(define aix %(name('dsname.esds.myindex') );

   /* Relate the index to the cluster entry STUDENT. */
%let def=&def %str(relate('dsname.esds.student') );

   /* Specify the record size.                       */
%let def=&def %str(recsz(19 19) );
%let def=&def %str(shareoptions(2,3) );
%let def=&def %str(volumes(xxxxxx) );
%let def=&def %str(reuse);

   /* Specify NONUNIQUEKEY because there is more than one record */
   /* with the same state code.                                  */
%let def=&def %str(nonuniquekey );
%let def=&def %str(records (10 5) );
%let def=&def %str(cisz(2048) );

   /* Use the two-letter state code that is offset 69 bytes      */
   /* in each record.                                            */
%let def=&def %str(keys(2 69)%) );

   /* Define a path as a reference to the alternate index.       */
%let path=%str(define path %(name('dsname.esds.student.path') );
%let path=&path %str(pathentry('dsname.esds.myindex')%) );

   /* Build the index from data contained in INDATASET,          */
   /* and put it into the index specified in OUTDATASET.         */
%let bld = %str(bldindex indataset('dsname.esds.student') );

%let bld=&bld %str(outdataset('dsname.esds.myindex') );

%sysexec &def;
%sysexec &path;
%sysexec &bld;

   /* Assign a fileref to the index path.                        */
filename mypath 'dsname.esds.student.path ' disp=shr;

   /* Read the data from the data set in alternate index order.  */
data aixtest;
   infile mypath  vsam;
   input id $9. lastname $10. frstname $10. address $25. city $15.
         state $2. zip $5. balance $5. gpa $4. class $2. hrs $2.
         finaid $1.;
run;

   /* It is a good practice to clear filerefs when you are        */
   /* done with them, but it is not necessary.                    */
filename mypath  clear;