SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irmst_create_rating_formats.sas
1/*---------------------------------------------------------------------------------
2 * NAME: irmst_create_rating_formats.sas
3 *
4 * PURPOSE: Create a function package which contains functions that map rating
5 * grades to rating numbers and vice versa
6 *
7 * NOTES:
8 *
9 * MACRO OPTIONS:
10 * tm_model_sk - Specify space-delimited list of TM model SK
11 * function_lib - Specify libname to store function package
12 * package_name - Specify name for function package
13 * debug - For debugging API calls and keeping intermediate tables
14 * mipurl - MIP URL to make rest API calls
15 * tgt_ticket - Specify TGT used to generate service tickets
16 *
17 * MACRO PARAMETERS FOR WORKGROUP SWITCHING:
18 * host workgroup outVarTicket restartLUA
19 * server authMethod outSuccess clearCache
20 * port outds outResponseStatus
21 *
22 *--------------------------------------------------------------------------------- */
23%macro irmst_create_rating_formats(
24 tm_model_sk = ,
25 function_lib = ,
26 package_name = ,
27 debug = NO,
28 mipurl = ,
29 tgt_ticket = ,
30 host = ,
31 server = ,
32 port = ,
33 workgroup = ,
34 authMethod = token,
35 outds = work._tmp_workgroups_,
36 outVarTicket = ,
37 outSuccess = 0,
38 outResponseStatus = ,
39 restartLUA = Y,
40 clearCache = Y
41 );
42
43 %local rating_length;
44
45 /*-------------------------------------------
46 * Ensure correct workgroup before proc http
47 *-------------------------------------------*/
48 %irm_rest_set_mip_workgroup(host = &host,
49 server = &server,
50 port = &port,
51 workgroup = &workgroup,
52 authMethod = &authMethod,
53 tgt_ticket = %superq(tgt_ticket),
54 outds = &outds,
55 outVarTicket = &outVarTicket,
56 outSuccess = &outSuccess,
57 outResponseStatus = &outResponseStatus,
58 debug = &debug,
59 restartLUA = &restartLUA,
60 clearCache = &clearCache
61 )
62
63 /*-----------------------------------------------------------------
64 * Get order of rating grades from models and append them together
65 *-----------------------------------------------------------------*/
66 %do i = 1 %to %sysfunc(countw(&tm_model_sk));
67 %if &i = 1 %then %do;
68 proc sql;
69 create table work.rating_mapping_appended(
70 model_id num,
71 rating_grade varchar(32),
72 order_idx num
73 );
74 quit;
75 %end;
76
77 %let model_id&i = %scan(&tm_model_sk, &i);
78
79 %irm_rest_get_ticket(
80 url = &mipurl/rest/models/&&model_id&i,
81 ticketType = st,
82 tgt_ticket = %superq(tgt_ticket),
83 outVarTicket = service_ticket,
84 debug = true
85 );
86
87 filename mdl temp;
88
89 proc http url = "&mipurl/rest/models/&&model_id&i?%str(&)ticket=&service_ticket"
90 out = mdl
91 method = 'GET'
92 expect_100_continue;
93 debug level = 2;
94 headers "Accept" = "application/json"
95 "Content-Type" = "application/json";
96 run;
97
98 libname mdl_lib JSON fileref = mdl;
99
100 proc transpose data = mdl_lib.transitionmatrix_tostates(drop = ordinal_toStates
101 ordinal_transitionMatrix)
102 out = work.ratings_model_&&model_id&i;
103 var _all_;
104 run;
105 /*---------------------------------------------------------------------
106 * Compressing aphabetic characters from _name_ to obtain rating order
107 * e.g. order_idx 1 is obtained from _name_ = toStates1
108 *---------------------------------------------------------------------*/
109 data work.ratings_model_&&model_id&i.._2(drop = _name_
110 col1);
111 set work.ratings_model_&&model_id&i;
112 length model_id 8
113 rating_grade $32
114 order_idx 8;
115 model_id = &&model_id&i;
116 rating_grade = upcase(col1);
117 order_idx = input(compress(_name_, ' ', 'A'),8.);
118 run;
119
120 proc append base = work.rating_mapping_appended
121 data = work.ratings_model_&&model_id&i.._2;
122 run;
123 %end;
124
125 proc sort data = work.rating_mapping_appended;
126 by model_id
127 order_idx;
128 quit;
129
130 /*------------------------------------------------------------------
131 * Obtain (1-to-many) mapping between rating_grade and rating_num.
132 * If more than 1 model is obtained, rating_grade can be duplicated
133 *------------------------------------------------------------------*/
134 data work.rating_mapping_tmp(keep = rating_grade
135 rating_num);
136 set work.rating_mapping_appended;
137 by model_id
138 order_idx;
139 rating_num=_n_;
140 run;
141
142 /*--------------------------------------------------------------------
143 * Drop duplicate rating grades from different models to obtain
144 * 1-to-1 mapping from rating grades to rating nums. Order may not be
145 * preserved if >1 tm model is obtained. Different models may have
146 * different rating grades for same order index
147 *--------------------------------------------------------------------*/
148 proc sort data = work.rating_mapping_tmp
149 out = work.rating_mapping
150 nodupkey;
151 by rating_grade;
152 quit;
153
154 /*----------------------------------------
155 * Obtain maximum length of rating grades
156 *----------------------------------------*/
157 proc sql noprint;
158 select max(length(rating_grade))
159 into :rating_length trimmed
160 from work.rating_mapping;
161 quit;
162
163 /*--------------------------------------------
164 * Mapping from rating number to rating grade
165 *--------------------------------------------*/
166 filename numtogrd temp;
167
168 data _null_;
169 file numtogrd lrecl = 32767;
170 set work.rating_mapping;
171 put 'when(' rating_num +(-1) ')';
172 put 3*' ' "return('" rating_grade +(-1) "');";
173 run;
174
175 /*--------------------------------------------
176 * Mapping from rating grade to rating number
177 *--------------------------------------------*/
178 filename grdtonum temp;
179
180 data _null_;
181 file grdtonum lrecl = 32767;
182 set work.rating_mapping;
183 put "when('" rating_grade +(-1) "')";
184 put 3*' ' 'return(' rating_num +(-1) ');';
185 run;
186
187 /*----------------------------------------
188 * Compile functions to library specified
189 *----------------------------------------*/
190 proc compile outlib = &function_lib..&package_name
191 label = "Rating Formats"
192 package = &package_name;
193 /*------------------------------
194 * rating_num_to_grade function
195 *------------------------------*/
196 function rating_num_to_grade(rating_num) $ &rating_length
197 kind="rating format";
198 select(rating_num);
199 %include numtogrd;
200 otherwise;
201 end;
202 endsub;
203 /*------------------------------
204 * rating_grade_to_num function
205 *------------------------------*/
206 function rating_grade_to_num(rating_grade $)
207 kind="rating format";
208 select(rating_grade);
209 %include grdtonum;
210 otherwise;
211 end;
212 endsub;
213 quit;
214
215 filename numtogrd clear;
216 filename grdtonum clear;
217
218 /*-----------------
219 * Dropping tables
220 *-----------------*/
221 %if %upcase(&debug) eq NO %then
222 %do;
223 proc sql;
224 drop table work.rating_mapping_appended,
225 %do i = 1 %to %sysfunc(countw(&tm_model_sk));
226 work.ratings_model_&&model_id&i,
227 work.ratings_model_&&model_id&i.._2,
228 %end;
229 work.rating_mapping_tmp,
230 work.rating_mapping;
231 quit;
232 %end;
233
234%mend irmst_create_rating_formats;