Loading...
Searching...
No Matches
getdata.sas
Go to the documentation of this file.
1/**
2 @file getdata.sas
3 @brief Returns a dataset to the editor front end
4 @details
5
6 <h4> Service Inputs </h4>
7
8 <h5> SASCONTROLTABLE </h5>
9 |LIBDS:$41.|FILTER_RK:$5.|
10 |---|---|
11 |DC258467.MPE_X_TEST|-1|
12
13 <h4> Service Outputs </h4>
14 <h5> sasdata </h5>
15 <h5> sasparams </h5>
16 Contains info on the request. One row is returned.
17 @li CLS_FLG - set to 0 if there are no CLS rules (everything editable)
18 else set to 1 (CLS rules exist)
19 @li ISMAP - set to 1 if the target DS is an excel map target, else 0
20 @li TRUNCATED - set to 1 if the selection was cut down to the
21 DC_MAXOBS_WEBEDIT / DC_MAXCELLS_WEBEDIT limit, else 0
22 @li TRUNC_MSG - the reason shown against the table name when TRUNCATED=1,
23 else empty
24 @li DERIVEDVARS - space separated list of the derived (computed) columns
25 configured in MPE_DERIVED, upper case, empty when there are none. They
26 are appended after the physical columns and are read-only; the EDIT grid
27 marks them so a user can see why a column cannot be edited.
28 @li PRE_EDIT_HOOK - path of the PRE_EDIT hook MPE_TABLES configures for this
29 table, empty when there is none. This hook has already run over the rows
30 in sasdata.
31 @li POST_EDIT_HOOK - path of the POST_EDIT hook, empty when there is none.
32 @li PRE_APPROVE_HOOK - path of the PRE_APPROVE hook, empty when there is none.
33 @li POST_APPROVE_HOOK - path of the POST_APPROVE hook, empty when there is
34 none. The EDIT title marks a table carrying any of the four.
35
36 <h5> approvers </h5>
37 <h5> dqrules </h5>
38 <h5> dqdata </h5>
39 <h5> cols </h5>
40 Contains column level attributes.
41 @li NAME - column name
42 @li LABEL - var label. https://core.sasjs.io/mp__getcols_8sas.html
43 @li FMTNAME - derived format. https://core.sasjs.io/mp__getcols_8sas.html
44 @li DDTYPE - derived dropdown. C=CHARACTER, N=NUMERIC, else DATE / TIME /
45 DATETIME. https://core.sasjs.io/mp__getcols_8sas.html
46 @li CLS_RULE - values include:
47 - EDIT - the column is editable
48 - READ - the column should be readonly
49 - HIDE - the column should be hidden
50 @li memlabel
51 @li desc- augmented with MPE_DATADICTIONARY if exists, else label
52 @li longdesc - from MPE_DATADICTIONARY
53 @li coltype - front-end column formatting spec
54
55
56 <h5> maxvarlengths </h5>
57 <h5> xl_rules </h5>
58 <h5> query </h5>
59
60 <h5> versions </h5>
61 history of DC versions for this particular table
62
63
64 <h4> SAS Macros </h4>
65 @li dc_assignlib.sas
66 @li dc_casload.sas
67 @li dc_getgroupmembers.sas
68 @li mf_existvar.sas
69 @li mf_getattrn.sas
70 @li mf_getvarlist.sas
71 @li mf_existds.sas
72 @li mf_getquotedstr.sas
73 @li mf_getuniquefileref.sas
74 @li mf_getuniquename.sas
75 @li mf_getuser.sas
76 @li mf_nobs.sas
77 @li mf_verifymacvars.sas
78 @li mf_wordsinstr1butnotstr2.sas
79 @li mp_abort.sas
80 @li mp_cntlout.sas
81 @li mp_getcols.sas
82 @li mp_getmaxvarlengths.sas
83 @li mp_validatecol.sas
84 @li mpe_accesscheck.sas
85 @li mpe_columnlevelsecurity.sas
86 @li mpe_derived.sas
87 @li mpe_dsmeta.sas
88 @li mpe_getlabels.sas
89 @li mpe_getversions.sas
90 @li mpe_filtermaster.sas
91 @li mpe_runhook.sas
92 @li mpeinit.sas
93 @li mpeterm.sas
94
95 @version 9.2
96 @author 4GL Apps Ltd
97 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
98 and may not be re-distributed or re-sold without the express permission of
99 4GL Apps Ltd.
100
101 @sasjslint {"maxLineLength": 83}
102
103**/
104
105%mpeinit()
106
107/**
108 * Validate inputs
109 */
110data work.intest;
111 length filter_rk 8;
112 set work.SASCONTROLTABLE;
113
114 /* validate filter_rk */
115 if filter_rk le 0 then filter_rk=-1;
116
117 call symputx('orig_libds',upcase(libds));
118
119 is_fmt=0;
120 if substr(cats(reverse(libds)),1,3)=:'CF-' then do;
121 libds=scan(libds,1,'-');
122 putlog "Format Catalog Captured";
123 is_fmt=1;
124 libds='work.fmtextract';
125 call symputx('libds',libds);
126 end;
127 call symputx('is_fmt',is_fmt);
128 putlog (_all_)(=);
129
130 /* validate libds */
131 %mp_validatecol(LIBDS,LIBDS,is_libds)
132
133 if is_libds=0 then do;
134 putlog 'ERR' 'OR: Invalid libds:' libds;
135 stop;
136 end;
137 else do;
138 call symputx('filter_rk',filter_rk);
139 call symputx('libds',libds);
140 end;
141 output;
142 stop;
143run;
144
145%mp_abort(iftrue= (%mf_nobs(work.intest)=0)
146 ,mac=&_program
147 ,msg=%str(Some err with service inputs)
148)
149
150%mp_abort(
151 iftrue=(%mf_verifymacvars(libds filter_rk)=0)
152 ,mac=&_program
153 ,msg=%str(Missing: libds filter_rk)
154)
155
156/* export format catalog */
157%mp_cntlout(
158 iftrue=(&is_fmt=1)
159 ,libcat=&orig_libds
160 ,fmtlist=0
161 ,cntlout=work.fmtextract
162)
163
164/* stream back meta info, further calls will return col metadata and actual data
165*/
166%let libref=%upcase(%scan(&libds,1,.));
167%let dsn=%upcase(%scan(&libds,2,.));
168%dc_assignlib(WRITE,&libref)
169%dc_casload(&libds)
170
171/**
172 * First check user has access permission to edit the table
173 */
174%put checking access;
175%let user=%mf_getuser();
176%mpe_accesscheck(&orig_libds,outds=mw_auth,user=&user,access_level=EDIT)
177
178
179%mp_abort(iftrue= (%mf_getattrn(work.mw_auth,NLOBS)=0)
180 ,mac=mpestp_getdata.sas
181 ,msg=&user is not authorised to edit &orig_libds %trim(
182 )in the &mpelib..MPE_SECURITY table
183)
184
185%mp_abort(iftrue= ( %mf_existds(libds=&libds) ne 1)
186 ,mac=mpestp_getdata.sas
187 ,msg=dataset &libds does not exist!!
188)
189
190%mp_abort(iftrue= (&syscc ne 0)
191 ,mac=&_program..sas
192 ,msg=%str(syscc=&syscc at line 60 )
193)
194
195
196%global loadtype var_txfrom var_txto var_processed filter_text pk coltype
197 sortpk var_busfrom var_busto rk_underlying buskey pre_edit_hook
198 post_edit_hook pre_approve_hook post_approve_hook;
199/* clear first: a global survives the call, so in a reused session it would
200 still hold the previous request's values, and any path that skips an
201 assignment would apply them to this one */
202%let loadtype=;
203%let var_txfrom=;
204%let var_txto=;
205%let var_processed=;
206%let filter_text=;
207%let pk=;
208%let coltype=;
209%let sortpk=;
210%let var_busfrom=;
211%let var_busto=;
212%let rk_underlying=;
213%let buskey=;
214%let pre_edit_hook=;
215%let post_edit_hook=;
216%let pre_approve_hook=;
217%let post_approve_hook=;
218
219%put getting table attributes;
220proc sql noprint;
221select upcase(loadtype)
222 ,var_txfrom,var_txto
223 ,var_busfrom,var_busto
224 ,var_processed,rk_underlying,buskey
225 ,coalesce(rk_underlying,buskey)
226 ,pre_edit_hook,post_edit_hook
227 ,pre_approve_hook,post_approve_hook
228 ,case when missing(rk_underlying) then buskey else rk_underlying end
229 into: loadtype,:var_txfrom,:var_txto
230 ,:var_busfrom ,:var_busto
231 ,:var_processed,:rk_underlying,:buskey, :sortPK, :pre_edit_hook
232 ,:post_edit_hook,:pre_approve_hook,:post_approve_hook,:pk
233 from &mpelib..mpe_tables
234 where &dc_dttmtfmt. lt TX_TO
235 and upcase(dsn)="%scan(&orig_libds,2,.)"
236 and upcase(libref)="%scan(&orig_libds,1,.)";
237
238%put preparing filter query:;
239%mpe_filtermaster(EDIT,&orig_libds,
240 dclib=&mpelib,
241 filter_rk=&filter_rk,
242 outref=filtref,
243 outds=work.query
244)
245
246/**
247 * Derived (computed) variables, configured in MPE_DERIVED. They are added
248 * via a view, so that the filter (applied by the step that reads the view)
249 * can reference them - a WHERE clause cannot see a variable that is created
250 * in the same data step.
251 */
252%mpe_derived(libds=&libds,
253 dclib=&mpelib,
254 outref=dercode,
255 outview=work.vw_derived,
256 outvars=work.derived_vars
257)
258%let derived_nobs=%mf_nobs(work.derived_vars);
259%put &=derived_nobs;
260
261%macro mpestp_getdata();
262 %if not %symexist(DC_MAXOBS_WEBEDIT) %then %do;
263 %put NOTE:;%put NOTE- DC_MAXOBS_WEBEDIT not found!;
264 %put NOTE- Please add to &mpelib..MPE_CONFIG table;
265 %put NOTE-;%put NOTE-;
266 %global DC_MAXOBS_WEBEDIT;
267 %let DC_MAXOBS_WEBEDIT=250;
268 %end;
269 %if not %symexist(DC_MAXCELLS_WEBEDIT) %then %do;
270 %put NOTE:;%put NOTE- DC_MAXCELLS_WEBEDIT not found!;
271 %put NOTE- Please add to &mpelib..MPE_CONFIG table;
272 %put NOTE-;%put NOTE-;
273 %global DC_MAXCELLS_WEBEDIT;
274 %let DC_MAXCELLS_WEBEDIT=200000;
275 %end;
276 /* for tables which use RKs/SKs then we just expose the business key to
277 users - this lets uploads be sent to multiple environments (with
278 potentially different RK/SK values for the same business key).
279 Note that the config table has the RK column in the buskey field in
280 this scenario. */
281 %if %length(&rk_underlying)>0 %then %let drop_rk=&buskey;
282 %else %let drop_rk=;
283
284 /* always remove the PROCESSED_DTTM column, if it exists */
285 %if %length(&var_processed)=0 %then %do;
286 %if %mf_existvar(&libds,PROCESSED_DTTM)>0 %then
287 %let var_processed=PROCESSED_DTTM;
288 %end;
289
290 /**
291 * Now get the slice of the actual table
292 */
293 options obs=10000;
294
295 %if &loadtype=BITEMPORAL %then %do;
296 data out (drop=&var_txfrom &var_txto &var_processed &drop_rk );
297 %if &derived_nobs>0 %then %do;
298 set work.vw_derived;
299 %end;
300 %else %do;
301 set &libds;
302 %end;
303 where %inc filtref;;
304 run;
305 proc sort data=out;
306 by &pk &var_busfrom;
307 run;
308 data out;
309 set out;
310 by &pk &var_busfrom;
311 if last.%scan(&pk,-1);
312 run;
313 %end;
314 %else %do;
315 data out (drop=&var_txfrom &var_txto &var_processed &drop_rk);
316 %if &derived_nobs>0 %then %do;
317 set work.vw_derived;
318 %end;
319 %else %do;
320 set &libds;
321 %end;
322 where %inc filtref;;
323 run;
324 %end;
325 options obs=max;
326 %mp_abort(iftrue= (&syscc ne 0)
327 ,mac=&_program
328 ,msg=%str(Issue with filtering (line 165) )
329 )
330
331 /* Sort first - the PRE_EDIT_HOOK contract is that it runs on sorted rows -
332 then run the hook, then apply the row and cell limits (below). */
333 %let sortpk=%sysfunc(coalescec(&sortpk &var_busfrom,_ALL_));
334 proc sort data=work.out; by &sortPK; run;
335
336 %mpe_runhook(PRE_EDIT_HOOK)
337
338 /* Serve the selection truncated, at whichever limit binds first, and carry
339 the reason in sasparams for the EDIT screen to show against the table
340 name. Bounding the browser's work is the point: a wide table exhausts the
341 cell budget long before it reaches the row limit - 250 rows of a 1000
342 column table is a quarter of a million cells - so the row cap is
343 min(DC_MAXOBS_WEBEDIT, DC_MAXCELLS_WEBEDIT / columns). Counting after the
344 PRE_EDIT_HOOK means the cap covers what the hook added to work.out. */
345 %let obscnt=%mf_getattrn(work.out,NLOBS);
346 %let colcnt=%sysfunc(countw(%mf_getvarlist(work.out)));
347 %let maxrows=&DC_MAXOBS_WEBEDIT;
348 %let limitvar=DC_MAXOBS_WEBEDIT;
349 %if &colcnt>0 %then %do;
350 %let cellrows=%sysfunc(floor(&DC_MAXCELLS_WEBEDIT/&colcnt));
351 %if %sysevalf(&cellrows lt &maxrows) %then %do;
352 %let maxrows=&cellrows;
353 %let limitvar=DC_MAXCELLS_WEBEDIT;
354 %end;
355 %end;
356 %if %sysevalf(&maxrows lt 1) %then %let maxrows=1;
357 %global truncated trunc_msg;
358 %let truncated=0;
359 %if %sysevalf(&obscnt gt &maxrows) %then %let truncated=1;
360 %let trunc_msg=;
361 %if &truncated=1 %then %do;
362 %let trunc_msg=Only the first &maxrows rows are loaded;
363 %let trunc_msg=&trunc_msg - the &limitvar limit has been reached.;
364 %let trunc_msg=&trunc_msg Filter the table to work with the rest.;
365 %end;
366
367
368 /* order pk fields at start of table. The delete flag is NOT added here:
369 _____DELETE__THIS__RECORD_____ is a client-owned column, created by the
370 editor itself (see deleteRecordColumnRule.ts), and getdata must never
371 emit it - a real table column of that name is refused by the frontend. */
372 %let sourcevars=%mf_wordsInStr1ButNotStr2(
373 Str1=%mf_getvarlist(work.out)
374 ,Str2=&pk
375 );
376 %put sourcevars=&sourcevars;
377 /* the payload is capped at &maxrows - whichever limit that works out to be
378 - this is what goes to the browser. The cap is a data set option on the
379 SET, not `options obs=`, so no later step in this session can be cut by it:
380 a session option left set would truncate the metadata steps below, and a
381 table wide enough to hit the cell limit would lose columns rather than rows. */
382 data outdata;
383 /* pk fields come first */
384 %if %length(&pk)>0 %then %do;
385 attrib &pk label='';
386 %end;
387 /* keep remaining variable order */
388 %if %length(&sourcevars)>0 %then %do;
389 attrib &sourcevars label='';
390 %end;
391 %if %mf_nobs(work.out)=0 %then %do;
392 /* send empty row if empty table to help with hot rendering */
393 output;
394 %end;
395 set work.out(obs=&maxrows) ;
396 run;
397
398
399
400 /* get list of variables and their formats */
401 proc contents noprint data=outdata
402 out=vars(keep=name type length varnum format: label);
403 run;
404
405 proc sort;
406 by varnum;
407 run;
408
409 data vars3(keep=name type length format label pk varnum formatd);
410 set vars(rename=(format=format2 type=type2));
411 name=upcase(name);
412 /* not interested in transaction or processing dates
413 (append table must be supplied without them) */
414 if name not in ("&VAR_TXFROM","&VAR_TXTO","&VAR_PROCESSED");
415 if type2=2 or type2=6 then do;
416 length format $49.;
417 if format2='' then format=cats('$',length,'.');
418 else format=cats(format2,formatl,'.');
419 type='char';
420 end;
421 else do;
422 if format2='' then format=cats(length,'.');
423 else if upcase(format2)='DATETIME' and formatl=0 then format='DATETIME.';
424 else format=cats(format2,formatl,'.',formatd);
425 type='num';
426 end;
427
428 if name in ('',%upcase(%mf_getQuotedStr(&pk,dlm=%str(,),quote=S)))
429 then PK='YES';
430 run;
431 %mp_abort(iftrue= (&syscc ne 0)
432 ,mac=&_program..sas
433 ,msg=%str(syscc=&syscc at 242 (vars3 step) in &_program \n
434 %superq(syserrortext)
435 )
436 )
437
438 %global jsdttmvars jsdtvars jstmvars;
439 data vars4;
440 set vars3;
441 length coltype $500.;
442 format=upcase(format);
443 coltype=cats('{"data":"',name,'"');
444 if type='num' then do;
445 if format=:'DATETIME' or format=:'E8601DT' or format=:'NLDATM'
446 then do;
447 colType=cats(coltype,',"type":"datetime"}');
448 /* build var list to reformat datetimes in javascript format */
449 call symput('jsdttmvars',symget('jsdttmvars')!!' '!!name);
450 end;
451 else if format=:'DATE' or format=:'DDMMYY' or format=:'MMDDYY'
452 or format=:'YYMMDD' or format=:'E8601DA' or format=:'B8601DA'
453 or format=:'MONYY' or format=:'NLDATE'
454 then do;
455 /* see bottom of file for more date formats!! */
456 /* also when updating, update stagedata.sas and mp_getcols.sas
457 and mpe_loader.sas */
458 colType=cats(coltype,',"type":"date"}');
459 /* build var list to reformat as javascript dates */
460 call symput('jsdtvars',symget('jsdtvars')!!' '!!name);
461 end;
462 else if format=:'TIME' or format=:'HHMM' then do;
463 colType=cats(coltype,',"type":"time"}');
464 /* build var list to reformat as javascript times */
465 call symput('jstmvars',symget('jstmvars')!!' '!!name);
466 end;
467 else do;
468 /* is standard numeric but need to ascertain precision */
469 retain base '000000000000000000';
470 if formatd>0 then numFormat=cats('.',substr(base,1,formatd));
471 colType=cats(coltype,',"type":"numeric","format":"0',numFormat,'"}');
472 end;
473 end;
474 else colType=cats(coltype,'}');
475 output;
476 putlog (_all_)(=);
477 run;
478
479 %mp_abort(iftrue= (&syscc ne 0)
480 ,mac=&_program..sas
481 ,msg=%str(syscc=&syscc at 283 (null step) in &_program)
482 )
483
484 PROC FORMAT;
485 picture yymmddThhmmss (default=28) other='%0Y-%0m-%0d %0H:%0M:%0s'
486 (datatype=datetime);
487 picture JSyymmdd other='%0Y-%0m-%0d' (datatype=date);
488 picture JShhmmss (default=16) other='%0H:%0M:%0s' (datatype=time);
489 RUN;
490 /* before we send the data, need to rebuild all date & datetime vars as char*/
491 %let finalvars=%mf_getvarlist(work.outdata);
492 data sasdata;
493 /* set formats & col order ahead of rename+import */
494 informat &finalvars ;
495 /* read dataset and rename date / datetime vars as necessary */
496 set outdata
497 %if %length(&jsdttmvars&jsdtvars&jstmvars)>0 %then %do;
498 (rename=(
499 %local dtvarnum dtvar tmvar;
500 /* temp datetime vars end in _____ */
501 %do dtvarnum=1 %to %sysfunc(countw(&jsdttmvars,%str( )));
502 %let dtvar=%scan(&jsdttmvars ,&dtvarnum);
503 &dtvar=_____&dtvarnum._____
504 %end;
505 /* temp date vars do not end in _____ */
506 %do dtvarnum=1 %to %sysfunc(countw(&jsdtvars,%str( )));
507 %let dtvar=%scan( &jsdtvars,&dtvarnum);
508 &dtvar=_____&dtvarnum
509 %end;
510 /* temp time vars end in ___tm */
511 %do tmvarnum=1 %to %sysfunc(countw(&jstmvars,%str( )));
512 %let tmvar=%scan( &jstmvars,&tmvarnum);
513 &tmvar=_____&tmvarnum.___tm
514 %end;
515 ))
516 %end;
517 ;
518 %if %length(&jsdttmvars)>0 %then %do ;
519 %do dtvarnum=1 %to %sysfunc(countw(&jsdttmvars,%str( )));
520 %let dtvar=%scan(&jsdttmvars,&dtvarnum);
521 &dtvar=cats(put(_____&dtvarnum._____,yymmddThhmmss28.));
522 if &dtvar="ERROR" then call missing(&dtvar);
523 drop _____&dtvarnum._____;
524 %end;
525 %end;
526 %if %length(&jsdtvars)>0 %then %do;
527 %do dtvarnum=1 %to %sysfunc(countw(&jsdtvars,%str( )));
528 %let dtvar=%scan(&jsdtvars,&dtvarnum);
529 &dtvar=cats(put(_____&dtvarnum,JSyymmdd.));
530 if &dtvar="ERROR" then call missing(&dtvar);
531 drop _____&dtvarnum;
532 %end;
533 %end;
534 %if %length(&jstmvars)>0 %then %do;
535 %do tmvarnum=1 %to %sysfunc(countw(&jstmvars,%str( )));
536 %let tmvar=%scan(&jstmvars,&tmvarnum);
537 &tmvar=cats(put(_____&tmvarnum.___tm,JShhmmss14.));
538 if &tmvar="ERROR" then call missing(&tmvar);
539 drop _____&tmvarnum.___tm;
540 %end;
541 %end;
542 output;
543 run;
544
545 /* get the relevant approvers for the drop down */
546 %put getting approvers;
547 %local sas_groups sas_i sas_group;
548 proc sql noprint;
549 select distinct sas_Group into: sas_groups separated by "|"
550 from &mpelib..mpe_security
551 where libref="%scan(&orig_libds,1,.)"
552 and dsn="%scan(&orig_libds,2,.)"
553 and access_level='APPROVE'
554 and &dc_dttmtfmt. lt TX_TO;
555 %mp_abort(iftrue= (&syscc ne 0)
556 ,mac=&_program
557 ,msg=%str(syscc=&syscc extracting from &mpelib..mpe_security)
558 )
559
560 %if %length(&sas_groups)=0 %then %do;
561 %dc_getgroupmembers(&dc_admin_group,outds=work.access1)
562 %end;
563 %else %do sas_i=1 %to %sysfunc(countw(&sas_groups,%str(|)));
564 %let sas_group=%scan(&sas_Groups,&sas_i,%str(|));
565 %dc_getgroupmembers(&sas_group,outds=work.temp&sas_i)
566 proc append base=work.access1 data=work.temp&sas_i(keep=membername);run;
567 %end;
568 %mp_abort(iftrue= (&syscc ne 0)
569 ,mac=&_program
570 ,msg=%str(syscc=&syscc after dc_getgroupmembers for &sas_groups)
571 )
572
573%mend mpestp_getdata;
574
575%mpestp_getdata()
576
577/* mp_abort cannot exit cleanly from inside a %include, so an abort there
578 (eg in a hook script run via mp_include) only records the failure in
579 work.mp_abort_errds. mode=INCLUDE picks that up and aborts properly here. */
580%mp_abort(mode=INCLUDE)
581
582/* extract column level security rules */
583%mpe_columnlevelsecurity(%scan(&libds,1,.),%scan(&libds,2,.),work.sasdata
584 ,mode=EDIT
585 ,clsds=&mpelib..mpe_column_level_security
586 ,groupds=work.groups /* was created in mpe_filtermaster */
587 ,outds=work.sasdata1
588 ,outmeta=work.cls_rules
589)
590%mp_abort(iftrue= (&syscc ne 0)
591 ,mac=&_program
592 ,msg=%str(syscc=&syscc extracting from &mpelib..mpe_column_level_security)
593)
594
595/* get labels - pull column names from the CLS-filtered WORK dataset but
596 join to mpe_datadictionary using the underlying LIB.DSN reference */
597%mpe_getlabels(COLUMNS,sasdata1,tgt_ds=&libds,outds=spec)
598%mp_abort(iftrue= (&syscc ne 0)
599 ,mac=&_program
600 ,msg=%str(syscc=&syscc extracting spec info)
601)
602
603/* extract col info */
604%mp_getcols(&libds, outds=cols1)
605
606/* derived variables are added to the view, so take their attributes from
607 there - this keeps type, length, format and label identical to the data */
608%macro add_derived_cols();
609 %if &derived_nobs>0 %then %do;
610 %mp_getcols(work.vw_derived, outds=derived_cols_all)
611 proc sql;
612 create table work.derived_cols as
613 select a.*
614 from work.derived_cols_all a
615 where a.NAME in (select NAME from work.derived_vars);
616 quit;
617 data work.cols1;
618 set work.cols1 work.derived_cols;
619 run;
620 %end;
621%mend add_derived_cols;
622%add_derived_cols()
623
624/* join with cls rules. Trim COLS payload in the SQL below - DDTYPE
625 C=CHARACTER N=NUMERIC (dates kept in full), VARNUM dropped (client
626 relies on column order, not position) */
627proc sql;
628create table work.cols as
629 select a.NAME
630 ,coalesce(c.desc,a.LABEL,a.NAME) as LABEL
631 ,a.FMTNAME
632 ,case a.DDTYPE
633 when 'CHARACTER' then 'C'
634 when 'NUMERIC' then 'N'
635 else a.DDTYPE end as DDTYPE
636 ,case b.cls_hide
637 when 1 then 'HIDE'
638 when 0 then 'EDIT'
639 else 'READ' end as CLS_RULE
640 ,c.memlabel
641 ,c.longdesc
642 ,d.colType
643 from work.cols1(drop=varnum) a
644 left join work.cls_rules b
645 on a.NAME=b.CLS_VARIABLE_NM
646 left join work.spec c
647 on a.NAME=c.NAME
648 left join work.vars4 d
649 on a.NAME=d.NAME;
650
651proc sql;
652create table approvers as select distinct membername as personname
653 ,membername as email, membername as userid
654 from work.access1;
655/*
656create table access3 as select b.userid,b.email
657 from access2 a
658 ,support.users b
659 where a.personname=b.userid
660 and a.personname ne "%mf_getuser()"
661 and %sysfunc(datetime()) lt b.tx_to_dttm
662 order by 1;
663*/
664data _null_;
665 infile filtref end=eof;
666 input;
667 length filter_text $32767;
668 retain filter_text;
669 filter_text=catx(' ',filter_text,_infile_);
670 if eof then do;
671 if cats(filter_text)='1=1' then filter_text='';
672 call symputx('filter_text',filter_text);
673 end;
674run;
675
676%put params;
677%let ismap=0;
678proc sql noprint;
679select count(*) into: ismap from &mpelib..mpe_xlmap_info
680 where XLMAP_TARGETLIBDS="&orig_libds" and &dc_dttmtfmt. le TX_TO;
681
682/* the derived columns, named for the EDIT grid so it can mark them as special.
683 They are read-only through the READONLY DQ rule below, but that rule does not
684 say WHY the column cannot be edited - a computed column is a different thing
685 from an ordinary read-only one. work.derived_vars exists on every path, empty
686 when the table has no derived variables. */
687%let derivedvars=;
688proc sql noprint;
689select upcase(name) into: derivedvars separated by ' '
690 from work.derived_vars;
691quit;
692
693data sasparams;
694 length colHeaders $20000 filter_text $32767 trunc_msg $200 derivedvars $32767
695 pre_edit_hook $200 post_edit_hook $200 pre_approve_hook $200
696 post_approve_hook $200;
697 colHeaders=cats(upcase("%mf_getvarlist(sasdata1,dlm=%str(,))"));
698 pkCnt=countw("&pk");
699 pk="&pk";
700 dtvars=compbl("&jsdtvars");
701 dttmvars=compbl("&jsdttmvars");
702 tmvars=compbl("&jstmvars");
703 loadtype=symget('loadtype');
704 if trim(symget('rk_underlying')) ne '' then rk_flag=1;
705 else rk_flag=0;
706 filter_text=symget('filter_text');
707 derivedvars=symget('derivedvars');
708 /* set when the selection was cut down to the row / cell limit, so the EDIT
709 screen can say so against the table name */
710 truncated=&truncated;
711 trunc_msg="&trunc_msg";
712 /* the edit hooks run over the rows - pre-edit before they reach the grid,
713 post-edit before a submit is validated - and the approve hooks run around
714 the approval, so the EDIT title marks a table with any of them configured */
715 pre_edit_hook=symget('pre_edit_hook');
716 post_edit_hook=symget('post_edit_hook');
717 pre_approve_hook=symget('pre_approve_hook');
718 post_approve_hook=symget('post_approve_hook');
719 if %mf_nobs(work.cls_rules)=0 then cls_flag=0;
720 else cls_flag=1;
721 put (_all_)(=);
722 if "&orig_libds"="&mpelib..MPE_XLMAP_DATA" or &ismap ne 0 then ismap=1;
723 else ismap=0;
724run;
725
726
727/* Extract validation DQ Rules */
728proc sort data=&mpelib..mpe_validations
729 (where=(&dc_dttmtfmt. le TX_TO
730 and BASE_LIB="%scan(&orig_libds,1,.)" and BASE_DS="%scan(&orig_libds,2,.)"
731 and rule_active=1))
732 out=dqrules (keep=base_col rule_type rule_value);
733 by base_col rule_type rule_value;
734run;
735
736/* merge with NOTNULL constraints in the physical table, and mark the derived
737 variables read-only: a derived column is computed at read time, so the grid
738 must not accept a value for it. COLS carries the same READ (a derived column
739 has no CLS rule row) but the client only applies COLS.CLS_RULE when CLS_FLAG
740 is set, which is driven by the table's column-level security - so the rule is
741 declared here, where it holds regardless.
742
743 work.derived_vars exists on every path that reaches this step: %mpe_derived
744 writes it before returning, empty when the table has no derived variables,
745 so the union below needs no guard */
746proc sql;
747create table _data_ as
748 select * from dqrules
749union
750 select upcase(name) as base_col
751 ,'NOTNULL' as rule_type
752 ,'' as rule_value
753 from dictionary.columns
754 where upcase(libname)="%scan(&orig_libds,1,.)"
755 and upcase(memname)="%scan(&orig_libds,2,.)"
756 and upcase(name) in (select name from vars3)
757 and notnull='yes'
758union
759 select upcase(name) as base_col
760 ,'READONLY' as rule_type
761 ,'' as rule_value
762 from work.derived_vars
763 order by 1,2,3;
764data dqrules;
765 set &syslast;
766 by base_col rule_type rule_value;
767 if last.rule_type;
768 if rule_type in ('HARDSELECT','SOFTSELECT') and countw(rule_value)=3 then
769 do;
770 retain x 0; x+1;
771 call symputx(cats('source',x),rule_value);
772 %let sourcecnt=0;
773 call symputx('sourcecnt',x);
774 call symputx(cats('base_col',x),base_col);
775 end;
776run;
777
778proc sql;
779create table dqdata as
780 select distinct base_column as base_col length=32
781 ,upcase(base_column) as rule_value length=74 /* deprecated */
782 ,selectbox_value as rule_data length=1000
783 ,selectbox_order
784 from &mpelib..mpe_selectbox
785 where &dc_dttmtfmt. lt ver_to_dttm
786 and select_lib="%scan(&orig_libds,1,.)"
787 and select_ds="%scan(&orig_libds,2,.)";
788
789%mp_abort(iftrue= (&syscc ne 0)
790 ,mac=&_program
791 ,msg=%str(syscc=&syscc during DQ rule validation)
792)
793
794/* extract selectbox data */
795%macro dq_selects();
796 %local x source lib ds col filtref filterds srcview;
797 %do x=1 %to &sourcecnt;
798 %let source=&&source&x;
799 %let lib=%scan(&source,1,.);
800 %let ds=%scan(&source,2,.);
801 %let col=%scan(&source,3,.);
802 %put &=source;
803 %put &=lib;
804 %dc_assignlib(READ,&lib)
805 %dc_casload(&lib..&ds)
806 /* Selectbox values are read straight from the source table, so the same
807 Row Level Security that filters the grid must be applied here too -
808 otherwise values from rows the user is not permitted to see leak into
809 the cell dropdown. %mpe_filtermaster builds the filter; a data step
810 view is used because a fileref cannot be %inc'd inside a proc sql where
811 clause. EDIT mode mirrors the grid on this (editor) page. */
812 %let filtref=%mf_getuniquefileref();
813 %let filterds=%mf_getuniquename();
814 %mpe_filtermaster(EDIT,&lib..&ds,
815 dclib=&mpelib,
816 outref=&filtref,
817 outds=work.&filterds
818 )
819 %let srcview=%mf_getuniquename();
820 data work.&srcview / view=work.&srcview;
821 set &lib..&ds;
822 where %inc &filtref;;
823 run;
824 proc sql;
825 create table dqdata&x as
826 select distinct "&&base_col&x" as base_col length=32
827 ,"&source" as rule_value length=74
828 ,cats(&col) as rule_data length=1000
829 ,&col as tmp_order
830 from work.&srcview
831 order by tmp_order;
832 /* ensure both numerics and char vals are ordered correctly */
833 data work.dqdata&x (drop=tmp_order);
834 set work.dqdata&x;
835 selectbox_order=_n_;
836 run;
837 %mp_abort(iftrue= (&syscc ne 0)
838 ,mac=&_program
839 ,msg=%str(syscc=&syscc when selecting &&base_col&x from &orig_libds)
840 )
841 proc append base=dqdata data=dqdata&x;run;
842 proc sql; drop table dqdata&x;
843 /* drop the per-source filter artifacts - WORK is session scoped, but the
844 loop should not leave a view and a filter table behind per source */
845 proc datasets lib=work nolist;
846 delete &srcview &filterds;
847 quit;
848 filename &filtref clear;
849 %end;
850%mend dq_selects;
851%dq_selects()
852
853proc sort data=dqdata;
854 /* order by selectbox_order then the value */
855 by base_col selectbox_order rule_data;
856run;
857
858%mp_getmaxvarlengths(work.sasdata1,outds=maxvarlengths)
859
860data xl_rules;
861 set &mpelib..mpe_excel_config;
862 where &dc_dttmtfmt. lt tx_to;
863 where also upcase(xl_libref)="%scan(&orig_libds,1,.)";
864 where also upcase(xl_table)="%scan(&orig_libds,2,.)";
865 where also xl_active=1;
866 keep xl_column xl_rule;
867run;
868
869%mpe_dsmeta(&orig_libds, outds=dsmeta)
870
871%mpe_getversions(&mpelib,
872 %scan(&orig_libds,1,.),
873 %scan(&orig_libds,2,.),
874 outds=versions
875)
876
877
878/* send to the client */
879%webout(OPEN)
880%webout(OBJ,approvers)
881%webout(OBJ,cols)
882%webout(OBJ,dqdata,missing=STRING)
883%webout(OBJ,dqrules)
884%webout(OBJ,dsmeta)
885%webout(OBJ,maxvarlengths)
886%webout(OBJ,query)
887%webout(OBJ,sasdata1,fmt=N,missing=STRING,showmeta=YES,dslabel=sasdata)
888%webout(OBJ,sasparams)
889%webout(OBJ,versions)
890%webout(OBJ,xl_rules)
891%webout(CLOSE)
892
893/*
894$N8601Bw
895$N8601BAw
896$N8601Ew
897$N8601EAw
898$N8601EHw
899$N8601EXw
900$N8601Hw
901$N8601Xw
902B8601DAw
903B8601DNw
904B8601DTw
905B8601DZw
906B8601LZw
907B8601TMw
908B8601TZw
909DATEw
910DATEAMPMw
911DATETIMEw
912DAYw
913DDMMYYw
914DDMMYYxw
915DOWNAMEw
916DTDATEw
917DTMONYYw
918DTWKDATXw
919DTYEARw
920DTYYQCw
921E8601DAw
922E8601DNw
923E8601DTw
924E8601DZw
925E8601LZw
926E8601TMw
927E8601TZw
928HHMMw
929HOURw
930JULDAYw
931JULIANw
932MMDDYYw
933MMDDYYxw
934MMSSw
935MMYYw
936MMYYxw
937MONNAMEw
938MONTHw
939MONYYw
940PDJULGw
941PDJULIw
942QTRw
943QTRRw
944TIMEw
945TIMEAMPMw
946TODw
947WEEKDATEw
948WEEKDATXw
949WEEKDAYw
950WEEKUw
951WEEKVw
952WEEKWw
953WORDDATEw
954WORDDATXw
955YEARw
956YYMMw
957YYMMxw
958YYMMDDw
959YYMMDDxw
960YYMONw
961YYQw
962YYQxw
963YYQRw
964YYQRxw
965$N8601BAw
966$N8601Ew
967$N8601EAw
968$N8601EHw
969$N8601EXw
970$N8601Hw
971$N8601Xw
972B8601DAw
973B8601DNw
974B8601DTw
975B8601DZw
976B8601LZw
977B8601TMw
978B8601TZw
979E8601DAw
980E8601DNw
981E8601DTw
982E8601DZw
983E8601LZw
984E8601TMw
985E8601TZw
986*/
987%mpeterm()