Loading...
Searching...
No Matches
viewdata.sas
Go to the documentation of this file.
1/**
2 @file viewdata.sas
3 @brief Provide the raw view of the data
4 @details Pass a LIBDS and FILTER_RK to return a dataset for viewing.
5 VIEW datasets include all columns / rows (unlike EDIT, which are filtered
6 for current records and don't include the SCD2 etc cols).
7
8 <h4> Service Inputs </h4>
9
10 <h5> SASCONTROLTABLE </h5>
11 |LIBDS:$41.|FILTER_RK:best.|SEARCHTYPE:$4|SEARCHVAL:$100|
12 |---|---|---|---
13 |DC258467.MPE_X_TEST|-1|CHAR|Some String|
14
15 No input column may be longer than the length declared in this service -
16 LIBDS 41, SEARCHTYPE 4, SEARCHVAL 100. An input column longer than the
17 declared length stops the step with "Multiple lengths were specified for
18 the variable ...", so the SASjs adapter sends the $char form (eg $char4.),
19 which also preserves any leading blanks.
20
21 <h4> Service Outputs </h4>
22
23 <h5> cols </h5>
24 @li DDTYPE - C=CHARACTER, N=NUMERIC, else DATE / TIME / DATETIME
25 @li FORMAT
26 @li LABEL
27 @li LENGTH
28 @li NAME
29 @li TYPE
30
31 <h5> sasparams </h5>
32 @li FILTER_TEXT
33 @li NOBS
34 @li PK_FIELDS - string seperated list of primary key fields, if they exist
35 @li TABLENAME
36 @li TABLEURI
37 @li VARS
38
39 <h5> versions </h5>
40 history of DC versions for this particular table
41
42 <h5> viewdata </h5>
43 The raw data from the target table.
44
45 <h4> SAS Macros </h4>
46 @li dc_assignlib.sas
47 @li dc_casload.sas
48 @li dc_createdataset.sas
49 @li dc_gettableid.sas
50 @li mf_existds.sas
51 @li mf_getvarcount.sas
52 @li mf_nobs.sas
53 @li mf_verifymacvars.sas
54 @li mp_abort.sas
55 @li mp_cntlout.sas
56 @li mp_getcols.sas
57 @li mp_getpk.sas
58 @li mp_searchdata.sas
59 @li mp_validatecol.sas
60 @li mpe_columnlevelsecurity.sas
61 @li mpe_derived.sas
62 @li mpe_dsmeta.sas
63 @li mpe_getversions.sas
64 @li mpe_filtermaster.sas
65
66
67 @version 9.2
68 @author 4GL Apps Ltd
69 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
70 and may not be re-distributed or re-sold without the express permission of
71 4GL Apps Ltd.
72**/
73
74%mpeinit()
75
76/* configure macvars */
77%global LIBDS FILTER_RK SEARCHVAL SEARCHTYPE FMT_IND;
78
79/* avoid code injection */
80%let FMT_IND=0;
81%let SEARCHTYPE=;
82%let SEARCHVAL=;
83%let FILTER_RK=;
84%let LIBDS=;
85
86/**
87 * Validate inputs
88 */
89data work.intest;
90 length libds $41 filter_rk 8. searchval $100 searchtype $4;
91 set work.SASCONTROLTABLE;
92 call symputx('orig_libds',libds);
93 /* validate filter_rk */
94 if filter_rk le 0 then filter_rk=-1;
95
96 /* check if the request is for a format catalog */
97 if substr(cats(reverse(libds)),1,3)=:'CF-' then do;
98 libds=scan(libds,1,'-');
99 putlog "Format Catalog Captured";
100 call symputx('fmt_ind',1);
101 end;
102 putlog (_all_)(=);
103
104 /* validate libds */
105 %mp_validatecol(LIBDS,LIBDS,is_libds)
106
107 if searchtype in ('CHAR','NUM') then do;
108 searchval=tranwrd(searchval,'%','');
109 searchval=tranwrd(searchval,'&','');
110 searchval=tranwrd(searchval,';','');
111 searchval=tranwrd(searchval,'"','');
112 call symputx('searchtype',searchtype);
113 call symputx('searchval',searchval);
114 end;
115 else if searchtype not in ('','NONE') then do;
116 putlog 'ERR' 'OR: Invalid searchtype:' searchtype;
117 stop;
118 end;
119
120 if is_libds=0 then do;
121 putlog 'ERR' 'OR: Invalid libds:' libds;
122 stop;
123 end;
124 else do;
125 call symputx('filter_rk',filter_rk);
126 call symputx('libds',libds);
127 end;
128 output;
129 stop;
130run;
131
132%mp_abort(iftrue= (%mf_verifymacvars(libds filter_rk fmt_ind)=0)
133 ,mac=&_program..sas
134 ,msg=%str(Problem with macro inputs)
135)
136
137%mp_abort(iftrue= (%mf_nobs(work.intest)=0)
138 ,mac=&_program
139 ,msg=%str(Some err with service inputs)
140)
141%mp_abort(iftrue= (&syscc ne 0)
142 ,mac=&_program..sas
143 ,msg=%str(syscc=&syscc)
144)
145
146
147/**
148 * assign the Library
149 */
150%dc_assignlib(READ,%scan(&LIBDS,1,.))
151%dc_casload(&libds)
152
153/* abort if looking for a format and the catalog doesn't exist */
154%mp_abort(iftrue= (&fmt_ind=1 and %sysfunc(exist(&libds,CATALOG))=0)
155 ,mac=&_program..sas
156 ,msg=%str(Catalog &libds does not exist!)
157)
158
159/**
160 check if dataset can actually be opened - as library may exist but it may not
161 be possible to assign, and even if it can, the physical table may not exist
162**/
163data _null_;
164 if &fmt_ind=0 then do;
165 dsid=open("&libds");
166 rc=close(dsid);
167 end;
168 else dsid=42;
169 call symputx('existds',dsid,'l');
170 putlog 'dataset exists check:' dsid;
171run;
172
173/**
174 * get the data
175 */
176%global dsobs;
177%let dsobs=0;
178%macro viewdata_body();
179%if not %symexist(DC_MAXOBS_WEBVIEW) %then %do;
180 %put NOTE:;%put NOTE- DC_MAXOBS_WEBVIEW not found!;
181 %put NOTE- Please add to &mpelib..MPE_CONFIG table;
182 %put NOTE-;%put NOTE-;
183 %global DC_MAXOBS_WEBVIEW;
184 %let DC_MAXOBS_WEBVIEW=2000;
185%end;
186%if &existds>0 %then %do;
187
188 %if &fmt_ind=1 %then %do;
189 /* export format and point the libds to the output table from here on */
190 %mp_cntlout(
191 libcat=&libds
192 ,fmtlist=0
193 ,cntlout=work.fmtextract
194 )
195 %let libds=WORK.FMTEXTRACT;
196 proc datasets lib=work noprint;
197 modify FMTEXTRACT;
198 index create
199 pk_cntlout=(type fmtname fmtrow)
200 /nomiss unique;
201 quit;
202 %end;
203
204 proc sql noprint;
205 select count(*) into: dsobs from &libds;
206
207 %put preparing query;
208 %mpe_filtermaster(VIEW,&libds,
209 dclib=&mpelib,
210 filter_rk=&filter_rk,
211 outref=filtref,
212 outds=work.query
213 )
214 %put printing generated filterquery:;
215 data _null_;
216 infile filtref;
217 input;
218 putlog _infile_;
219 run;
220
221 /* derived variables exist only in a view, so a filter that references one
222 must be applied against it. The columns themselves are dropped from the
223 output - they are an EDIT screen feature. */
224 %mpe_derived(libds=&libds,
225 dclib=&mpelib,
226 outref=dercode,
227 outview=work.vw_derived,
228 outvars=work.derived_vars
229 )
230 %let derived_nobs=%mf_nobs(work.derived_vars);
231 %let derived_droplist=;
232 %let filterds=%sysfunc(ifc(&derived_nobs>0,work.vw_derived,&libds));
233 %if &derived_nobs>0 %then %do;
234 proc sql noprint;
235 select name into: derived_droplist separated by ' '
236 from work.derived_vars;
237 quit;
238 %end;
239
240 %if &searchtype=NONE or "%trim(&searchtype) " = " " %then %do;
241 /* get row count. SQL has no colon modifier, so this query takes the
242 truncated operators the clause is equivalent to - EQT for =: and NET
243 for ^=: (the SQL procedure's truncated string comparison operators).
244 The data step below keeps the clause as written; only this query is
245 swapped. The operator is the second token of a line written by
246 mp_filtergenerate, so anchoring the pattern at the start of the line
247 leaves a raw value carrying the same characters untouched. */
248 filename rows temp;
249 data _null_;
250 file rows;
251 infile filtref end=eof;
252 input;
253 /* the line is the variable, the operator and a raw value, and
254 mp_validatefilter shapes a raw value as $32767 - an IN list of long
255 values makes the line longer than 4000, and the parenthesis the
256 rewrite appends is what a shorter buffer cuts */
257 length sql_filter $32767;
258 if _n_=1 then do;
259 put 'proc sql;';
260 put "select count(*) into: dsobs from &filterds where";
261 end;
262 sql_filter=prxchange('s/^( *\S+ +)\^=:( |$)/$1net$2/',1,_infile_);
263 sql_filter=prxchange('s/^( *\S+ +)=:( |$)/$1eqt$2/',1,sql_filter);
264 put sql_filter;
265 if eof then put ';';
266 run;
267 data _null_;
268 infile rows;
269 input;
270 putlog _infile_;
271 run;
272
273 %inc rows;
274
275 /* send actual data, filtered and row-capped */
276 data work.viewdata;
277 set &filterds;
278 where %inc filtref;;
279 %if %length(&derived_droplist)>0 %then %do;
280 drop &derived_droplist;
281 %end;
282 if _n_>&DC_MAXOBS_WEBVIEW then stop;
283 run;
284 %if %mf_nobs(work.viewdata)=0 %then %do;
285 data work.viewdata;
286 /* send empty row if empty table to help with hot rendering */
287 output;
288 set work.viewdata;
289 run;
290 %end;
291 %end;
292 %else %do;
293 data work.vwsearch/view=work.vwsearch;
294 set &filterds;
295 where %inc filtref;;
296 %if %length(&derived_droplist)>0 %then %do;
297 drop &derived_droplist;
298 %end;
299 run;
300 %if %upcase(&searchtype)=CHAR %then %do;
301 %mp_searchdata(lib=work
302 ,ds=vwsearch
303 ,string=%superq(searchval)
304 ,outobs=&DC_MAXOBS_WEBVIEW
305 )
306 %end;
307 %else %if %upcase(&searchtype)=NUM %then %do;
308 %mp_searchdata(lib=work
309 ,ds=vwsearch
310 ,numval=%superq(searchval)
311 ,outobs=&DC_MAXOBS_WEBVIEW
312 )
313 %end;
314 %if %mf_existds(libds=MPSEARCH.vwsearch) %then %do;
315 %let dsobs=%mf_nobs(MPSEARCH.vwsearch);
316 /* the derived columns were dropped from work.vwsearch above, so
317 mp_searchdata's output cannot carry them */
318 data viewdata;
319 set MPSEARCH.vwsearch;
320 if _n_<=&DC_MAXOBS_WEBVIEW;
321 run;
322 %end;
323 %else %do;
324 %let dsobs=0;
325 data viewdata;
326 set &filterds;
327 stop;
328 run;
329 %end;
330 %end;
331%end;
332%else %do;
333 /* physical table is not accessible so create from metatadata definition */
334 %dc_createdataset(libds=&libds,outds=viewdata)
335 data viewData;
336 output;
337 set viewdata;
338 run;
339
340 /* make filtref / work.query / work.groups to avoid downstream issues */
341 filename filtref temp;
342 data work.query;
343 file filtref;
344 x=0;
345 put x;
346 run;
347 data work.groups;
348 length groupuri groupname $32 groupdesc $128 ;
349 call missing (of _all_);
350 output;
351 stop;
352 run;
353%end;
354%mend viewdata_body; %viewdata_body()
355
356/* apply column level security */
357%mpe_columnlevelsecurity(%scan(&libds,1,.),%scan(&libds,2,.),work.viewdata
358 ,mode=VIEW
359 ,clsds=&mpelib..mpe_column_level_security
360 ,groupds=work.groups /* was created in mpe_filtermaster */
361 ,outds=work.viewdata2
362 ,outmeta=work.cls_rules
363)
364
365
366/* get table uri (if sas 9) to enable linking direct to lineage */
367%dc_gettableid(libref=%scan(&libds,1,.)
368 ,ds=%scan(&libds,2,.)
369 ,outds=work.parambase
370)
371
372data _null_;
373 infile filtref end=eof;
374 input;
375 length filter_text $32767;
376 retain filter_text;
377 filter_text=catx(' ',filter_text,_infile_);
378 if eof then do;
379 if cats(filter_text)='1=1' then filter_text='';
380 call symputx('filter_text',filter_text);
381 end;
382run;
383
384%mp_getpk(%scan(&libds,1,.), ds=%scan(&libds,2,.), outds=work.pk_fields)
385
386%let pk_fields=;
387data _null_;
388 set work.pk_fields;
389 call symputx('pk_fields',pk_fields);
390run;
391
392data work.sasparams;
393 set work.parambase;
394 format FILTER_TEXT $32767.;
395 FILTER_TEXT=symget('FILTER_TEXT');
396 length PK_FIELDS $512;
397 PK_FIELDS=symget('PK_FIELDS');
398 nobs=&dsobs;
399 vars=%mf_getvarcount(viewdata);
400 maxrows=&DC_MAXOBS_WEBVIEW;
401run;
402
403%mp_abort(iftrue= (&syscc ne 0)
404 ,mac=&_program..sas
405 ,msg=%str(syscc=&syscc)
406)
407
408%mp_getcols(&libds, outds=cols1)
409
410/* trim COLS payload in the SQL below - DDTYPE C=CHARACTER N=NUMERIC
411 (dates kept in full), VARNUM dropped (client relies on column order,
412 not position) */
413proc sql;
414create table cols as
415 select a.name
416 ,a.type
417 ,a.length
418 ,a.format
419 ,a.fmtname
420 ,case a.ddtype
421 when 'CHARACTER' then 'C'
422 when 'NUMERIC' then 'N'
423 else a.ddtype end as ddtype
424 ,coalesce(b.dd_shortdesc,a.label,a.name) as label
425 from cols1 a
426 left join &mpelib..mpe_datadictionary
427 (where=(&dc_dttmtfmt. < tx_to
428 and dd_source ? %upcase("&orig_libds")
429 and dd_type='COLUMN')) b
430 on scan(b.dd_source,-1,'.')=upcase(a.name);
431quit;
432
433
434%mpe_dsmeta(&orig_libds, outds=dsmeta)
435
436%mpe_getversions(&mpelib,
437 %scan(&orig_libds,1,.),
438 %scan(&orig_libds,2,.),
439 outds=versions
440)
441
442%webout(OPEN)
443%webout(OBJ,cls_rules)
444%webout(OBJ,cols)
445%webout(OBJ,dsmeta)
446%webout(OBJ,query)
447%webout(OBJ,sasparams)
448%webout(OBJ,versions)
449%webout(OBJ,viewData2,fmt=Y,missing=STRING,showmeta=YES,dslabel=viewdata)
450%webout(CLOSE)
451
452%mpeterm()