//VSAMJOB ...job accounting info...
// EXEC SAS,OPTIONS='VSAMREAD VSAMLOAD SYSIN=INSAS'
//INSAS DD *

   /* Allocate the required ddnames for idcams.                 */
filename sysin '&temp' lrecl=80 blksize=6160 recfm=fb;
filename sysprint sysout=*;

%macro vsamchk(dsn);

      /* First make sure cluster exists. If it does, then       */
      /* sysfilrc will be 0; else, sysfilrc will be 10005.      */
   filename myfile vsam "&dsn";
   %if &sysfilrc ne 0 %then
      %put 'vsam file does not exist';
   %else %do;

      /* The file does exist. Now check use idcams to see if    */
      /* there are records. If there are, then sysrc will be 0; */
      /* otherwise, it will be 12.                              */
   data _null_;
      file sysin;
      x=upcase("verify dataset(&dsn)");
         put  @2 x;
   proc idcams;
   run;
   %end;
%mend ;

%vsamchk(your.vsam.cluster);

%macro runcode;
   %if &sysfilrc=0 and &sysrc=0 %then %do;

         /* The file did exist, and it contained records. Put   */
         /* your normal code path that processes the vsam       */
         /* cluster here.                                       */
      %end;
   %else %if &sysfilrc=0 and &sysrc=12 %then %do;

         /* The file did exist, but it was empty. In this       */
         /* example we write a dummy record to the file to      */
         /* handle this situation. Whatever you decide to do,   */
         /* the logic would go here.                            */
      data _null_;
         file 'your.vsam.cluster' vsam;
         put  'add a record';
      run;
   %end;
   %else %if &sysfilrc ne 0 %then %do;

         /* The file did not exist. In this example we          */
         /* write a note to the log and stop the job.           */
         /* whatever you decide to do, the logic would          */
         /* go here.                                            */
      data _null_;
      put 'the file does not exist - stop the job';
      stop;
   run;
   %end;
   %else %do;

         /* This catches all other values of sysfilrc           */
         /* and sysrc. If we get here the user needs to         */
         /* check errors or warnings in the saslog.             */
      data _null_;
         put '*** check the saslog for warnings or errors';
      run;
   %end;

%mend runcode;

%runcode;