Production Ready Macros for SAS Application Developers
mp_stprequests.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Capture session start / finish times and request details
4  @details For details, see http://www.rawsas.com/2015/09/logging-of-stored-process-server.html.
5  Requires a base table in the following structure (name can be changed):
6 
7  proc sql;
8  create table &libds(
9  request_dttm num not null format=datetime.
10  ,status_cd char(4) not null
11  ,_metaperson varchar(100) not null
12  ,_program varchar(500)
13  ,sysuserid varchar(50)
14  ,sysjobid varchar(12)
15  ,_sessionid varchar(50)
16  );
17 
18  Called via STP init / term events (configurable in SMC) as follows:
19 
20  %mp_stprequests(status_cd=INIT, libds=YOURLIB.DATASET )
21 
22 
23  @param status_cd= Use INIT for INIT and TERM for TERM events
24  @param libds= Location of base table (library.dataset). To minimise risk
25  of table locks, we HIGHLY recommend using a database (NOT a SAS dataset).
26  THE LIBRARY SHOULD BE ASSIGNED ALREADY - eg in autoexec or earlier in the
27  init program proper.
28 
29  @version 9.2
30  @author Allan Bowe
31  @source https://github.com/Boemska/macrocore
32 
33 **/
34 
35 %macro mp_stprequests(status_cd= /* $4 eg INIT or TERM */
36  ,libds=somelib.stp_requests /* base table location */
37 )/*/STORE SOURCE*/;
38 
39  /* set nosyntaxcheck so the code runs regardless */
40  %local etls_syntaxcheck;
41  %let etls_syntaxcheck=%sysfunc(getoption(syntaxcheck));
42  options nosyntaxcheck;
43 
44  data ;
45  if 0 then set &libds;
46  request_dttm=datetime();
47  status_cd="&status_cd";
48  _METAPERSON="&_metaperson";
49  _PROGRAM="&_program";
50  SYSUSERID="&sysuserid";
51  SYSJOBID="&sysjobid";
52  %if not %symexist(_SESSIONID) %then %do;
53  /* session id is stored in the replay variable but needs to be extracted */
54  _replay=symget('_replay');
55  _replay=subpad(_replay,index(_replay,'_sessionid=')+11,length(_replay));
56  index=index(_replay,'&')-1;
57  if index=-1 then index=length(_replay);
58  _replay=substr(_replay,1,index);
59  _SESSIONID=_replay;
60  drop _replay index;
61  %end;
62  %else %do;
63  /* explicitly created sessions are automatically available */
64  _SESSIONID=symget('_SESSIONID');
65  %end;
66  output;
67  stop;
68  run;
69 
70  proc append base=&libds data=&syslast nowarn;run;
71 
72  options &etls_syntaxcheck;
73 %mend;