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, 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_binarycopy.sas
19 @li mp_cntlout.sas
20 @li mp_ds2cards.sas
21 @li mp_ds2csv.sas
22 @li mp_streamfile.sas
23 @li mp_validatecol.sas
24 @li mpe_validatecol.sas
25 @li mpe_filtermaster.sas
26
27
28 @version 9.2
29 @author 4GL Apps Ltd
30 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
31 and may not be re-distributed or re-sold without the express permission of
32 4GL Apps Ltd.
33
34**/
35%global table type filter ds format is_fmt txfrom txto;
36%mpeinit()
37
38%let user=%mf_getuser();
39%let is_fmt=0;
40
41/**
42 * Validate inputs before they reach executable code. table is used as a
43 * dataset reference, in the output file path, and in the download filename,
44 * so it must be a well-formed LIBREF.DATASET (the trailing -FC catalog
45 * suffix is permitted); filter must be an integer. Values are read with
46 * symget (never re-resolved) and validated in a data step so no macro
47 * content in the input can execute.
48 */
49%let is_libds=0;
50%let is_int=0;
51%let is_type=0;
52data _null_;
53 length _table $64 _filter $16 _type $16;
54 _type=upcase(coalescec(symget('type'),''));
55 _table=coalescec(symget('table'),'');
56 _filter=coalescec(symget('filter'),'0');
57 if missing(_table) then do;
58 putlog 'ERR' 'OR: Missing table';
59 stop;
60 end;
61 %mpe_validatecol(_table,LIBDS,is_libds)
62 /* an absent filter is a valid, unfiltered download */
63 if missing(_filter) then _filter='0';
64 %mp_validatecol(_filter,ISINT,is_int)
65 /* type is validated against a fixed list of download formats */
66 length _types_ok 8;
67 _types_ok=0;
68 if _type in ('SAS','CSV','EXCEL','MARKDOWN','WEBCSV','WEBTAB')
69 then _types_ok=1;
70 else putlog 'ERR' 'OR: Invalid type:' _type;
71 if is_libds=0 then putlog 'ERR' 'OR: Invalid table:' _table;
72 if is_int=0 then putlog 'ERR' 'OR: Invalid filter:' _filter;
73 call symputx('is_libds',is_libds,'l');
74 call symputx('is_int',is_int,'l');
75 call symputx('is_type',_types_ok,'l');
76 call symputx('filter',_filter,'l');
77 if is_libds=1 then call symputx('table',upcase(_table),'l');
78 if _types_ok=1 then call symputx('type',_type,'l');
79run;
80
81%mp_abort(iftrue= (&is_libds ne 1)
82 ,mac=&_program..sas
83 ,msg=%str(Invalid table)
84)
85%mp_abort(iftrue= (&is_int ne 1)
86 ,mac=&_program..sas
87 ,msg=%str(Invalid filter)
88)
89%mp_abort(iftrue= (&is_type ne 1)
90 ,mac=&_program..sas
91 ,msg=%str(Invalid type)
92)
93
94%let libds=%upcase(&table); /* actual source */
95%let table=%upcase(&table); /* used as label for fmt catalogs */
96%let lib=%scan(&table,1,.);
97%let ds=%scan(&table,2,.);
98%dc_assignlib(READ,&lib)
99
100data _null_;
101 set &mpelib..MPE_TABLES;
102 where upcase(libref)="&lib" and upcase(dsn)="&ds";
103
104 /* if a TXTEMPORAL table then filter as such */
105 call symputx('txfrom',var_txfrom);
106 call symputx('txto',var_txto);
107run;
108
109/* if a format, extract relevant info */
110data _null_;
111 ds=symget('ds');
112 is_fmt=0;
113 if subpad(cats(reverse(ds)),1,3)=:'CF-' then do;
114 ds=scan(ds,1,'-');
115 table=cats("&lib..",ds);
116 putlog "Format Catalog Captured";
117 is_fmt=1;
118 call symputx('libds','work.fmtextract');
119 call symputx('table',table);
120 end;
121 call symputx('is_fmt',is_fmt);
122 putlog (_all_)(=);
123run;
124
125%mp_cntlout(
126 iftrue=(&is_fmt=1)
127 ,libcat=&table
128 ,fmtlist=0
129 ,cntlout=work.fmtextract
130)
131
132%put preparing query;
133%mpe_filtermaster(DLOAD,&libds,
134 dclib=&mpelib,
135 filter_rk=&filter,
136 outref=filtref,
137 outds=work.query
138)
139%put printing generated filterquery:;
140data _null_;
141 infile filtref;
142 input;
143 putlog _infile_;
144run;
145
146options obs=200000;/* stop limit */
147data staged(drop=&txfrom &txto);
148 set &libds;
149 where %inc filtref;;
150run;
151options obs=max;
152
153options validvarname=upcase;
154
155%macro mpestp_getrawdata();
156 %local outfile;
157 %if &type=SAS %then %do;
158 %let outfile=%sysfunc(pathname(work))/&table..sas;
159 %mp_ds2cards(base_ds=staged
160 , tgt_ds=&table
161 , cards_file= "&outfile"
162 , maxobs=100000)
163 %let ext=sas;
164 %let mimetype=text;
165 %end;
166 %else %if &type=CSV or (&type=EXCEL and %mf_existfeature(EXPORTXLS) ne 1)
167 /* cannot proc export excel if PC Files is not licensed */
168 %then %do;
169 %let outfile=%sysfunc(pathname(work))/&table..csv;
170 /* cannot use PROC EXPORT as we need to wrap all char values in quotes */
171 %mp_ds2csv(work.staged,outfile="&outfile",headerformat=NAME)
172 %let ext=csv;
173 %let mimetype=csv;
174 %end;
175 %else %if &type=EXCEL %then %do;
176 %let ext=xlsx;
177 %let outfile=%sysfunc(pathname(work))/&table..&ext;
178 PROC EXPORT DATA= staged
179 OUTFILE= "&outfile"
180 DBMS=xlsx ;
181 RUN;
182 %let mimetype=XLSX;
183 %end;
184 %else %if &type=MARKDOWN %then %do;
185 %let ext=md;
186 %let outfile=%sysfunc(pathname(work))/&table..&ext;
187 filename mdref "&outfile" lrecl=32767;
188
189 %mp_ds2md(staged,outref=mdref,showlog=NO)
190
191 %let mimetype=MARKDOWN;
192 %end;
193 %else %if &type=WEBCSV %then %do;
194 PROC EXPORT DATA= staged
195 OUTFILE= _webout
196 DBMS=csv REPLACE;
197 RUN;
198 /* don't set headers */
199 %return;
200 %end;
201 %else %if &type=WEBTAB %then %do;
202 PROC EXPORT DATA= staged
203 OUTFILE= _webout
204 DBMS=tab REPLACE;
205 RUN;
206 /* don't set headers */
207 %return;
208 %end;
209 %else %do;
210 %mp_abort(msg=type &type not supported,mac=mpestp_getrawdata.sas);
211 %end;
212
213 %mp_abort(iftrue= (&syscc ne 0)
214 ,mac=&_program..sas
215 ,msg=%str(syscc=&syscc)
216 )
217
218 %mp_streamfile(contenttype=&mimetype
219 ,inloc=%str(&outfile)
220 ,outname=&table..&ext
221 )
222
223%mend mpestp_getrawdata;
224%mpestp_getrawdata()
225
226%mpeterm()