SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irmst_get_model_group_maps.sas
1/*-----------------------------------------------------------------------
2 * NAME: irmst_get_model_group_maps.sas
3 *
4 * PURPOSE: Get model group maps based on the name and version of
5 * modeling system(s)
6 *
7 * NOTES: Assumption to pass tm dimension from Segment-level model
8 * groups to loan-level model group is that Segment-level
9 * insttype = loan-level insttype in the model group mappings.
10 * The dimension is used later to expand instid in a portfolio
11 * data set
12 *
13 * MACRO OPTIONS:
14 * model_group_kind - Model group kind to retrieve:
15 * SCORING / EVALUATION
16 * (see NOTES for the assumption made)
17 * ms_sk - SK of modeling system
18 * add_seg_lvl_ms_sk - Name of additional Segment-level modeling
19 * system
20 * out_model_group_map - Name of output table
21 * DEFAULT: work.model_group_map
22 * mipurl - MIP URL to make rest API calls
23 * tgt_ticket - Specify generated TGT
24 * debug - OPTIONAL: For debugging API calls and
25 * keeping intermediate tables
26 *
27 * MACRO PARAMETERS FOR WORKGROUP SWITCHING:
28 * host workgroup outVarTicket restartLUA
29 * server authMethod outSuccess clearCache
30 * port outds outResponseStatus
31 *
32 * USE CASES:
33 * 1) To get only Segment-level model group maps:
34 * - Specify the Segment-level modeling system SK in ms_SK
35 * - Specify the SAME Segment-level modeling system SK in
36 * add_seg_lvl_ms_sk
37 * 2) To get loan-level model group maps without information about
38 * TM dimension:
39 * - Specify the loan-level modeling system SK in ms_sk
40 * - Leave add_seg_lvl_ms_sk blank
41 * 3) To get loan-level model group maps with information about TM
42 * dimension:
43 * - Specify the loan-level modeling system SK in ms_sk
44 * - Specify the Segment-level modeling system SK in
45 * add_seg_lvl_ms_sk
46 *
47 *-----------------------------------------------------------------------*/
48%macro irmst_get_model_group_maps(
49 model_group_kind = ,
50 ms_sk = ,
51 add_seg_lvl_ms_sk = ,
52 out_model_group_map = work.model_group_map,
53 mipurl = ,
54 tgt_ticket = ,
55 debug = false,
56 host = ,
57 server = ,
58 port = ,
59 workgroup = ,
60 authMethod = token,
61 outds = work._tmp_workgroups_,
62 outVarTicket = ,
63 outSuccess = 0,
64 outResponseStatus = ,
65 restartLUA = Y,
66 clearCache = Y
67 );
68
69 %local num_seg_mg
70 tm_model_name
71 tm_model_sk;
72
73 /*-------------------------------------------
74 * Ensure correct workgroup before proc http
75 *-------------------------------------------*/
76 %irm_rest_set_mip_workgroup(host = &host.,
77 server = &server.,
78 port = &port.,
79 workgroup = &workgroup.,
80 authMethod = &authMethod.,
81 tgt_ticket = %superq(tgt_ticket),
82 outds = &outds.,
83 outVarTicket = &outVarTicket.,
84 outSuccess = &outSuccess.,
85 outResponseStatus = &outResponseStatus.,
86 debug = &debug.,
87 restartLUA = &restartLUA.,
88 clearCache = &clearCache.
89 );
90
91 /*--------------------------------------------------
92 * Access data sets related to MIP model group maps
93 *--------------------------------------------------*/
94 %irm_rest_get_ticket(
95 url = &mipurl./rest/modelGroupMaps,
96 ticketType = st,
97 tgt_ticket = %superq(tgt_ticket),
98 outVarTicket = service_ticket,
99 %if %qupcase(&debug.) eq TRUE %then
100 debug = true;
101 %else
102 debug = false;
103 );
104
105 filename mgmjson temp;
106
107 proc http url = "&mipurl./rest/modelGroupMaps?%str(&)ticket=&service_ticket."
108 out = mgmjson
109 method = 'GET'
110 expect_100_continue;
111 %if %upcase(&debug.) eq TRUE %then
112 debug level = 2;;
113 headers "Accept" = "application/json"
114 "Content-Type" = "application/json";
115 run;
116
117 libname mgm_json JSON fileref = mgmjson;
118
119 /*-------------------------------------------------------------------------
120 * Build Model Map Table and obtain number of Segment-level model groups
121 *-------------------------------------------------------------------------*/
122 %let num_seg_mg = 0;
123
124 proc sql;
125 create table work.model_group_map_tmp as
126 select input(a.id, best32.) as modeling_system_sk,
127 a.name as modeling_system_name,
128 a.entityVersion as modeling_system_version,
129 b.name as model_group_map_name,
130 input(b.id, best32.) as model_group_map_sk,
131 c.mapentrykey as insttype length=32,
132 d.name as model_group_name,
133 input(d.id, best32.) as model_group_sk,
134 upcase(d.kindName) as model_group_kind length=32
135 from mgm_json.items_modelingsystem as a,
136 mgm_json.items as b,
137 mgm_json.items_mapentries as c,
138 mgm_json.mapentries_modelgroup as d
139 where b.ordinal_items eq a.ordinal_items and
140 b.ordinal_items eq c.ordinal_items and
141 c.ordinal_mapentries eq d.ordinal_mapentries and
142 upcase(d.kindName) eq upcase("&model_group_kind.") and
143 a.id in ("&ms_sk." %if %sysevalf(%superq(add_seg_lvl_ms_sk) ne, boolean) %then
144 "&add_seg_lvl_ms_sk.";)
145 order by modeling_system_sk,
146 model_group_map_sk,
147 model_group_sk;
148 quit;
149
150 %if %sysevalf(%superq(add_seg_lvl_ms_sk) ne, boolean) %then
151 %do;
152 proc sql noprint;
153 select distinct model_group_sk
154 into : seg_mg_sk1-
155 from work.model_group_map_tmp
156 where modeling_system_sk eq &add_seg_lvl_ms_sk.;
157 quit;
158
159 %let num_seg_mg = &sqlobs.;
160 %end;
161
162 libname mgm_json clear;
163 filename mgmjson clear;
164
165 /*--------------------------------------------------------------
166 * Get TM dimensions associated to Segment-level model groups
167 *--------------------------------------------------------------*/
168 proc sql;
169 create table work.model_group_x_tm_dim(
170 model_group_sk num,
171 tm_model_name varchar(32),
172 tm_model_sk num,
173 tm_dim num
174 );
175 quit;
176
177 %if (&num_seg_mg. ne 0) %then
178 %do i = 1 %to &num_seg_mg.;
179
180 filename mgjson temp;
181
182 %irm_rest_get_ticket(
183 url = &mipurl./rest/modelGroups/&&seg_mg_sk&i,
184 ticketType = st,
185 tgt_ticket = %superq(tgt_ticket),
186 outVarTicket = service_ticket,
187 %if %qupcase(&debug.) eq TRUE %then
188 debug = true;
189 %else
190 debug = false;
191 );
192
193 proc http url = "&mipurl./rest/modelGroups/&&seg_mg_sk&i?%str(&)ticket=&service_ticket."
194 out = mgjson
195 method = 'GET'
196 expect_100_continue;
197 %if %upcase(&debug.) eq TRUE %then
198 debug level = 2;;
199 headers "Accept" = "application/json"
200 "Content-Type" = "application/json";
201 run;
202
203 libname mg_json JSON fileref = mgjson;
204
205 /*-----------------------------------------------------------
206 * Get TM model information from model groups that have them
207 *-----------------------------------------------------------*/
208 %if %rsk_dsexist(mg_json.modelgroupmodels_model) %then
209 %do;
210 proc sql noprint;
211 select modelid,
212 id
213 into :tm_model_name,
214 :tm_model_sk
215 from mg_json.modelgroupmodels_model
216 where upcase(formType) eq "TRANSITIONMATRIX";
217 quit;
218 %end;
219
220 /*----------------------------------------------
221 * Get TM dimension based of number of toStates
222 *----------------------------------------------*/
223 proc sql;
224 insert into work.model_group_x_tm_dim
225 select &&seg_mg_sk&i as model_group_sk,
226 coalesce("&tm_model_name.",'') as tm_model_name,
227 coalesce(&tm_model_sk.,.) as tm_model_sk,
228 count(name) as tm_dim
229 from dictionary.columns
230 where libname eq 'MG_JSON' and
231 memname eq 'TRANSITIONMATRICES_TOSTATES' and
232 lowcase(name) like "tostate%";
233 quit;
234
235 filename mgjson clear;
236 libname mg_json clear;
237
238 %end;
239
240 /*------------------------------------------------------------------------
241 * Pass TM dim from Segment-level model group to loan-level model group
242 * ASSUMPTION : Segment-level insttype = loan-level insttype
243 *-----------------------------------------------------------------------*/
244 proc sql;
245 create table &out_model_group_map. as
246 select mgm.*,
247 tm.tm_model_name,
248 tm.tm_model_sk,
249 tm.tm_dim
250 from work.model_group_map_tmp as mgm
251 left join
252 (select distinct a.insttype,
253 b.tm_model_name,
254 b.tm_model_sk,
255 b.tm_dim
256 from work.model_group_map_tmp as a,
257 work.model_group_x_tm_dim as b
258 where a.model_group_sk eq b.model_group_sk) as tm
259 on upcase(mgm.insttype) eq upcase(tm.insttype)
260 order by modeling_system_sk,
261 model_group_map_sk,
262 model_group_sk;
263 quit;
264
265 /*---------------------------------------------
266 * Delete Macro Vars Generated by JSON Libname
267 *---------------------------------------------*/
268 %local vars_to_delete;
269 proc sql noprint;
270 select name
271 into : vars_to_delete
272 separated by ' '
273 from sashelp.vmacro
274 where scope eq 'GLOBAL' and
275 (upcase(name) like 'MGM_JSON_%' or
276 upcase(name) like 'MG_JSON_%');
277 quit;
278
279 %if &vars_to_delete. ne %then
280 %do;
281 %put NOTE: Deleting variable(s) &vars_to_delete.;
282 %symdel &vars_to_delete.;
283 %end;
284
285 /*----------------------------------------------------
286 * If debug not set to TRUE, drop intermediate tables
287 *----------------------------------------------------*/
288 %if %qupcase(&debug) ne TRUE %then
289 %do;
290 proc sql;
291 drop table work.model_group_map_tmp,
292 work.model_group_x_tm_dim;
293 quit;
294 %end;
295
296%mend irmst_get_model_group_maps;