****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_checkpoint.sas  
*
* Created (mm/dd/yyyy): 03/10/2020
* Last modified: 
* Version: 1.0
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*   Records SAS session and preserves the ability to restart QRP at last completed checkpoint                                   
*   
*  Program inputs:                                                                                   
* 
*  Program outputs:                                                                                                                                       
* 
*  PARAMETERS:     
*   - macrocalled: macro that contains checkpoint  
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
*--------------------------------------------------------------------------------------------------
*  CHANGE LOG: 
*
*   Version   Date       Initials      Comment (reference external documentation when available)
*   -------   --------   --------   ---------------------------------------------------------------
*
***************************************************************************************************;

%macro ms_checkpoint(macrocalled=);

    %put =====> MACRO CALLED: ms_checkpoint v1.0;

    %if %sysevalf(&sysver >= 9.4) %then %do; /*SAS 9.4 only*/

	/*Evaluate if a previous error has occured - if &SYSCC >4 then abort*/
    %if %eval(&syscc.>4) %then %do;
	 /* Redirect log and call log checker */
		proc printto log="&MSOC.log_checker.log" new;
        run;
		%ms_logchecker(logdir =&MSOC.);
		   
	 /* Redirect back to main log */
        proc printto log="&MSOC.&RUNID._cida.log";
        run;
		   
        %put ERROR: (Sentinel) An error has occured so processing will not proceed. Please contact SOC;
        %abort;
    %end;
    %else %do;
        
        /*Copy current dplocal and msoc datasets to dptemp and msoctemp*/
        /*At restart datasets in dptemp and msoctemp will replace dplocal/msoc counterparts*/
        proc datasets nowarn noprint lib=dptemp kill; quit;
        proc copy in=dplocal out=dptemp memtype=data; run;
        proc datasets nowarn noprint lib=msoctemp kill; quit;
        proc copy in=msoc out=msoctemp memtype=data; run;
        proc datasets nowarn noprint lib=wrktemp2 kill; quit;
        proc copy in=worktemp out=wrktemp2 memtype=data; run;

        *Delete prior environment and save current SAS environment;
        %if %sysfunc(fileexist(&sasenv.sas_environment.sas)) ge 1 %then %do;
        %let rc=%sysfunc(filename(temp,&sasenv.sas_environment.sas));
        %let rc=%sysfunc(fdelete(&temp));
        %end; 

        filename prescode "&sasenv.sas_environment.sas";  
        proc presenv permdir=sasenv sascode=prescode; 
        run;

        *Write to dplocal.&runid._checkpoints number of checkpoints completed and timestamp;
        %isdata(dataset=dplocal.&runid._checkpoints);
        %if %eval(&nobs.<1) %then %do;
            data dplocal.&runid._checkpoints;
                length macrocalled $32;
                format date date9. time timeampm. macrocalled $32.;
                macrocalled = "&macrocalled.";
                groupnum = &group.;
                date = date();
                time = time();
            run;
        %end;
        %else %do;
            data dplocal.&runid._checkpoints;
                set dplocal.&runid._checkpoints end=eof;
                output;
                if eof then do;
                    macrocalled = "&macrocalled.";
                    groupnum = &group.;
                    date = date();
                    time = time();
                    output;
                end;
            run;
        %end;

        proc copy in=dplocal out=dptemp;
            select &runid._checkpoints;
        run;

    %end;
    %end; /*SAS 9.4 only*/

	%put NOTE: ********END OF MACRO: ms_checkpoint v1.0********;

%mend ms_checkpoint;