Loading...
Searching...
No Matches
mpe_targetloader.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Generic loader for tables configured in &mpelib..mpe_tables
4 @details General wrapper for loading tables using ALL load types.
5
6 Called by admin/postdata. The dclib should already be assigned.
7
8 @param [in] dclib= (NOTPROVIDED) The data controller control library
9 @param [in] libds= the library.dataset to load
10 @param [in] now= (%sysfunc(datetime())) Static processed timestamp
11
12 <h4> SAS Macros </h4>
13 @li bitemporal_dataloader.sas
14 @li mf_existvar.sas
15 @li mf_getengine.sas
16 @li mf_getuniquename.sas
17 @li mf_getuser.sas
18 @li mp_abort.sas
19 @li mp_loadformat.sas
20 @li mp_lockanytable.sas
21
22 @version 9.2
23 @author 4GL Apps Ltd
24 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
25 and may not be re-distributed or re-sold without the express permission of
26 4GL Apps Ltd.
27
28**/
29
30%macro mpe_targetloader(libds= /* library.dataset to LOAD (target) */
31 ,now= %sysfunc(datetime()) /* static processed timestamp */
32 ,etlsource= /* process from whence the data came */
33 ,STAGING_DS= STAGING_DS /* name of staging (work) dataset which should
34 be appended into the target. */
35 ,LOADTARGET=NO /* set to yes to actually load the target */
36 ,CLOSE_VARS= /* provide close vars to override defaults */
37 ,dclib=NOTPROVIDED
38 ,mdebug=0
39 ,dc_dttmtfmt=%sysfunc(datetime())
40 );
41%local lib ds nobs;
42
43/**
44 * if a format catalog (suffix "-FC") we assume the catalog has already been
45 * created by the calling program with a libds of work.fmtextract
46*/
47%let orig_lib=%upcase(%scan(&libds,1,.));
48%let orig_ds=%upcase(%scan(&libds,2,.));
49%let orig_libds=&libds;
50%if %scan(&libds,2,-)=FC %then %do;
51 %let lib=WORK;
52 %let ds=FMTEXTRACT;
53 %let libds=&lib..&ds;
54%end;
55%else %do;
56 %let lib=&orig_lib;
57 %let ds=&orig_ds;
58%end;
59
60%mp_abort(iftrue= (&dclib=NOTPROVIDED)
61 ,mac=&sysmacroname
62 ,msg=%str(dclib=NOTPROVIDED)
63)
64
65/* get table attributes */
66%let nobs=0;
67data work.sumo_config;
68 set &mpelib..mpe_tables;
69 where &dc_dttmtfmt. lt tx_to
70 and libref="&orig_lib"
71 and dsn="&orig_ds";
72 call symputx('LOADTYPE',loadtype,'l');
73 call symputx('BUSKEY',buskey,'l');
74 call symputx('VAR_TXFROM',var_txfrom,'l');
75 call symputx('VAR_TXTO',var_txto,'l');
76 call symputx('VAR_BUSFROM',var_busfrom,'l');
77 call symputx('VAR_BUSTO',var_busto,'l');
78 call symputx('VAR_PROCESSED',VAR_PROCESSED,'l');
79 call symputx('RK_UNDERLYING',RK_UNDERLYING,'l');
80 %if %length(&CLOSE_VARS)=0 %then %do;
81 call symputx('CLOSE_VARS',CLOSE_VARS,'l');
82 %end;
83 call symputx('nobs',_n_,'l');
84 if missing(AUDIT_LIBDS) then AUDIT_LIBDS="&dclib..MPE_AUDIT";
85 call symputx('AUDIT_LIBDS',AUDIT_LIBDS,'l');
86 put (_all_)(=);
87run;
88
89/* check if table is actually configured to load */
90%if &nobs ne 1 %then %do;
91 proc sql;
92 insert into &mpelib..mpe_loads
93 set USER_NM="%mf_getuser()"
94 ,STATUS='FAILED (BAD DS)'
95 ,CSV_DIR=symget('ETLSOURCE')
96 ,PROCESSED_DTTM=&now;
97%end;
98%mp_abort(iftrue= (&nobs=0)
99 ,mac=&sysmacroname
100 ,msg=%str(Table not registered in &mpelib..mpe_tables)
101)
102%mp_abort(iftrue= (&nobs>1)
103 ,mac=&sysmacroname
104 ,msg=%str(Something is very wrong with the PK in &mpelib..mpe_tables)
105)
106
107
108%if &LOADTYPE=TXTEMPORAL %then %do;
109
110 %bitemporal_dataloader(bus_from=,bus_to= /* explicitly empty*/
111 ,tech_from=&VAR_TXFROM
112 ,tech_to = &VAR_TXTO
113 ,base_lib=&lib
114 ,base_dsn=&ds
115 ,append_lib=WORK
116 ,append_dsn=&STAGING_DS
117 ,high_date='31DEC9999:23:59:59'dt
118 ,PK= &buskey
119 ,ETLSOURCE=&ETLSOURCE
120 ,LOADTYPE=&loadtype
121 ,RK_UNDERLYING=&RK_UNDERLYING
122 ,LOADTARGET=&LOADTARGET
123 ,RK_UPDATE_MAXKEYTABLE=&LOADTARGET
124 ,CLOSE_VARS=&CLOSE_VARS
125 ,processed=&VAR_PROCESSED
126 ,dclib=&dclib
127 ,outds_audit=&AUDIT_LIBDS
128 )
129%end;
130%else %if &loadtype=REPLACE %then %do;
131 %if &LOADTARGET=YES %then %do;
132 %mp_lockanytable(LOCK,lib=&lib,ds=&ds,ref=%str(&etlsource),
133 ctl_ds=&dclib..mpe_lockanytable
134 )
135 data WORK.&STAGING_DS;
136 set WORK.&STAGING_DS;
137 %if %mf_existvar(&libds,&VAR_PROCESSED) %then %do;
138 &VAR_PROCESSED = &now;
139 %end;
140 drop _____DELETE__THIS__RECORD_____;
141 run;
142 %local engine_type;
143 %let engine_type=%mf_getengine(&lib);
144 %if &engine_type=CAS %then %do;
145 /* Fixed char variables cannot be appended to CAS varchar columns, so
146 cast them (per the target table structure) in a CASUSER copy of the
147 staging table, then append via data step. Approach mirrors the CAS
148 append in bitemporal_dataloader.sas */
149 proc contents noprint data=&libds
150 out=work.rpl_base_cols(keep=name type);
151 run;
152 proc contents noprint data=WORK.&STAGING_DS
153 out=work.rpl_stag_cols(keep=name type);
154 run;
155 proc sql noprint;
156 create table work.rpl_vchars as
157 select a.name
158 from work.rpl_base_cols a
159 inner join work.rpl_stag_cols b
160 on upcase(a.name)=upcase(b.name)
161 where a.type=6; /* varchar in target */
162 quit;
163 /* get varchar variables ready for casting */
164 %local vcfmt vcrename vcassign vcdrop tmpds;
165 %let vcfmt=;
166 %let vcrename=;
167 %let vcassign=;
168 %let vcdrop=;
169 data _null_;
170 set work.rpl_vchars end=last;
171 length vcrename vcassign vcdrop vcfmt $32767 rancol $32;
172 retain vcrename vcassign vcdrop vcfmt;
173 if _n_=1 then vcrename='(rename=(';
174 rancol=resolve('%mf_getuniquename()');
175 vcfmt=trim(vcfmt)!!'length '!!cats(name)!!' varchar(*);';
176 vcrename=trim(vcrename)!!' '!!cats(name,'=',rancol);
177 vcassign=cats(vcassign,name,'=',rancol,';');
178 vcdrop=cats(vcdrop,'drop '!!rancol,';');
179 if last then do;
180 vcrename=cats(vcrename,'))');
181 call symputx('vcfmt',vcfmt);
182 call symputx('vcrename',vcrename);
183 call symputx('vcassign',vcassign);
184 call symputx('vcdrop',vcdrop);
185 end;
186 run;
187 /* prepare a temp cas table with varchars casted */
188 %let tmpds=%mf_getuniquename();
189 data casuser.&tmpds;
190 &vcfmt
191 set WORK.&STAGING_DS &vcrename;
192 &vcassign
193 &vcdrop
194 run;
195 /* exit on err condition before the destructive truncate below */
196 %if &syscc>0 %then %do;
197 %mp_lockanytable(UNLOCK,lib=&lib,ds=&ds,ref=&ETLSOURCE (aborted),
198 ctl_ds=&dclib..mpe_lockanytable
199 )
200 /* ensure cleanup */
201 proc sql;drop table CASUSER.&tmpds;quit;
202 %end;
203 %mp_abort(iftrue= (&syscc>0)
204 ,mac=&sysmacroname
205 ,msg=%str(syscc=&syscc - aborting before REPLACE truncate of &libds.)
206 )
207 /* CAS tables do not support SQL deletes, so truncate with deleteRows.
208 This is deliberately the last step before the append, to minimise
209 the time in which the target table is empty. */
210 proc cas;
211 table.deleteRows / table={caslib="&lib",name="&ds",where="1=1"};
212 quit;
213 /* load the target with varchars applied */
214 data &libds (append=yes) / sessref=dcsession;
215 set casuser.&tmpds;
216 run;
217 /* drop temp table */
218 proc sql;
219 drop table CASUSER.&tmpds;
220 quit;
221 %end;
222 %else %do;
223 /* exit on err condition before the destructive delete below */
224 %if &syscc>0 %then %do;
225 %mp_lockanytable(UNLOCK,lib=&lib,ds=&ds,ref=&ETLSOURCE (aborted),
226 ctl_ds=&dclib..mpe_lockanytable
227 )
228 %end;
229 %mp_abort(iftrue= (&syscc>0)
230 ,mac=&sysmacroname
231 ,msg=%str(syscc=&syscc - aborting before REPLACE delete of &libds.)
232 )
233 proc sql;
234 delete * from &libds;
235 quit;
236 proc append base=&libds data=WORK.&STAGING_DS force nowarn;run;
237 %end;
238
239 %mp_lockanytable(UNLOCK,lib=&lib,ds=&ds,ctl_ds=&dclib..mpe_lockanytable)
240 %end;
241 %else %do;
242 /* is full replace so treat all staged records as new in diff screen */
243 data work.outds_mod work.outds_add ;
244 set work.&staging_ds;
245 output work.outds_add;
246 run;
247 /* previous table will be considered fully deleted */
248 data work.outds_del;
249 set &lib..&ds;
250 run;
251 %end;
252%end;
253%else %if &loadtype=UPDATE %then %do;
254 %bitemporal_dataloader(bus_from=,bus_to=
255 ,tech_from= ,tech_to = /* explicitly empty*/
256 ,base_lib=&lib
257 ,base_dsn=&ds
258 ,append_lib=WORK
259 ,append_dsn=&STAGING_DS
260 ,high_date='31DEC9999:23:59:59'dt
261 ,PK= &buskey
262 ,ETLSOURCE=%superq(etlsource)
263 ,LOADTYPE=UPDATE
264 ,RK_UNDERLYING=&RK_UNDERLYING
265 ,LOADTARGET=&LOADTARGET
266 ,RK_UPDATE_MAXKEYTABLE=&LOADTARGET
267 ,processed=&VAR_PROCESSED
268 ,dclib=&dclib
269 ,outds_audit=&AUDIT_LIBDS
270 )
271%end;
272%else %if &loadtype=FORMAT_CAT %then %do;
273 /**
274 * run mp_formatload
275 * inputs:
276 * - LOADTARGET
277 * - CATALOG
278 * - STAGEDATA
279 * - LOADAUDIT
280 * outputs:
281 * work.outds_add
282 * work.outds_del
283 * work.outds_mod
284 */
285 %mp_loadformat(&orig_libds
286 ,&staging_ds
287 ,loadtarget=&LOADTARGET
288 ,auditlibds=&AUDIT_LIBDS
289 ,locklibds=&dclib..mpe_lockanytable
290 ,delete_col=_____DELETE__THIS__RECORD_____
291 ,outds_add=outds_add
292 ,outds_del=outds_del
293 ,outds_mod=outds_mod
294 ,mdebug=&mdebug
295 )
296%end;
297%else %if &loadtype=BITEMPORAL %then %do;
298 %bitemporal_dataloader(bus_from=&VAR_BUSFROM,bus_to=&VAR_BUSTO
299 ,tech_from=&VAR_TXFROM
300 ,tech_to = &VAR_TXTO
301 ,base_lib=&lib
302 ,base_dsn=&ds
303 ,append_lib=WORK
304 ,append_dsn=&STAGING_DS
305 ,high_date='31DEC9999:23:59:59'dt
306 ,PK= &buskey
307 ,ETLSOURCE=%superq(etlsource)
308 ,LOADTYPE=BITEMPORAL
309 ,RK_UNDERLYING=&RK_UNDERLYING
310 ,LOADTARGET=&LOADTARGET
311 ,RK_UPDATE_MAXKEYTABLE=&LOADTARGET
312 ,CLOSE_VARS=&CLOSE_VARS
313 ,processed=&VAR_PROCESSED
314 ,dclib=&dclib
315 ,outds_audit=&AUDIT_LIBDS
316 )
317%end;
318%else %do;
319 %put %str(WARN)ING: LOADTYPE &LOADTYPE not supported;
320 %let syscc=4;
321 %mp_abort(msg=LOADTYPE &LOADTYPE not supported,mac=mpe_targetloader.sas)
322%end;
323
324%mp_abort(iftrue= (&syscc ne 0)
325 ,mac=&_program
326 ,msg=%str(syscc=&syscc exiting MPE_TARGETLOADER macro)
327)
328
329%mend mpe_targetloader;