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_verifymacvars.sas
15 @li mf_getuser.sas
16 @li mf_existfeature.sas
17 @li dc_assignlib.sas
18 @li mp_ds2cards.sas
19 @li mp_abort.sas
20 @li mp_binarycopy.sas
21 @li mp_cntlout.sas
22 @li mp_streamfile.sas
23 @li mpe_filtermaster.sas
24
25
26 @version 9.2
27 @author 4GL Apps Ltd
28 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
29 and may not be re-distributed or re-sold without the express permission of
30 4GL Apps Ltd.
31
32**/
33%global table type filter ds format is_fmt txfrom txto;
34%mpeinit()
35
36%let user=%mf_getuser();
37%let is_fmt=0;
38
39%mp_abort(iftrue= (%mf_verifymacvars(type table)=0)
40 ,mac=&_program..sas
41 ,msg=%str(Invalid inputs: type table)
42)
43
44%let libds=%upcase(&table); /* actual source */
45%let table=%upcase(&table); /* used as label for fmt catalogs */
46%let lib=%scan(&table,1,.);
47%let ds=%scan(&table,2,.);
48%dc_assignlib(READ,&lib)
49
50data _null_;
51 set &mpelib..MPE_TABLES;
52 where upcase(libref)="&lib" and upcase(dsn)="&ds";
53
54 /* if a TXTEMPORAL table then filter as such */
55 call symputx('txfrom',var_txfrom);
56 call symputx('txto',var_txto);
57run;
58
59/* if a format, extract relevant info */
60data _null_;
61 ds=symget('ds');
62 is_fmt=0;
63 if subpad(cats(reverse(ds)),1,3)=:'CF-' then do;
64 ds=scan(ds,1,'-');
65 table=cats("&lib..",ds);
66 putlog "Format Catalog Captured";
67 is_fmt=1;
68 call symputx('libds','work.fmtextract');
69 call symputx('table',table);
70 end;
71 call symputx('is_fmt',is_fmt);
72 putlog (_all_)(=);
73run;
74
75%mp_cntlout(
76 iftrue=(&is_fmt=1)
77 ,libcat=&table
78 ,fmtlist=0
79 ,cntlout=work.fmtextract
80)
81
82%put preparing query;
83%mpe_filtermaster(DLOAD,&libds,
84 dclib=&mpelib,
85 filter_rk=&filter,
86 outref=filtref,
87 outds=work.query
88)
89%put printing generated filterquery:;
90data _null_;
91 infile filtref;
92 input;
93 putlog _infile_;
94run;
95
96options obs=200000;/* stop limit */
97data staged(drop=&txfrom &txto);
98 set &libds;
99 where %inc filtref;;
100run;
101options obs=max;
102
103options validvarname=upcase;
104
105%macro mpestp_getrawdata();
106 %local outfile;
107 %if &type=SAS %then %do;
108 %let outfile=%sysfunc(pathname(work))/&table..sas;
109 %mp_ds2cards(base_ds=staged
110 , tgt_ds=&table
111 , cards_file= "&outfile"
112 , maxobs=100000)
113 %let ext=sas;
114 %let mimetype=text;
115 %end;
116 %else %if &type=CSV or (&type=EXCEL and %mf_existfeature(EXPORTXLS) ne 1)
117 /* cannot proc export excel if PC Files is not licensed */
118 %then %do;
119 %let outfile=%sysfunc(pathname(work))/&table..csv;
120 PROC EXPORT DATA= staged
121 OUTFILE= "&outfile"
122 DBMS=csv REPLACE;
123 RUN;
124 %let ext=csv;
125 %let mimetype=csv;
126 %end;
127 %else %if &type=EXCEL %then %do;
128 %let ext=xlsx;
129 %let outfile=%sysfunc(pathname(work))/&table..&ext;
130 PROC EXPORT DATA= staged
131 OUTFILE= "&outfile"
132 DBMS=xlsx ;
133 RUN;
134 %let mimetype=XLSX;
135 %end;
136 %else %if &type=MARKDOWN %then %do;
137 %let ext=md;
138 %let outfile=%sysfunc(pathname(work))/&table..&ext;
139 filename mdref "&outfile" lrecl=32767;
140
141 %mp_ds2md(staged,outref=mdref,showlog=NO)
142
143 %let mimetype=MARKDOWN;
144 %end;
145 %else %if &type=WEBCSV %then %do;
146 PROC EXPORT DATA= staged
147 OUTFILE= _webout
148 DBMS=csv REPLACE;
149 RUN;
150 /* don't set headers */
151 %return;
152 %end;
153 %else %if &type=WEBTAB %then %do;
154 PROC EXPORT DATA= staged
155 OUTFILE= _webout
156 DBMS=tab REPLACE;
157 RUN;
158 /* don't set headers */
159 %return;
160 %end;
161 %else %do;
162 %mp_abort(msg=type &type not supported,mac=mpestp_getrawdata.sas);
163 %end;
164
165 %mp_abort(iftrue= (&syscc ne 0)
166 ,mac=&_program..sas
167 ,msg=%str(syscc=&syscc)
168 )
169
170 %mp_streamfile(contenttype=&mimetype
171 ,inloc=%str(&outfile)
172 ,outname=&table..&ext
173 )
174
175%mend mpestp_getrawdata;
176%mpestp_getrawdata()
177
178%mpeterm()