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