For long-running code it can be frustrating when a session ends (either intentionally or unintentionally) and the process must be re-started from the beginning as:
- all of the datasets in the WORK library have been deleted
- previously declared system options have reverted to default
- macro variables have been deleted from the GLOBAL symbol table
- macro program definitions have been deleted from the macro catalog in the WORK library
- format definitions in the WORK catalog have also been deleted
Since SAS 9.4 there has been a simple solution with PROC PRESENV which easily PREServes your ENVironment.
The first stage is to set the GLOBAL system option
options presenv ;
then assign a permanent library to contain the datasets and catalogs
libname bkup 'c:\bkup' ;
This location should NOT contain any other datasets / catalogs as the first stage of the process is to delete anything already in the directory, to be replaced by the current session data.
Assign a file to contain SAS code which the procedure generates to restore the session
filename rest 'c:\bkupcode\presenv_restore.sas' ;
After adding the main body of code add the PROC PRESENV code to create the restore point.
options presenv ; libname bkup 'c:\bkup' ; filename rest 'c:\bkupcode\presenv_restore.sas' ; proc format ; picture hmsd low - high = '-%0H-%0M-%0S' (datatype = time) ; run ; %let tstp = %sysfunc(putn(%sysfunc(time()),hmsd9.)) ; %put &=tstp ; %macro runit ; %put NOTE: This macro does nothing. ; %mend runit ; %runit options nodate ; title 'Change the default TITLE' ; data class ; set sashelp.class ; run ; proc print ; run ; proc presenv permdir = bkup sascode = rest show_comments ; run ;
In a new session open the code generated in the specified restore file and submit it.
The existing code can then be re-started from the restore point, without having to re-run the preceding code.