/* Example OPEN Routine                                         */

  dsid = open('sasuser.idb_usa', 'V');
   if (dsid=0) then
      do;
      link logerror;
      _status_='H';
      return;
   end;

      /* Check the composite key and rebuild it if needed          */
   else if (ivarlist(dsid, 'prodcomp') ne 'PRODUCT COMPANY') then
      do;
      rc=idelete(dsid, 'prodcomp');
      rc=icreate(dsid, 'prodcomp', 'product company', 'unique');
      if (rc=0) then
         put 'NOTE: Composite index PRODCOMP created'
             'for SASUSER.IDB_USA';
      else
         do;
         put 'ERROR: Composite index PRODCOMP creation'
             'failed for SASUSER.IDB_USA';

         link logerror;
         _status_='H';
      end;

         /* Close data set and use the return code from CLOSE      */
         /* to reset dsid to zero                                  */
      dsid = close(dsid); 
   end;

   return;        /* Last statement executed if _status_='H'       */

LOGERROR:
   emsg=sysmsg();
   rc=sysrc();
   put emsg;
   put 'SYSRC=' rc;
   return;

________________________________________________________________________

   /* Generalizing the OPEN Routine                                */

   ENTRY dsname $ 17  dsid 8  optional = rebuilt 8;

INIT:
   dsid=0;
   rebuilt=0;           /* flags a previous ICREATE attempt        */

OPENUP:
   dsid=open(dsname, 'u');
   if (dsid=0) then
      do;
      link logerror;
      _status_='H';
      return;
   end;

      /* Check the composite key, rebuild it if needed-but no      */
      /* second attempts!                                          */
   else if ( ivarlist(dsid, 'prodcomp') ne 'PRODUCT COMPANY' 
             or ioption(dsid, 'prodcomp') ne 
             'UNIQUE MISSING' ) then do;
      if (rebuilt=0) then
         do;
         link recreate;
         goto openup;             /* back to the top and try again */
      end;
      else
         do;
         put 'ERROR: Verification failed on composite'
             'index PRODCOMP of data set ' dsname;
         put '       PRODCOMP does not exist or is'
             'constructed incorrectly.';

         dsid=close(dsid);
         _status_='H';
      end;
   end;

   return;           /* If _status_ not = 'H', the key PRODCOMP is */
                     /* now in place; dsname is opened for update  */

RECREATE:
   dsid=close(dsid);
   dsid=open(dsname, 'V');
   if (dsid=0) then
      do;
      link logerror;
      _status_='H';       /* return to calling program with dsid=0 */
      return;
   end;

   rc=idelete(dsid, 'prodcomp');
   rc=icreate(dsid, 'prodcomp', 'product company', 'unique');

   if (rc=0) then
      put 'NOTE: Composite index PRODCOMP created for data set '
          dsname;
   else
      do;
      put 'ERROR: Composite index PRODCOMP creation'
          'failed for data set ' dsname;
      link logerror;
      _status_='H';
   end;

   dsid=close(dsid);
   rebuilt=1;                  /* no loops!                        */
   return;

LOGERROR:
   emsg=sysmsg();
   rc=sysrc();
   put emsg;
   put 'SYSRC =' rc;
   return;

MAIN:
   return;
TERM:
   return;

________________________________________________________________________

   /* Calling the OPEN Routine                                     */

   /* caller specifies data set to be opened                       */
dset='sasuser.idb_euro';
dsid=0;
call display('group.catalog.myopen.scl', dset, dsid);
if (dsid < 1) then
   put 'Open for update failed for data set' dset;
else do;
   call set(dsid);