KEY        MONTH
  ===========   =======
   11000001       01
   21000002       02
   31000001       02
   31000002       02
   31000003       02
   41000001       02
   41000002       03
   51000001       04
   61000001       04

________________________________________________________________________

data keys;
   input kvar $2.;
   cards;
21
31
31
61
;
run;

data test;
   set keys;
   infile ksds vsam key=kvar keypos=pos skip genkey;
   input @ pos key $ month $;
   do while (month eq '02');
      select(_fdbk_);
         when(0) do;
                     put 'record found:  key ='  kvar;
                     put _all_;
                 end;
         when(4) stop;    /* reached the end of file         */
         otherwise do;
                     put 'unexpected error:' kvar= _fdbk_= ;
                     _fdbk_ = 0 ; _error_ = 0 ;
                     stop;
                  end;
      end;
      output;
      input @ pos key $ month $;
   end;
   run;

________________________________________________________________________

data test;
   set keys;
   infile ksds vsam key=kvar keypos=pos skip genkey;
   input @ pos key $ month $;

      /* This is where we check for the duplicate            */
      /* consecutive values of the generic key and force     */
      /* the access method back to direct if there is a      */
      /* duplicate by executing a read with a value that     */
      /* we know will not return a VSAM record. In this      */
      /* example the value is '99'.                          */
   if lag(kvar)=kvar then do;
      temp=kvar;
      kvar='99';
      input;
      _error_=0;
      _fdbk_=0;
      kvar=temp;
      input @ pos key $ month $;
   end;

   do while (month eq '02');
      select(_fdbk_);
         when(0) do;
            put 'record found:  key ='  kvar;
            put _all_;
         end;
         when(4) stop;    /* reached the end of file         */
         otherwise do;
            put 'unexpected error:' kvar= _fdbk_= ;
            _fdbk_ = 0 ; _error_ = 0 ;
            stop;
         end;
      end;
      output;
      input @ pos key $ month $;
   end;
   run;