SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irmst_script_ccar_measures.sas
1/*----------------------------------------------------------------------------
2 * NAME: irmst_script_ccar_measures.sas
3 *
4 * PURPOSE: Execution configuration code for loan-level run
5 *
6 *----------------------------------------------------------------------------*/
7/* ********************************************* */
8/* Credit Risk Analysis: Input Parameters */
9/* ********************************************* */
10
11/* TGT Authentication Ticket */
12%let rgf_tgt_ticket = ${globals.ticket};
13
14/* Get the root location of the SAS Risk Workgroup Application */
15%let sas_risk_workgroup_dir = ${globals.sas_risk_workgroup_dir};
16
17/* Cycle Id */
18%let cycle_id = ${context.cycle.key};
19
20/* Analysis Run Id */
21%let analysis_run_id = ${context.analysisRun.key};
22
23/* IRM Configuration Set Id */
24%let config_set_id = ${params.CONFIGSETID};
25
26/* Portfolio key */
27%let portfolio_key = ${params.PORTFOLIO.key};
28
29/* Credit model - run key */
30%let model_run_key = ${params.CREDITMODEL.rels("context").modelRun.key};
31
32/* Credit model coutnerparty key */
33%let counterparty_key = ${params.CREDITMODEL.params.COUNTERPARTY.key};
34
35/* Mitigant key */
36%let mitigant_key = ${params.CREDITMODEL.params.MITIGATION.key};
37
38/* Collateral key */
39%let collateral_key = ${params.CREDITMODEL.params.COLLATERAL.key};
40
41/*Economic Simulation key*/
42%let econsimulation_key = ${params.CREDITMODEL.params.ECONSIMULATION.key};
43
44/*TODO: No IADATA parameter */
45/* IA data key */
46%let ia_data_key = ${params.IADATA.key};
47
48/* Model Input DataMap key */
49%let modelIn_dataMap_key = ${params.MODELINPUTMAP.key};
50
51/* Model Output DataMap key */
52%let modelOut_dataMap_key = ${params.MODELOUTPUTMAP.key};
53
54/* TM Results Analysis Data Key */
55%let tm_results_key = ${params.TRANSITIONMATRIXRESULTS.key};
56
57/* Query results from all horizons? */
58%let query_all_hrz_flg = ${params.QUERYALLHRZ};
59
60/* ATM Overlay */
61%let atm_overlay = ${params.ATMOVERLAY};
62%if &atm_overlay eq Y %then %do;
63 /* Process Scenarios selected for ATM Overlay */
64 %let scenario_prompt = ${params.SCENARIOTOOVERLAY};
65 /* Results are returned like [BEP_Name1 - MRS Name1, BEP_Name2 - MRS Name2]. Get rid of square backets */
66 %let scenario_to_overlay = %sysfunc(prxchange(s/[\[\]]//i, -1, %superq(scenario_prompt)));
67
68 /* Process ATM Spreadsheet Upload - outputs to work.atmupload */
69 ${function:ProcessSpreadsheetParameter(params.ATMUPLOAD,"ATMUPLOAD")}
70%end;
71
72/* Keep MIP Cube? */
73%if &query_all_hrz_flg. = Y %then %do;
74 %let keep_mip_cube_flg = Y;
75%end;
76%else %do;
77 %let keep_mip_cube_flg = ${params.KEEPMIPCUBE};
78%end;
79
80/* LASR load method (Append/Replace) */
81%let lasr_load_method = ${params.LASRLOADMETHOD};
82
83/* TODO: There is no RELOADKEYLIST parameter */
84/* List of analysis data objecs to reload (if lasr_load_method = replace) */
85%let reload_key_list = ${params.RELOADKEYLIST};
86/* Convert to space separated list: ["<key1>","<key2>",...,"<keyN>"] --> <key1> <key2> ... <keyN> */
87%let reload_key_list = %sysfunc(prxchange(s/\W+/ /i, -1, %superq(reload_key_list)));
88
89/* Log Level: 1-4 */
90%let log_level = ${params.LOGLEVEL};
91
92/* Maximum wait time for IRM job completion: <number of seconds> */
93%let max_wait = ${params.MAXWAIT};
94
95/* ********************************************* */
96
97/* Initialize the environment */
98%include "&sas_risk_workgroup_dir./groups/Public/SASRiskManagementCore/cycles/&cycle_id./init.sas" / lrecl = 32000 source2;
99
100/* Set logging options (based on the value of LOG_LEVEL macro variable) */
101%irm_set_logging_options();
102
103/* Process LASR load method */
104%let reload_lasr_flg = %irmc_process_lasr_load_method(&lasr_load_method.);
105
106/*-----------------------------
107 * Retrieve BEP / MRS details
108 *-----------------------------*/
109/* Find the anlayis run that created the transition matrix */
110%irm_rest_get_rgf_analysis_run(host = &rgf_protocol.://&rgf_host.
111 , server = &rgf_service.
112 , solution = &rgf_solution.
113 , port = &rgf_port.
114 , tgt_ticket = %superq(rgf_tgt_ticket)
115 , filter = filter=hasObjectLinkTo(%str(%')%upcase(&rgf_solution.)%str(%'),'analysisData_analysisRun_out',&tm_results_key.,0)
116 , outds = analysis_run
117 , outds_params = analysis_run_params
118 , outVarTicket = ticket
119 , outSuccess = httpSuccess
120 , outResponseStatus = responseStatus
121 );
122
123/* From that analysis run, get the BEPs and MRSs that were used */
124%let BEP = ;
125data _null_;
126 set analysis_run_params;
127 where upcase(parameterName) eq "BEP";
128 call symputX('BEP',parameterValue);
129run;
130
131/* Parse the json string and create macro variables */
132proc lua restart;
133submit;
134 local json = require 'sas.risk.utils.json'
135 local BEPstr = '{"rows":'..sas.symget('BEP')..'}'
136 local BEPtbl = json:decode(BEPstr)
137 local bep_cnt = #BEPtbl.rows
138 sas.symput('BEP_CNT',bep_cnt)
139 for i,bep in ipairs(BEPtbl.rows) do
140 local bepKey = bep.handle:gsub(".-|(%d+)|.*","%1")
141 sas.symput('BEP_'..i,bepKey);
142 local bep_mrs_cnt = #bep.dependencies;
143 sas.symput('BEP_'..i..'_MRS_CNT',bep_mrs_cnt)
144 for j,scen in ipairs(bep.dependencies) do
145 local scenKey = scen.handle:gsub(".-|(%d+)|.*","%1")
146 sas.symput('BEP_'..i..'_MRS_'..j,scenKey)
147 end
148 end
149endsubmit;
150run;
151
152/* This is the standard code used when the BEP comes in directly as a model parameter */
153data bep_x_mrs_config;
154 length BEP_KEY MRS_KEY 8.;
155 keep BEP_KEY MRS_KEY;
156 bep_cnt = symgetn("BEP_CNT");
157 do n = 1 to bep_cnt;
158 bep_key = symgetn(cats("BEP_", n));
159 mrs_cnt = symgetn(cats("BEP_", n, "_MRS_CNT"));
160 do i = 1 to mrs_cnt;
161 mrs_key = symgetn(cats("BEP_", n, "_MRS_", i));
162 output;
163 end;
164 if mrs_cnt <= 0 then
165 put "WARNING: No Master Risk Scenarios have been assigned to Business Evolution Plan Id " bep_key;
166 end;
167run;
168
169/*--------------------------------------------------------------------------
170 * Fetch the key of segment-level analysis run and the associated portfolio
171 *--------------------------------------------------------------------------*/
172%let segment_lvl_analysis_run_key=;
173data _null_;
174 set work.analysis_run;
175 call symputx('segment_lvl_analysis_run_key',objectId);
176run;
177
178%let portParmStr=;
179%let data_prep_out_portfolio_key=;
180
181data _null_;
182 set analysis_run_params;
183 if parameterName eq 'PORTFOLIO' then
184 call symputX('portParmStr',parameterValue);
185run;
186
187proc lua restart;
188submit;
189 local JSON = require 'risk.common.json'
190 local portParmTbl = JSON:decode(sas.symget('portParmStr'))
191 local portKey = portParmTbl.handle:gsub(".-|(%d+)|.*","%1")
192 sas.symput('data_prep_out_portfolio_key',portKey)
193endsubmit;
194run;
195
196/* Set Instance Parameters */
197%let jobflow_category = Analytics;
198%let jobflow_type = %lowcase(irm&solutionId._credit_risk);
199%let instance_date = %sysfunc(date(), yymmddp10.);
200%let instance_time = %sysfunc(prxchange(s/(\d+):(\d+):(\d+)/$1h$2m$3s/i, -1, %sysfunc(time(), tod8.)));
201%let instanceName = Credit Risk Analysis &instance_date._&instance_time.;
202%let instanceDesc = Credit Risk Analysis;
203
204/*---------------------------------------------------------------
205 * Process ATM Spreadsheet Upload
206 *---------------------------------------------------------------*/
207%if &atm_overlay eq Y %then %do;
208
209 /* Data def info for TM matrix overlay */
210 %let ad_name_tm_overlay = Transition Matrix Results with ATM Overlay;
211 %let ad_desc_tm_overlay = Transition Matrix Results with ATM Overlay from Analysis Run &analysis_run_id.. Created by &_metauser on %sysfunc(datetime(), nldatmw200.);
212
213 /* Get existing TM from Segment Level Run */
214 %irm_rgf_retrieve_analysis_data(key = &tm_results_key.
215 , outds = tm_orig
216 , out_type = data
217 , outds_dataInfo = tm_dataInfo
218 , outds_dataDef = tm_dataDef
219 , host = &rgf_protocol.://&rgf_host.
220 , server = &rgf_service.
221 , solution = &rgf_solution.
222 , port = &rgf_port.
223 , tgt_ticket = %superq(rgf_tgt_ticket)
224 , outVarTicket = ticket_upload
225 , outSuccess = httpSuccess
226 , outResponseStatus = responseStatus
227 , restartLUA = Y
228 , clearCache = Y
229 );
230
231 /* Get data def info for TM matrix to use in ATM upload */
232 data _null_;
233 set tm_dataDef;
234 call symputx("schemaName", schemaName, "L");
235 call symputx("schemaVersion", schemaVersion, "L");
236 run;
237
238 proc sql;
239 select distinct
240 model_group_name
241 into :tm_model_groups separated by ' '
242 from tm_orig
243 ;
244 quit;
245
246 /* Invoke macro to import the atm spreadsheet */
247 %irmst_import_atm_spreadsheet(atmupload = work.atmupload
248 , outds = work.atm_outds
249 , in_model_groups = &tm_model_groups.
250 );
251
252 /*---------------------------
253 * Process ATM overlay logic
254 *---------------------------*/
255 %irmst_run_atm_overlay(in_model_tm_results = tm_orig
256 , in_atm_upload = work.atm_outds
257 , scenario_to_overlay = %superq(scenario_to_overlay)
258 /* Analysis Data Parameters */
259 , analysis_data_name = &ad_name_tm_overlay.
260 , analysis_data_desc = %quote(&ad_name_tm_overlay.)
261 , analysis_run_id = &analysis_run_id
262 , base_date = &base_dt
263 , cycle_id = &cycle_id
264 , status_cd = Preliminary
265 , visibility_cd = Private /* or Shared */
266 /* Data Definition Parameters (note: Case-sensitive for certain parameters) */
267 , libref = &dr_libref
268 , schema_name = &schemaName.
269 , schema_version = &schemaVersion.
270 , entity_id = &entity_id.
271 /* Output tables */
272 , out_atm_results_key_var = atm_results_key
273 , out_exceptions = work._atm_upload_exceptions
274 , out_analysis_data = work._atm_upload_analysis_data
275 , out_partition_list = work._atm_upload_partition_list
276 , out_data_definition = work._atm_upload_data_definition
277 , out_link_instance = work._atm_upload_link_instance
278 /* Connection Parameters */
279 , host = &rgf_protocol.://&rgf_host.
280 , server = &rgf_service.
281 , solution = &rgf_solution.
282 , port = &rgf_port.
283 , tgt_ticket = %superq(rgf_tgt_ticket)
284 , debug = &log_debug
285 )
286%end;
287
288%if %symexist(atm_results_key) and
289 %superq(atm_results_key) ne %then
290%do;
291 %let tm_results_key = &atm_results_key;
292%end;
293
294/* Run option table */
295data work.run_option;
296 length
297 config_name $32.
298 config_value $10000.
299 ;
300 config_name = "RMC_FA_ID"; config_value = "&rmc_fa_id."; output;
301 config_name = "CYCLE_ID"; config_value = "&cycle_id."; output;
302 config_name = "ANALYSIS_RUN_ID"; config_value = "&analysis_run_id."; output;
303 config_name = "ANALYSIS_TYPE"; config_value = "CCAR_SCR"; output;
304 config_name = "MAX_WAIT"; config_value = "&max_wait."; output;
305 config_name = "LOG_LEVEL"; config_value = "&log_level."; output;
306 config_name = "RELOAD_LASR_FLG"; config_value = "&reload_lasr_flg."; output;
307 config_name = "RELOAD_KEY_LIST"; config_value = "&reload_key_list."; output;
308 config_name = "TGT_TICKET"; config_value = "&rgf_tgt_ticket."; output;
309 config_name = "SEGMENT_LVL_ANALYSIS_RUN_KEY"; config_value = "&segment_lvl_analysis_run_key."; output;
310run;
311
312/* Credit Stress parameters table */
313data work.credit_risk_config;
314 length
315 config_name $32.
316 config_value $100.
317 ;
318 config_name = "TM_RESULTS_KEY"; config_value = "&tm_results_key."; output;
319 config_name = "QUERY_ALL_HRZ_FLG"; config_value = "&query_all_hrz_flg."; output;
320 config_name = "DATA_PREP_OUT_PORTFOLIO_KEY"; config_value = "&data_prep_out_portfolio_key"; output;
321 /* TODO: For now always assume Y, but how to allow for N? */
322 %let dma_expansion_flg = Y;
323 config_name = "DMA_EXPANSION_FLG"; config_value = "&dma_expansion_flg"; output;
324
325 config_name = "CREDIT_MODEL_RUN_KEY"; config_value = "&model_run_key."; output;
326 config_name = "PORTFOLIO_KEY"; config_value = "&portfolio_key."; output;
327 config_name = "COUNTERPARTY_KEY"; config_value = "&counterparty_key."; output;
328 config_name = "MITIGANT_KEY"; config_value = "&mitigant_key."; output;
329 config_name = "COLLATERAL_KEY"; config_value = "&collateral_key."; output;
330 config_name = "ECONSIMULATION_KEY"; config_value = "&econsimulation_key."; output;
331 config_name = "IA_DATA"; config_value = "&ia_data_key."; output;
332 config_name = "MODELIN_DATAMAP_KEY"; config_value = "&modelIn_dataMap_key."; output;
333 config_name = "MODELOUT_DATAMAP_KEY"; config_value = "&modelOut_dataMap_key."; output;
334 config_name = "KEEP_MIP_CUBE_FLG"; config_value = "&keep_mip_cube_flg."; output;
335run;
336
337%let ticket = ;
338%let httpSuccess = 0;
339%let responseStatus = ;
340
341%let instanceKey =;
342%let jobflow_status = Unknown;
343/* Create job flow instance and wait for completion */
344%irm_rest_create_jobflow_instance(instance_name = &instanceName.
345 , description = &instanceDesc.
346 , entityId = &entity_id.
347 , entityRoleKey = &entity_role_key.
348 , category = &jobflow_category.
349 , jobflowFile = &jobflow_type.
350 , baseDate = &irm_base_dt.
351 , configSetId = &config_set_id.
352 , federatedAreaID = &irm_fa_id.
353 , sourceFederatedAreaId = &irm_fa_id.
354 , src_param_tables = work.run_option work.credit_risk_config
355 , tgt_param_tables = &solutionLibrefPrefix._CFG.RUN_OPTION &solutionLibrefPrefix._CFG.CREDIT_RISK_CONFIG
356 , src_upload_tables = work.bep_x_mrs_config
357 , tgt_upload_tables = &solutionLibrefPrefix._CFG.BEP_X_MRS_CONFIG
358 , autoRun = true
359 , wait_flg = Y
360 , pollInterval = 3
361 , maxWait = &max_wait.
362 , host = &irm_protocol.://&irm_host.
363 , port = &irm_port.
364 , tgt_ticket = &rgf_tgt_ticket.
365 , outvar = instanceKey
366 , outVarStatus = jobflow_status
367 );
368
369%if(&jobflow_status. = Successful) %then %do;
370 /* Refresh the Star Schema Views */
371 %irmc_refresh_lasr_schema_view(lasr_lib = &lasr_libref.
372 , mart_table_name = &solutionId._CREDIT_RISK_DETAIL
373 , rls_table_name = &solutionId._CREDIT_RISK_DETAIL_RLS
374 , meta_repository = &meta_repository.
375 , lasr_library_name = &lasr_library_name.
376 , lasr_meta_folder = &lasr_meta_folder.
377 );
378
379 %irmc_refresh_lasr_schema_view(lasr_lib = &lasr_libref.
380 , mart_table_name = &solutionId._MIGRATION_DETAIL
381 , rls_table_name = &solutionId._MIGRATION_DETAIL_RLS
382 , meta_repository = &meta_repository.
383 , lasr_library_name = &lasr_library_name.
384 , lasr_meta_folder = &lasr_meta_folder.
385 );
386%end;
387
388/* Update the Cycle control table */
389%irmc_update_ctrl_table(cycle_id = &cycle_id.
390 , analysis_run_id = &analysis_run_id.
391 , dr_libref = &dr_libref.
392 , entry_type = IRM
393 , entry_id = &instanceKey.
394 , entry_name = &instanceName.
395 );