Wednesday, March 4, 2015

run time analysis (with -TERMSTMT)

Some batches may take long time. You definitely do not want to wait until it finish. So when you kick off the batch, you can add the system option -TERMSTMT to run the program at below. For the tip -TERMSTMT, please see my previous post.
filename a temp;
filename mymail email "mail@address" subject="The job finished. Please check!";
options nonotes;
data _null_;
    file a;

    start_dttm = "&sysdate9 &systime"dt;
    end_dttm = datetime();
    run_time = end_dttm - start_dttm;

    run_minutes = intck('minute', start_dttm, end_dttm);
    if run_minutes > 15 then do;
        put "data _null_;";
        put "file mymail;";
        put "put 'Run time analysis:';";
        put "put '" "start time: " start_dttm datetime. "';";
        put "put '" "end time: " end_dttm datetime. "';";
        put "put '" "Run time: " run_time time5. "';";
        put "put '" "*** Run minutes ***: " run_minutes "';";
        put "run;";
    end;
run;

%inc a;

INITSTMT/TERMSTMT with %include

Here is scenario: You have to test many batches manually. As the jobs are for different projects, you want to setup environment at the beginning of SAS session, or send out analysis report at the end of SAS session. However, you can not change any existing configuration and program. Here I want to show how I handle them using system options INITSTMT/TERMSTMT.
$ alias sasp='/(saspath)/sas_batch.sh 
-initstmt "%let a=%sysfunc(quote(/work/scripts/initstmt.sas));%inc &a;" 
-termstmt "%let b=%sysfunc(quote(/work/scripts/termstmt.sas));%inc &b;" '

$ sasp code.sas -log code.(timestamp).log
Why I use "quote" function? As the shell may be confused with quotation.