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:$5.|SEARCHTYPE:$4|SEARCHVAL:$1000
12 |---|---|---|---
13 |DC258467.MPE_X_TEST|-1|CHAR|Some String|
14
15 <h4> Service Outputs </h4>
16
17 <h5> cols </h5>
18 @li DDTYPE
19 @li FORMAT
20 @li LABEL
21 @li LENGTH
22 @li NAME
23 @li TYPE
24 @li VARNUM
25
26 <h5> sasparams </h5>
27 @li FILTER_TEXT
28 @li NOBS
29 @li PK_FIELDS - string seperated list of primary key fields, if they exist
30 @li TABLENAME
31 @li TABLEURI
32 @li VARS
33
34 <h5> versions </h5>
35 history of DC versions for this particular table
36
37 <h5> viewdata </h5>
38 The raw data from the target table.
39
40 <h4> SAS Macros </h4>
41 @li dc_assignlib.sas
42 @li dc_casload.sas
43 @li dc_createdataset.sas
44 @li dc_gettableid.sas
45 @li mf_existds.sas
46 @li mf_getvarcount.sas
47 @li mf_nobs.sas
48 @li mf_verifymacvars.sas
49 @li mp_abort.sas
50 @li mp_cntlout.sas
51 @li mp_getcols.sas
52 @li mp_getpk.sas
53 @li mp_jsonout.sas
54 @li mp_searchdata.sas
55 @li mp_validatecol.sas
56 @li mpe_columnlevelsecurity.sas
57 @li mpe_dsmeta.sas
58 @li mpe_getversions.sas
59 @li mpe_filtermaster.sas
60
61
62 @version 9.2
63 @author 4GL Apps Ltd
64 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
65 and may not be re-distributed or re-sold without the express permission of
66 4GL Apps Ltd.
67**/
68
69%mpeinit()
70
71/* configure macvars */
72%global LIBDS FILTER_RK SEARCHVAL SEARCHTYPE FMT_IND;
73
74%let maxrows=250;
75
76/* avoid code injection */
77%let FMT_IND=0;
78%let SEARCHTYPE=;
79%let SEARCHVAL=;
80%let FILTER_RK=;
81%let LIBDS=;
82
83/**
84 * Validate inputs
85 */
86data work.intest;
87 length libds $41 filter_rk 8. searchval $100 searchtype $4;
88 set work.SASCONTROLTABLE;
89 call symputx('orig_libds',libds);
90 /* validate filter_rk */
91 if filter_rk le 0 then filter_rk=-1;
92
93 /* check if the request is for a format catalog */
94 if substr(cats(reverse(libds)),1,3)=:'CF-' then do;
95 libds=scan(libds,1,'-');
96 putlog "Format Catalog Captured";
97 call symputx('fmt_ind',1);
98 end;
99 putlog (_all_)(=);
100
101 /* validate libds */
102 %mp_validatecol(LIBDS,LIBDS,is_libds)
103
104 if searchtype in ('CHAR','NUM') then do;
105 searchval=tranwrd(searchval,'%','');
106 searchval=tranwrd(searchval,'&','');
107 searchval=tranwrd(searchval,';','');
108 searchval=tranwrd(searchval,'"','');
109 call symputx('searchtype',searchtype);
110 call symputx('searchval',searchval);
111 end;
112 else if searchtype not in ('','NONE') then do;
113 putlog 'ERR' 'OR: Invalid searchtype:' searchtype;
114 stop;
115 end;
116
117 if is_libds=0 then do;
118 putlog 'ERR' 'OR: Invalid libds:' libds;
119 stop;
120 end;
121 else do;
122 call symputx('filter_rk',filter_rk);
123 call symputx('libds',libds);
124 end;
125 output;
126 stop;
127run;
128
129%mp_abort(iftrue= (%mf_verifymacvars(libds filter_rk fmt_ind)=0)
130 ,mac=&_program..sas
131 ,msg=%str(Problem with macro inputs)
132)
133
134%mp_abort(iftrue= (%mf_nobs(work.intest)=0)
135 ,mac=&_program
136 ,msg=%str(Some err with service inputs)
137)
138%mp_abort(iftrue= (&syscc ne 0)
139 ,mac=&_program..sas
140 ,msg=%str(syscc=&syscc)
141)
142
143
144/**
145 * assign the Library
146 */
147%dc_assignlib(READ,%scan(&LIBDS,1,.))
148%dc_casload(&libds)
149
150/* abort if looking for a format and the catalog doesn't exist */
151%mp_abort(iftrue= (&fmt_ind=1 and %sysfunc(exist(&libds,CATALOG))=0)
152 ,mac=&_program..sas
153 ,msg=%str(Catalog &libds does not exist!)
154)
155
156/**
157 check if dataset can actually be opened - as library may exist but it may not
158 be possible to assign, and even if it can, the physical table may not exist
159**/
160data _null_;
161 if &fmt_ind=0 then do;
162 dsid=open("&libds");
163 rc=close(dsid);
164 end;
165 else dsid=42;
166 call symputx('existds',dsid,'l');
167 putlog 'dataset exists check:' dsid;
168run;
169
170/**
171 * get the data
172 */
173%global dsobs;
174%let dsobs=0;
175%macro x();
176%if &existds>0 %then %do;
177
178 %if &fmt_ind=1 %then %do;
179 /* export format and point the libds to the output table from here on */
180 %mp_cntlout(
181 libcat=&libds
182 ,fmtlist=0
183 ,cntlout=work.fmtextract
184 )
185 %let libds=WORK.FMTEXTRACT;
186 proc datasets lib=work noprint;
187 modify FMTEXTRACT;
188 index create
189 pk_cntlout=(type fmtname fmtrow)
190 /nomiss unique;
191 quit;
192 %end;
193
194 proc sql noprint;
195 select count(*) into: dsobs from &libds;
196
197 %put preparing query;
198 %mpe_filtermaster(VIEW,&libds,
199 dclib=&mpelib,
200 filter_rk=&filter_rk,
201 outref=filtref,
202 outds=work.query
203 )
204 %put printing generated filterquery:;
205 data _null_;
206 infile filtref;
207 input;
208 putlog _infile_;
209 run;
210
211 %if &searchtype=NONE or "%trim(&searchtype) " = " " %then %do;
212 /* get row count */
213 filename rows temp;
214 data _null_;
215 file rows;
216 infile filtref end=eof;
217 input;
218 if _n_=1 then do;
219 put 'proc sql;';
220 put "select count(*) into: dsobs from &libds where";
221 end;
222 put _infile_;
223 if eof then put ';';
224 run;
225 data _null_;
226 infile rows;
227 input;
228 putlog _infile_;
229 run;
230
231 %inc rows;
232
233 /* send actual data, filtered and row-capped */
234 data work.viewdata;
235 set &libds;
236 where %inc filtref;;
237 if _n_>&maxrows then stop;
238 run;
239 %if %mf_nobs(work.viewdata)=0 %then %do;
240 data work.viewdata;
241 /* send empty row if empty table to help with hot rendering */
242 output;
243 set work.viewdata;
244 run;
245 %end;
246 %end;
247 %else %do;
248 data work.vwsearch/view=work.vwsearch;
249 set &libds;
250 where %inc filtref;;
251 run;
252 %if %upcase(&searchtype)=CHAR %then %do;
253 %mp_searchdata(lib=work
254 ,ds=vwsearch
255 ,string=%superq(searchval)
256 ,outobs=&maxrows
257 )
258 %end;
259 %else %if %upcase(&searchtype)=NUM %then %do;
260 %mp_searchdata(lib=work
261 ,ds=vwsearch
262 ,numval=%superq(searchval)
263 ,outobs=&maxrows
264 )
265 %end;
266 %if %mf_existds(libds=MPSEARCH.vwsearch) %then %do;
267 %let dsobs=%mf_nobs(MPSEARCH.vwsearch);
268 data viewdata;
269 set MPSEARCH.vwsearch;
270 if _n_<&maxrows;
271 run;
272 %end;
273 %else %do;
274 %let dsobs=0;
275 data viewdata;
276 set &libds;
277 stop;
278 run;
279 %end;
280 %end;
281%end;
282%else %do;
283 /* physical table is not accessible so create from metatadata definition */
284 %dc_createdataset(libds=&libds,outds=viewdata)
285 data viewData;
286 output;
287 set viewdata;
288 run;
289
290 /* make filtref / work.query / work.groups to avoid downstream issues */
291 filename filtref temp;
292 data work.query;
293 file filtref;
294 x=0;
295 put x;
296 run;
297 data work.groups;
298 length groupuri groupname $32 groupdesc $128 ;
299 call missing (of _all_);
300 output;
301 stop;
302 run;
303%end;
304%mend x; %x()
305
306/* apply column level security */
307%mpe_columnlevelsecurity(%scan(&libds,1,.),%scan(&libds,2,.),work.viewdata
308 ,mode=VIEW
309 ,clsds=&mpelib..mpe_column_level_security
310 ,groupds=work.groups /* was created in mpe_filtermaster */
311 ,outds=work.viewdata2
312 ,outmeta=work.cls_rules
313)
314
315
316/* get table uri (if sas 9) to enable linking direct to lineage */
317%dc_gettableid(libref=%scan(&libds,1,.)
318 ,ds=%scan(&libds,2,.)
319 ,outds=work.parambase
320)
321
322data _null_;
323 infile filtref end=eof;
324 input;
325 length filter_text $32767;
326 retain filter_text;
327 filter_text=catx(' ',filter_text,_infile_);
328 if eof then do;
329 if cats(filter_text)='1=1' then filter_text='';
330 call symputx('filter_text',filter_text);
331 end;
332run;
333
334%mp_getpk(%scan(&libds,1,.), ds=%scan(&libds,2,.), outds=work.pk_fields)
335
336%let pk_fields=;
337data _null_;
338 set work.pk_fields;
339 call symputx('pk_fields',pk_fields);
340run;
341
342data work.sasparams;
343 set work.parambase;
344 format FILTER_TEXT $32767.;
345 FILTER_TEXT=symget('FILTER_TEXT');
346 length PK_FIELDS $512;
347 PK_FIELDS=symget('PK_FIELDS');
348 nobs=&dsobs;
349 vars=%mf_getvarcount(viewdata);
350 maxrows=&maxrows;
351run;
352
353%mp_abort(iftrue= (&syscc ne 0)
354 ,mac=&_program..sas
355 ,msg=%str(syscc=&syscc)
356)
357
358%mp_getcols(&libds, outds=cols)
359
360%mpe_dsmeta(&orig_libds, outds=dsmeta)
361
362%mpe_getversions(&mpelib,
363 %scan(&orig_libds,1,.),
364 %scan(&orig_libds,2,.),
365 outds=versions
366)
367
368%webout(OPEN)
369%webout(OBJ,cls_rules)
370%webout(OBJ,cols)
371%webout(OBJ,dsmeta)
372%webout(OBJ,query)
373%webout(OBJ,sasparams)
374%webout(OBJ,versions)
375%webout(OBJ,viewData2,fmt=Y,missing=STRING,showmeta=YES,dslabel=viewdata)
376%webout(CLOSE)
377
378%mpeterm()