Loading...
Searching...
No Matches
getrawdata.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Downloads data in a variety of formats
4 @details To enable direct download, this service runs in a dedicated stream
5 as a GET request using URL parameters as inputs.
6
7 The inputs are:
8
9 @li table - the libds of the table to be downloaded
10 @li type - either SAS, CSV, EXCEL, MARKDOWN, PDF, WEBCSV or WEBTAB
11 @li filter - the filter RK if used
12
13 <h4> SAS Macros </h4>
14 @li mf_getuser.sas
15 @li mf_existfeature.sas
16 @li dc_assignlib.sas
17 @li mp_abort.sas
18 @li mp_cntlout.sas
19 @li mp_ds2cards.sas
20 @li mp_ds2csv.sas
21 @li mp_ds2md.sas
22 @li mp_ds2pdf.sas
23 @li mp_streamfile.sas
24 @li mp_validatecol.sas
25 @li mf_nobs.sas
26 @li mpe_derived.sas
27 @li mpe_validatecol.sas
28 @li mpe_filtermaster.sas
29
30
31 @version 9.2
32 @author 4GL Apps Ltd
33 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
34 and may not be re-distributed or re-sold without the express permission of
35 4GL Apps Ltd.
36
37**/
38%global table type filter ds format is_fmt txfrom txto;
39%mpeinit()
40
41%let user=%mf_getuser();
42%let is_fmt=0;
43
44/**
45 * Validate inputs before they reach executable code. table is used as a
46 * dataset reference, in the output file path, and in the download filename,
47 * so it must be a well-formed LIBREF.DATASET (the trailing -FC catalog
48 * suffix is permitted); filter must be an integer. Values are read with
49 * symget (never re-resolved) and validated in a data step so no macro
50 * content in the input can execute.
51 */
52%let is_libds=0;
53%let is_int=0;
54%let is_type=0;
55data _null_;
56 length _table $64 _filter $16 _type $16;
57 _type=upcase(coalescec(symget('type'),''));
58 _table=coalescec(symget('table'),'');
59 _filter=coalescec(symget('filter'),'0');
60 if missing(_table) then do;
61 putlog 'ERR' 'OR: Missing table';
62 stop;
63 end;
64 %mpe_validatecol(_table,LIBDS,is_libds)
65 /* an absent filter is a valid, unfiltered download */
66 if missing(_filter) then _filter='0';
67 %mp_validatecol(_filter,ISINT,is_int)
68 /* type is validated against a fixed list of download formats */
69 length _types_ok 8;
70 _types_ok=0;
71 if _type in ('SAS','CSV','EXCEL','MARKDOWN','PDF','WEBCSV','WEBTAB')
72 then _types_ok=1;
73 else putlog 'ERR' 'OR: Invalid type:' _type;
74 if is_libds=0 then putlog 'ERR' 'OR: Invalid table:' _table;
75 if is_int=0 then putlog 'ERR' 'OR: Invalid filter:' _filter;
76 call symputx('is_libds',is_libds,'l');
77 call symputx('is_int',is_int,'l');
78 call symputx('is_type',_types_ok,'l');
79 call symputx('filter',_filter,'l');
80 if is_libds=1 then call symputx('table',upcase(_table),'l');
81 if _types_ok=1 then call symputx('type',_type,'l');
82run;
83
84%mp_abort(iftrue= (&is_libds ne 1)
85 ,mac=&_program..sas
86 ,msg=%str(Invalid table)
87)
88%mp_abort(iftrue= (&is_int ne 1)
89 ,mac=&_program..sas
90 ,msg=%str(Invalid filter)
91)
92%mp_abort(iftrue= (&is_type ne 1)
93 ,mac=&_program..sas
94 ,msg=%str(Invalid type)
95)
96
97%let libds=%upcase(&table); /* actual source */
98%let table=%upcase(&table); /* used as label for fmt catalogs */
99%let lib=%scan(&table,1,.);
100%let ds=%scan(&table,2,.);
101%dc_assignlib(READ,&lib)
102
103data _null_;
104 set &mpelib..MPE_TABLES;
105 where upcase(libref)="&lib" and upcase(dsn)="&ds";
106
107 /* if a TXTEMPORAL table then filter as such */
108 call symputx('txfrom',var_txfrom);
109 call symputx('txto',var_txto);
110run;
111
112/* if a format, extract relevant info */
113data _null_;
114 ds=symget('ds');
115 is_fmt=0;
116 if subpad(cats(reverse(ds)),1,3)=:'CF-' then do;
117 ds=scan(ds,1,'-');
118 table=cats("&lib..",ds);
119 putlog "Format Catalog Captured";
120 is_fmt=1;
121 call symputx('libds','work.fmtextract');
122 call symputx('table',table);
123 end;
124 call symputx('is_fmt',is_fmt);
125 putlog (_all_)(=);
126run;
127
128%mp_cntlout(
129 iftrue=(&is_fmt=1)
130 ,libcat=&table
131 ,fmtlist=0
132 ,cntlout=work.fmtextract
133)
134
135%put preparing query;
136%mpe_filtermaster(DLOAD,&libds,
137 dclib=&mpelib,
138 filter_rk=&filter,
139 outref=filtref,
140 outds=work.query
141)
142%put printing generated filterquery:;
143data _null_;
144 infile filtref;
145 input;
146 putlog _infile_;
147run;
148
149/* derived variables exist only in a view, so a filter that references one
150 must be applied against it. They are dropped from the download - they are
151 an EDIT screen feature. */
152%mpe_derived(libds=&libds,
153 dclib=&mpelib,
154 outref=dercode,
155 outview=work.vw_derived,
156 outvars=work.derived_vars
157)
158%let derived_nobs=%mf_nobs(work.derived_vars);
159%let derived_droplist=;
160%let filterds=%sysfunc(ifc(&derived_nobs>0,work.vw_derived,&libds));
161/* unconditionally: with no derived variables the select returns no rows and the
162 macro variable keeps the blank value set above */
163proc sql noprint;
164select name into: derived_droplist separated by ' '
165 from work.derived_vars;
166quit;
167
168options obs=200000;/* stop limit */
169/* a macro rather than open code so the drop can be conditional */
170%macro build_staged();
171data staged(drop=&txfrom &txto);
172 set &filterds;
173 where %inc filtref;;
174 %if %length(%trim(&derived_droplist))>0 %then %do;
175 drop &derived_droplist;
176 %end;
177run;
178%mend build_staged;
179%build_staged()
180options obs=max;
181
182options validvarname=upcase;
183
184%macro mpestp_getrawdata();
185 %local outfile pdf_maxobs pdf_foot pdf_nobs;
186 %if &type=SAS %then %do;
187 %let outfile=%sysfunc(pathname(work))/&table..sas;
188 %mp_ds2cards(base_ds=staged
189 , tgt_ds=&table
190 , cards_file= "&outfile"
191 , maxobs=100000)
192 %let ext=sas;
193 %let mimetype=text;
194 %end;
195 %else %if &type=CSV or (&type=EXCEL and %mf_existfeature(EXPORTXLS) ne 1)
196 /* cannot proc export excel if PC Files is not licensed */
197 %then %do;
198 %let outfile=%sysfunc(pathname(work))/&table..csv;
199 /* cannot use PROC EXPORT as we need to wrap all char values in quotes */
200 %mp_ds2csv(work.staged,outfile="&outfile",headerformat=NAME)
201 %let ext=csv;
202 %let mimetype=csv;
203 %end;
204 %else %if &type=EXCEL %then %do;
205 %let ext=xlsx;
206 %let outfile=%sysfunc(pathname(work))/&table..&ext;
207 PROC EXPORT DATA= staged
208 OUTFILE= "&outfile"
209 DBMS=xlsx ;
210 RUN;
211 %let mimetype=XLSX;
212 %end;
213 %else %if &type=MARKDOWN %then %do;
214 %let ext=md;
215 %let outfile=%sysfunc(pathname(work))/&table..&ext;
216 filename mdref "&outfile" lrecl=32767;
217
218 %mp_ds2md(staged,outref=mdref,showlog=NO)
219
220 %let mimetype=MARKDOWN;
221 %end;
222 %else %if &type=PDF %then %do;
223 %let ext=pdf;
224 %let outfile=%sysfunc(pathname(work))/&table..&ext;
225 filename pdfref "&outfile";
226 /* the typeset report is bounded by pages rather than by rows: at about
227 27 observations per landscape page, a 2000 row cap is a ~75 page
228 document, which is as much as a synchronous download should stream.
229 The same value drives maxobs= and the truncation text, so the two
230 cannot disagree. */
231 %let pdf_maxobs=2000;
232 /* the footer carries the provenance: the filter that was applied, who
233 ran the report and when, plus the truncation notice when the cap bit.
234 user is the session identity set from mf_getuser() at the top of this
235 service, not an input - a caller cannot assert who ran the report. */
236 %let pdf_foot=;
237 %if &filter eq 0 %then %let pdf_foot=No filter;
238 %else %let pdf_foot=Filter RK &filter;
239 %let pdf_foot=&pdf_foot | &user | &sysdate9 &systime;
240 %let pdf_nobs=%mf_nobs(staged);
241 /* a blank count would reach %eval as nothing to evaluate and fail the
242 download, so it is treated as no truncation */
243 %if %length(%superq(pdf_nobs)) eq 0 %then %let pdf_nobs=0;
244 %if %eval(&pdf_nobs) > %eval(&pdf_maxobs) %then %do;
245 %let pdf_foot=&pdf_foot | first &pdf_maxobs of &pdf_nobs obs;
246 %end;
247 %mp_ds2pdf(staged, outref=pdfref
248 ,title=&table
249 ,footnote=&pdf_foot
250 ,maxobs=&pdf_maxobs
251 ,orientation=LANDSCAPE
252 ,pagenum=PAGEOF
253 )
254 %let mimetype=PDF;
255 %end;
256 %else %if &type=WEBCSV %then %do;
257 PROC EXPORT DATA= staged
258 OUTFILE= _webout
259 DBMS=csv REPLACE;
260 RUN;
261 /* don't set headers */
262 %return;
263 %end;
264 %else %if &type=WEBTAB %then %do;
265 PROC EXPORT DATA= staged
266 OUTFILE= _webout
267 DBMS=tab REPLACE;
268 RUN;
269 /* don't set headers */
270 %return;
271 %end;
272 %else %do;
273 %mp_abort(msg=type &type not supported,mac=mpestp_getrawdata.sas);
274 %end;
275
276 %mp_abort(iftrue= (&syscc ne 0)
277 ,mac=&_program..sas
278 ,msg=%str(syscc=&syscc)
279 )
280
281 %mp_streamfile(contenttype=&mimetype
282 ,inloc=%str(&outfile)
283 ,outname=&table..&ext
284 )
285
286%mend mpestp_getrawdata;
287%mpestp_getrawdata()
288
289%mpeterm()