SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irmst_build_tm_from_cube.sas
1/*------------------------------------------------------------------------
2 * NAME: irmst_build_tm_from_cube.sas
3 *
4 * PURPOSE: Builds a data set containing transition matrices for each
5 * scenario and horizon from a HPRisk Cube
6 *
7 * NOTES:
8 *
9 * MACRO OPTIONS:
10 * - MI_CUBE_NAME - Name of the generated risk cube
11 * - WORKGROUP - Name of workgroup with the generated risk cube
12 * - RUN_OUTPUT_ARRAY - OPTIONAL: specify the name of the array output
13 * storing transition probabilities
14 * DEFAULT: transmat_part
15 * - FUNCTION_PACKAGE - OPTIONAL: Specify 2-level data set name that
16 * stores function to format rating grade
17 * DEFAULT: mipfunc.rating_format
18 * - SCENARIO_META - Input table name of scenario metadata
19 * - OUTPUT_TBL - OPTIONAL: Two-level name for output table
20 * DEFAULT: work.tm_set
21 * - DEBUG - OPTIONAL: For debugging API calls and keeping
22 * intermediate tables
23 * DEFAULT: NO
24 * - MIPURL - MIP URL to make rest API calls
25 * - TGT_TICKET - Specify generated TGT
26 *
27 *------------------------------------------------------------------------*/
28%macro irmst_build_tm_from_cube(
29 mi_cube_name = ,
30 workgroup = ,
31 run_output_array = transmat_part,
32 function_package = mipfunc.rating_format,
33 scenario_meta = ,
34 output_tbl = work.tm_set,
35 debug = NO,
36 mipurl = ,
37 tgt_ticket =
38 );
39
40 %local ori_cmplib
41 rwg_appdata_root_dir
42 colname
43 tmdim;
44
45 /* ---------------------------
46 * Get original cmplib option
47 * --------------------------- */
48 data _null_;
49 length cmplib $32767.;
50 cmplib = getoption('cmplib');
51 cmplib = translate(cmplib,'','(','',')');
52 call symputx('ori_cmplib',cmplib,'l');
53 run;
54
55 /* --------------------------------------
56 * Add format function package to cmplib
57 * -------------------------------------- */
58 %irm_add_function_package(
59 cmplib = &function_package.,
60 check_function = rating_num_to_grade
61 );
62
63 /* ------------------------------------------------------------
64 * Get the root location of the SAS Risk Workgroup Application
65 * ------------------------------------------------------------*/
66 %let rwg_appdata_root_dir = %sysfunc(metadata_appprop(Risk Work Group Svr Cfg, root.dir));
67
68 /* --------------------------
69 * Query HPRisk Cube Results
70 * -------------------------- */
71 proc hprisk task=stress
72 cube = "&rwg_appdata_root_dir./groups/&workgroup./SASModelImplementationPlatform/output/run_instances/&mi_cube_name./results/&mi_cube_name.";
73 crossclassvars insttype;
74 output scentrans = work.&mi_cube_name. (drop = NInst
75 NMissing
76 Value);
77 query insttype;
78 run;
79 quit;
80
81 /* ------------------------------------
82 * Get tm dim based on length of array
83 * ------------------------------------ */
84 data _null_;
85 set work.&mi_cube_name.;
86 array arraylen{*} &run_output_array:;
87 tmdim = sqrt(dim(arraylen));
88 call symputx('tmdim',tmdim,'l');
89 run;
90
91 /* ------------------------------------
92 * Get names and SKs for run instances
93 * ------------------------------------ */
94 %irm_rest_get_ticket(
95 url = &mipurl./rest/runInstances,
96 ticketType = st,
97 tgt_ticket = %superq(tgt_ticket),
98 outVarTicket = service_ticket,
99 %if %qupcase(&debug.) ne NO %then
100 debug = true;
101 %else
102 debug = no;
103 );
104
105 filename rijson temp;
106
107 proc http url = "&mipurl./rest/runInstances?%str(&)ticket=&service_ticket."
108 out = rijson
109 method = 'GET'
110 expect_100_continue;
111 %if &debug. ne NO %then
112 %do;
113 debug level = 2;
114 %end;
115 headers "Accept" = "application/json"
116 "Content-Type" = "application/json";
117 run;
118
119 libname ri_json JSON fileref = rijson;
120
121 proc sql;
122 create table work.run_instance_meta as
123 select input(id,8.) as run_instance_sk,
124 name as run_nm
125 from ri_json.items;
126 quit;
127
128 filename rijson clear;
129 libname ri_json clear;
130
131 /* ---------------------------
132 * Convert array output to TM
133 * --------------------------- */
134 data &output_tbl (drop = scenario
135 rc_:
136 &run_output_array:
137 scenarioId);
138 if 0 then
139 set work.run_instance_meta;
140 set work.&mi_cube_name.;
141
142 length mrskey 8
143 scenarioId $32
144 from_rating $10;
145
146 call missing(mrskey);
147
148 /* --------------------------------------------
149 * Set up hash table to lookup run_instance_sk
150 * -------------------------------------------- */
151 if _n_ eq 1 then
152 do;
153 %irm_build_hash_lookup(
154 hash_name = run_instance,
155 hash_table = work.run_instance_meta,
156 key_vars = run_nm,
157 data_vars = run_instance_sk
158 );
159
160 %irm_build_hash_lookup(
161 hash_name = scenario_map,
162 hash_table = &scenario_meta.,
163 key_vars = scenarioId,
164 data_vars = mrskey
165 );
166 end;
167
168 if upcase(scan(Scenario,-1,":")) ne "BASECASE" then
169 do;
170 /* -----------------------
171 * Lookup run_instance_sk
172 * ----------------------- */
173 run_nm = scan(Scenario,1,":");
174 rc_run_instance = run_instance.find();
175 if rc_run_instance ne 0 then
176 do;
177 put 'ERROR: Could not obtain run_instance_sk associated to run result.';
178 end;
179 /* -------------------
180 * Lookup MRS key
181 * ------------------- */
182 scenarioId = substr(scenario,index(scenario,':')+1);
183 rc_scenario_map = scenario_map.find();
184 if rc_scenario_map ne 0 then
185 do;
186 put "ERROR: Could not obtain MRS key in the scenario metadata &scenario_meta..";
187 end;
188
189 /* ---------------------------------------
190 * Apply rating formats and create TM set
191 * --------------------------------------- */
192 %do i =1 %to &tmdim.;
193 from_rating = rating_num_to_grade(&i.);
194 %do j =1 %to &tmdim.;
195 %let colname = %qcmpres(%sysfunc(rating_num_to_grade(&j.)));
196 &colname. = &run_output_array._%eval((&i.-1)*&tmdim. + &j.);
197 %end;
198 output;
199 %end;
200 end;
201 run;
202
203 /* --------------------
204 * Reset cmplib option
205 * -------------------- */
206 options cmplib=(&ori_cmplib.);
207
208 /* -------------------------
209 * Drop intermediate tables
210 * ------------------------- */
211 %if %qupcase(&debug.) eq NO %then
212 %do;
213 proc sql;
214 drop table work.&mi_cube_name.,
215 work.run_instance_meta;
216 quit;
217 %end;
218
219 /*---------------------------------------------
220 * Delete Macro Vars Generated by JSON Libname
221 *---------------------------------------------*/
222 %local vars_to_delete;
223 proc sql noprint;
224 select name
225 into : vars_to_delete
226 separated by ' '
227 from sashelp.vmacro
228 where scope eq 'GLOBAL' and
229 (upcase(name) like 'RI_JSON%');
230 quit;
231
232 %if vars_to_delete ne %then
233 %do;
234 %put NOTE: Deleting variable(s) &vars_to_delete.;
235 %symdel &vars_to_delete.;
236 %end;
237
238%mend irmst_build_tm_from_cube;