Creating an Alternate Index for an Existing KSDS

You can create an alternate index over an existing KSDS by using IDCAMS using JCL. If the data set already has an alternate index that is defined, it is erased and then redefined. After the alternate index is built, SAS is invoked to read the data set using the alternate index and to write the records to the procedure output file.
//DALTINDX JOB accounting information
//*
//*   Define an alternate key for an existing KSDS.
//*
//STEP1    EXEC  PGM=IDCAMS
//SYSPRINT DD    SYSOUT=A
//*
//*  If an alternate index already exists, delete it.
//*  Then define the alternate index.
//*
//SYSIN    DD    *
DELETE (dsname.KSDS.STUDENT.ALTINDEX) PURGE ALTERNATEINDEX
IF LASTCC=8 THEN SET MAXCC=0

DEFINE ALTERNATEINDEX (name(dsname.KSDS.STUDENT.ALTINDEX ) -
          KEYS(2 69) VOLUMES(xxxx) RECSZ(34 34) -
          RELATE(dsname.KSDS.STUDENT) UPGRADE -   
          REUSE - 
          NONUNIQUEKEY -  
          CISZ(2048) - 
          RECORDS(10 5))
IF MAXCC=0 THEN -
   DEFINE PATH (NAME(dsname.KSDS.STUDENT.PATH ) -
          PATHENTRY(dsname.KSDS.STUDENT.ALTINDEX ))
IF MAXCC=0 THEN -
   BLDINDEX INDATASET(dsname.KSDS.STUDENT ) -
            OUTDATASET(dsname.KSDS.STUDENT.ALTINDEX )

/*
//*
//*  Invoke SAS to read the data set via the alternate index
//*  defined in STEP1.
//*
//STEP2    EXEC  SAS,PARM=' VSAMREAD '
//SYSUDUMP DD  SYSOUT=A
//PATH     DD  DISP=SHR,DSN=dsname.KSDS.STUDENT.PATH
//SYSIN    DD  *

   /* Read the KSDS via the alternate key.  Write the records */
   /* to the procedure output file, putting the observation number   */
   /* before each observation.                                 */

data one;
   infile path;
   input;
   file print;
   put _n_ @5 _infile_;
/*
//
To access the data set by the alternate index, you must have a DD statement that references the data set name in the DEFINE PATH statement. Also note that the STEP2 EXEC statement that invokes SAS specifies the SAS system option VSAMREAD, which is needed only if your installation's default value for this option is NOVSAMREAD.