SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irmst_get_required_mdl_vars.sas
1/*---------------------------------------------------------------------------------
2 * NAME: irmst_get_required_mdl_vars.sas
3 *
4 * PURPOSE: Script used to create control table to document required variables in
5 * portfolio data set for MIP execution
6 *
7 * NOTES:
8 *
9 * MACRO OPTIONS:
10 * model_group_sk - Specify space delimited list of model group sk(s)
11 * to extract variables from
12 * input_port - Specify input portfolio to match variables to
13 * output_table - OPTIONAL: name of the control table
14 * outpath - OPTIONAL: output directory of udl and resolved macros
15 * pathsep - OPTIONAL: path separator (default is unix)
16 * debug - OPTIONAL: keep intermediary work tables. default is NO
17 * tgt_ticket - Specify TGT used to generate ST for API calls
18 *
19 * MACRO PARAMETERS FOR WORKGROUP SWITCHING:
20 * host workgroup outVarTicket restartLUA
21 * server authMethod outSuccess clearCache
22 * port outds outResponseStatus
23 *
24 *--------------------------------------------------------------------------------- */
25%macro irmst_get_required_mdl_vars(
26 model_group_sk = ,
27 input_port = ,
28 output_table = work.portfolio_vars,
29 outpath = %sysfunc(pathname(work)),
30 pathsep = /,
31 debug = no,
32 mipurl = ,
33 tgt_ticket = ,
34 host = ,
35 server = ,
36 port = ,
37 workgroup = ,
38 authMethod = token,
39 outds = work._tmp_workgroups_,
40 outVarTicket = ,
41 outSuccess = 0,
42 outResponseStatus = ,
43 restartLUA = Y,
44 clearCache = Y
45 );
46
47 %local _debug_
48 _mg_sk
49 model_group_nm
50 num_models
51 logpath
52 initial_syscc;
53
54 /*-------------------------------------------
55 * Ensure correct workgroup before proc http
56 *-------------------------------------------*/
57 %irm_rest_set_mip_workgroup(host = &host.
58 , server = &server.
59 , port = &port.
60 , workgroup = &workgroup.
61 , authMethod = &authMethod.
62 , tgt_ticket = %superq(tgt_ticket)
63 , outds = &outds.
64 , outVarTicket = &outVarTicket.
65 , outSuccess = &outSuccess.
66 , outResponseStatus = &outResponseStatus.
67 , debug = &log_debug.
68 , restartLUA = &restartLUA.
69 , clearCache = &clearCache.
70 );
71
72 /*-----------------------
73 * Align debug parameter
74 *-----------------------*/
75 %if %qupcase(&debug.) eq FALSE %then
76 %let _debug_ = NO;
77 %else %if %qupcase(&debug.) eq TRUE %then
78 %let _debug_ = YES;
79
80 /*--------------
81 * Sanity check
82 *--------------*/
83 %if %quote(&model_group_sk) eq %then
84 %do;
85 %put ERROR: model_group_sk(s) not specified.;
86 %return;
87 %end;
88
89 /*--------------------------------------------
90 * Create output table to append results to
91 *--------------------------------------------*/
92 proc sql;
93 create table &output_table(
94 model_group_sk num,
95 model_group varchar(32),
96 variable varchar(32767)
97 );
98 quit;
99
100 /*--------------------------------------------
101 * Loop over model groups sk's specified
102 *--------------------------------------------*/
103 %do j = 1 %to %sysfunc(countw(&model_group_sk));
104 %let _mg_sk = %scan(&model_group_sk, &j);
105
106 %put NOTE: Processing model group &j of %sysfunc(countw(&model_group_sk)) (model_group_sk = &_mg_sk). >>>;
107 /*-------------------------------------------------------------
108 * Get model definitions from model group and model group name
109 *-------------------------------------------------------------*/
110 %irm_rest_get_ticket(
111 url = &mipurl/rest/modelGroups/&_mg_sk,
112 ticketType = st,
113 tgt_ticket = %superq(tgt_ticket),
114 outVarTicket = service_ticket,
115 %if %qupcase(&_debug_.) ne NO %then
116 debug = true;
117 %else
118 debug = no;
119 );
120
121 filename mgjson temp;
122
123 proc http url = "&mipurl/rest/modelGroups/&_mg_sk?%str(&)ticket=&service_ticket"
124 out = mgjson
125 method = 'GET'
126 expect_100_continue;
127 %if %qupcase(&_debug_.) ne NO %then
128 %do;
129 debug level = 2;
130 %end;
131 headers "Accept" = "application/json"
132 "Content-Type" = "application/json";
133 run;
134
135 libname mg_json JSON fileref = mgjson;
136
137 proc sql noprint;
138 select name into :model_group_nm trimmed
139 from mg_json.root
140 where id eq "&_mg_sk";
141 quit;
142
143 /*-------------------------------------------------------------
144 * Check for model-less model group
145 *-------------------------------------------------------------*/
146 %if %rsk_dsexist(mg_json.modelgroupmodels_model) %then
147 %do;
148 proc sql noprint;
149 select id into :model1-
150 from mg_json.modelgroupmodels_model;
151 quit;
152
153 %let num_models = &sqlobs;
154 %end;
155 %else
156 %do;
157 %let num_models = 0;
158 %end;
159
160 filename mgjson clear;
161 libname mg_json clear;
162
163 /*-------------------------------------------------------
164 * Parse model definitions and create list of variables
165 *-------------------------------------------------------*/
166 proc sql;
167 create table work.model_variable_metadata(
168 name varchar(32),
169 textExpression varchar(2048)
170 );
171 quit;
172
173 %do i = 1 %to &num_models;
174 %put NOTE: Extracting model &i of &num_models: &&model&i..;
175
176 %irm_rest_get_ticket(
177 url = &mipurl/rest/models/&&model&i,
178 ticketType = st,
179 tgt_ticket = %superq(tgt_ticket),
180 outVarTicket = service_ticket,
181 %if %qupcase(&_debug_.) ne NO %then
182 debug = true;
183 %else
184 debug = no;
185 );
186
187 filename mdljson temp;
188
189 proc http url = "&mipurl/rest/models/&&model&i?%str(&)ticket=&service_ticket"
190 out = mdljson
191 method = 'GET'
192 expect_100_continue;
193 %if %qupcase(&_debug_.) ne NO %then
194 %do;
195 debug level = 2;
196 %end;
197 headers "Accept" = "application/json"
198 "Content-Type" = "application/json";
199 run;
200
201 libname mdl_json JSON fileref = mdljson;
202
203 /*----------------------------------------------------
204 * Get model variable
205 * Models without variables (e.g. TM) are disregarded
206 *----------------------------------------------------*/
207 %if %rsk_varexist(mdl_json.variables, name) %then
208 %do;
209 proc sql;
210 create table work.model_&&model&i as
211 select name,
212 textExpression
213 from mdl_json.variables;
214 quit;
215
216 proc append base = work.model_variable_metadata
217 data = work.model_&&model&i
218 force
219 nowarn;
220 run;
221 %end;
222
223 filename mdljson clear;
224 libname mdl_json clear;
225 %end;
226
227 %irm_tslit_convert(
228 in_table = work.model_variable_metadata,
229 out_filter_variable_table = work.filter_variable_nm,
230 variable = name,
231 debug = no
232 );
233
234 %irm_tslit_convert(
235 in_table = work.model_variable_metadata,
236 out_filter_variable_table = work.filter_variable_expression,
237 variable = textExpression,
238 debug = no
239 );
240
241 /*------------------------------------------------
242 * Get UDL for the specified _mg_sk and
243 * model_group_nm global macro variable
244 *------------------------------------------------*/
245 %irmst_export_mip_udl(
246 chunk_size = 1000,
247 model_group_sk = &_mg_sk,
248 outpath = &outpath,
249 pathsep = &pathsep,
250 debug = &_debug_.,
251 mipurl = %superq(mipurl),
252 tgt_ticket = %superq(tgt_ticket)
253 );
254
255 filename udl "&outpath&pathsep&model_group_nm..sas";
256
257 /*------------------------------------------------
258 * Create file to store output from mprint option
259 * Delete file if it already exists
260 *------------------------------------------------*/
261 %let tempfile = &outpath&pathsep.mprint_&model_group_nm..sas;
262
263 %let rc = %sysfunc(filename(fref, &tempfile));
264 %if &rc = 0 and %sysfunc(fexist(&fref)) %then
265 %do;
266 %let rc = %sysfunc(fdelete(&fref));
267 %end;
268
269 /*-----------------------------------------------------------
270 * Use mfile and mprint to write resolved macros to a file.
271 * Since all code here is contained within a macro, all lines
272 * in udl that's not in a macro also gets sent to tempfile.
273 * This throws errors into the log which is rerouted with
274 * proc printo, but output is still written.
275 *-----------------------------------------------------------*/
276
277 filename reroute temp;
278
279 /*-----------------------------------------------------------
280 * Store original destination path for LOG before rerouting,
281 * null if no redirection of log initially
282 *-----------------------------------------------------------*/
283 %Let logpath = &sysprinttolog;
284
285 proc printto log=reroute;
286 run;
287
288 filename mprint "&tempfile";
289 options mfile mprint;
290 %let initial_syscc = &syscc;
291 %include udl / source2;
292 /*-------------
293 * Reset syscc
294 *-------------*/
295 %let syscc=&initial_syscc;
296 options nomfile;
297
298 /*-----------------------------------------------------------
299 * Reset to original destination path for LOG
300 *-----------------------------------------------------------*/
301 %if &logpath eq %then
302 %do;
303 proc printto;
304 run;
305 %end;
306 %else
307 %do;
308 proc printto log=&logpath;
309 run;
310 %end;
311
312 /*-------------
313 * Reset syscc
314 *-------------*/
315 %let syscc=&initial_syscc;
316
317 data work.udl_file2;
318 length udl_file $32767;
319 infile mprint;
320 input;
321 udl_file = _infile_;
322 run;
323
324 filename udl clear;
325 filename mprint clear;
326
327 /*------------------------------------------------
328 * Parse UDL and create list of variables
329 *------------------------------------------------*/
330 %irm_tslit_convert(
331 in_table = udl_file2,
332 out_filter_variable_table = work.filter_udl,
333 variable = udl_file,
334 debug = no
335 );
336
337 /*----------------------------------------------------------------
338 * Concatenate variables that appear in UDL and model definitions
339 *----------------------------------------------------------------*/
340 data work.filter_variables;
341 set work.filter_udl
342 work.filter_variable_nm
343 work.filter_variable_expression;
344 run;
345
346 /*--------------------------------------------
347 * Sort variables and drop duplicates if any
348 *--------------------------------------------*/
349 proc sort data=work.filter_variables
350 nodupkey;
351 by filter_variable;
352 run;
353
354 /*------------------------------------------------------------------------
355 * Match with variables found in input portfolio and create control table
356 *------------------------------------------------------------------------*/
357 proc sql;
358 create table work._out_&_mg_sk as
359 select &_mg_sk as model_group_sk,
360 "&model_group_nm" as model_group length=32,
361 filter_variable as variable
362 from work.filter_variables
363 where variable in
364 (select upcase(name)
365 from dictionary.columns
366 where libname="%qupcase(%scan(&input_port,1,.))" and
367 memname="%qupcase(%scan(&input_port,2,.))");
368 quit;
369
370 proc append base = &output_table
371 data = work._out_&_mg_sk;
372 run;
373
374 /*----------------------------------------------------
375 * Drop intermediate tables if debugging not required
376 *----------------------------------------------------*/
377 %if %qupcase(&_debug_.) eq NO %then
378 %do;
379 proc sql;
380 drop table work.udl_file2,
381 work.model_variable_metadata,
382 %do i = 1 %to &num_models;
383 work.model_&&model&i,
384 %end;
385 %do i = 1 %to %sysfunc(countw(&model_group_sk));
386 work._out_&_mg_sk,
387 %end;
388 work.filter_variables,
389 work.filter_udl,
390 work.filter_variable_nm,
391 work.filter_variable_expression;
392 quit;
393 %end;
394 %end;
395
396 /*---------------------------------------------------
397 * Delete Macro Vars Generated by JSON Libname
398 *---------------------------------------------------*/
399 %local vars_to_delete;
400 proc sql noprint;
401 select name
402 into : vars_to_delete
403 separated by ' '
404 from sashelp.vmacro
405 where scope eq 'GLOBAL' and
406 (upcase(name) like 'MG_JSON_%' or
407 upcase(name) like 'MDL_JSON_%');
408 quit;
409
410 %if vars_to_delete ne %then
411 %do;
412 %put NOTE: Deleting variable(s) &vars_to_delete.;
413 %symdel &vars_to_delete.;
414 %end;
415
416%mend irmst_get_required_mdl_vars;