SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irmst_export_mip_udl.sas
1/*----------------------------------------------------------------------
2 * NAME: irmst_export_mip_udl.sas
3 *
4 * PURPOSE: Exports MIP UDL from the Postgres database to a location on the server
5 *
6 * NOTES:
7 *
8 * MACRO OPTIONS:
9 * CHUNK_SIZE - amount of code to extract from a query
10 * MODEL_GROUP_SK - OPTIONAL: specify space delimited model group sk(s)
11 * OUTPATH - OPTIONAL: output directory of files
12 * PATHSEP - OPTIONAL: path separator (default is unix)
13 * DEBUG - OPTIONAL: specify YES/NO to turn on debugging for API calls
14 * TGT_TICKET - specify TGT used to generate ST for API calls
15 *
16 *----------------------------------------------------------------------*/
17
18%macro irmst_export_mip_udl(
19 chunk_size = 1000,
20 model_group_sk =,
21 outpath = %sysfunc(pathname(work)),
22 pathsep = /,
23 debug = NO,
24 mipurl = ,
25 tgt_ticket =
26 ) / minoperator;
27
28 %local i
29 j
30 length
31 model_group_nm;
32
33 /*------------------------------------------------------------
34 * Query for available MODEL_GROUP_SK values if not specified
35 *------------------------------------------------------------*/
36 %if %quote(&model_group_sk) eq %then
37 %do;
38 %local num_models;
39
40 %irm_rest_get_ticket(
41 url = &mipurl/rest/modelGroups,
42 ticketType = st,
43 tgt_ticket = %superq(tgt_ticket),
44 outVarTicket = service_ticket,
45 %if %qupcase(&debug) ne NO %then
46 debug = true;
47 %else
48 debug = no;
49 );
50
51 filename mgsjson temp;
52
53 proc http url = "&mipurl/rest/modelGroups?%str(&)ticket=&service_ticket"
54 out = mgsjson
55 method = 'GET'
56 expect_100_continue;
57 %if %qupcase(&debug) ne NO %then
58 %do;
59 debug level = 2;
60 %end;
61 headers "Accept" = "application/json"
62 "Content-Type" = "application/json";
63 run;
64
65 libname mgs_json JSON fileref = mgsjson;
66
67 proc sql noprint;
68 select id into :model_group_sk1-
69 from mgs_json.items
70 order by id;
71 quit;
72
73 filename mgsjson clear;
74 libname mgs_json clear;
75
76 %let num_models = &sqlobs;
77
78 %do i = 1 %to &num_models;
79 %put NOTE: Extracting model group &i of &num_models: &&model_group_sk&i..;
80 %irmst_export_mip_udl(
81 chunk_size = &chunk_size,
82 model_group_sk = &&model_group_sk&i,
83 outpath = &outpath,
84 pathsep = &pathsep,
85 debug = &debug,
86 mipurl = %superq(mipurl),
87 tgt_ticket = %superq(tgt_ticket)
88 )
89 %end;
90 %return;
91 %end;
92
93 %do j = 1 %to %sysfunc(countw(&model_group_sk));
94
95 %let model_group_sk&j = %scan(&model_group_sk, &j);
96
97 /*-------------------------------
98 * Get file character length
99 *-------------------------------*/
100 %irm_rest_get_ticket(
101 url = &mipurl/rest/modelGroups/&&model_group_sk&j,
102 ticketType = st,
103 tgt_ticket = %superq(tgt_ticket),
104 outVarTicket = service_ticket,
105 %if %qupcase(&debug) ne NO %then
106 debug = true;
107 %else
108 debug = no;
109 );
110
111 filename mgjson temp;
112
113 proc http url = "&mipurl/rest/modelGroups/&&model_group_sk&j?%str(&)ticket=&service_ticket"
114 out = mgjson
115 method = 'GET'
116 expect_100_continue;
117 %if %qupcase(&debug) ne NO %then
118 %do;
119 debug level = 2;
120 %end;
121 headers "Accept" = "application/json"
122 "Content-Type" = "application/json";
123 run;
124
125 libname mg_json JSON fileref = mgjson;
126
127 proc sql noprint;
128 select length(code) into :length trimmed
129 from mg_json.udl;
130 quit;
131
132 %if &length eq %then
133 %do;
134 %put ERROR: MODEL_GROUP_SK &&model_group_sk&j was not found.;
135 %return;
136 %end;
137
138 /*--------------------------------------
139 * Extract the file in a SAS data set
140 *--------------------------------------*/
141 proc sql noprint;
142 create table work.udl_file as
143 %do i = 0 %to %sysfunc(floor(%sysevalf(&length/&chunk_size)));
144 select substr(code,&i*&chunk_size+1,&chunk_size) as udl_file
145 from mg_json.udl
146 %if &i ne %sysfunc(floor(%sysevalf(&length/&chunk_size))) %then
147 union all;
148 %end;
149 ;
150
151 select name into :model_group_nm trimmed
152 from mg_json.root;
153 quit;
154
155 filename mgjson clear;
156 libname mg_json clear;
157
158 /*--------------------------------------
159 * Write the file to the desired path
160 *--------------------------------------*/
161 filename _udlfile "&outpath&pathsep&model_group_nm..sas";
162 data _null_;
163 set work.udl_file end=last;
164 file _udlfile lrecl=32767 termstr=lf;
165 put udl_file $char&chunk_size.. @;
166 run;
167 filename _udlfile clear;
168
169 %end;
170
171 /*---------------------------------------------------
172 * Delete Macro Vars Generated by JSON Libname
173 *---------------------------------------------------*/
174 %local vars_to_delete;
175 proc sql noprint;
176 select name
177 into : vars_to_delete
178 separated by ' '
179 from sashelp.vmacro
180 where scope eq 'GLOBAL' and
181 (upcase(name) like 'MGS_JSON_%' or
182 upcase(name) like 'MG_JSON_%');
183 quit;
184
185 %if vars_to_delete ne %then
186 %do;
187 %put NOTE: Deleting variable(s) &vars_to_delete.;
188 %symdel &vars_to_delete.;
189 %end;
190
191%mend irmst_export_mip_udl;