/*------------------------------------------------------------------------*/
/* Sentinel package of SAS code to execute a module at a time             */
/*                                                                        */
/*   called either by spawned or non spawned processes                    %/
/*------------------------------------------------------------------------*/
/* CONTACT INFO:
/* Sentinel Coordinating Center
/* info@sentinelsystem.org
/*
/*-----------------------------------------------------------------------;
*'; *"; *); /**/;

%let &module.start = %sysfunc(datetime());
%put ..&module start time: %sysfunc(putn(&&&module.start,datetime21.2)) ;

%include "&INFOLDER.macros/spawned.sas" /nosource2;
%include "&INFOLDER.&sascode..sas";

%macro do_signature ;
  %if %sysevalf(&syscc. gt 4) %then %do ; %* begin-if execute if last module failed;
  
    %* Print out useful debug info and abort ;
    data _null_;
      put 70*'!';
      put " ";
      put "Post-Module Check 1: System error detected. Process will abort.";
      put "==> Check program log qa_package.log for details.";
      put " ";
      put 70*'!';
    run;
  %end ;%* end-if execute if last module bombed ;
  
  %else %do;
    %let stoptime = %sysfunc(datetime());
    %let stoptimediff = %sysevalf(&stoptime-&&&module.start);
    %let hour = %sysfunc(hour(&stoptimediff));
    %let minute = %sysfunc(minute(&stoptimediff));
    %let second = %sysfunc(ceil(%sysfunc(second(&stoptimediff))));
    /* Create Signature File */
    data signature;
       DP="&DP.";
       ReqID="&ReqID.";
       ProjID="&ProjID.";
       WPType="&WPType.";
       WPID="&WPID.";
       DPID="&DPID.";
       VerID="&VerID.";
       QAVer="&QAVer.";
       SCDMVer="&SCDMVer.";
       Module="&MODULE.";
       OSABBR="&sysscp.";
       OSNAME="&sysscpl.";
       SASVersion="&sysver.";
       SASVersionLong="&sysvlong.";
       RunType="&sysenv.";
       NCPU="&sysncpu.";
       output;
    run;
    proc transpose data=signature out=msoc.&MODULE._signature (rename=(_NAME_=Variable COL1=Value));
       var _ALL_;
       attrib _ALL_ label = ' ';
    run;

    data dplocal.runtimes_&module;
       length module $30 runtime $20;
       Module = "&module";
       StartTime=put(&&&MODULE.START.,e8601dz.);
       StopTime=put(&stoptime,e8601dz.);
       RunTime=cat("&hour", ' h ',
                 "&minute", ' m ',
                 "&second", ' s');
       output;
     run;
  %end;
%mend ;
%do_signature ;

%macro ck_end_qa ;
  %if &end_qa. ne 0 %then %do;
    data _null;
        put 75*'!';
        put ' ';
        put "&module. module has produced data check flags that";
        put "require the QA package to abort. Process will abort after creating a master";
        put "signature file and running the log checker.";
        put ' ';
        put 75*'!';
    run;
  %end;
%mend ;
%ck_end_qa ;