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