Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_streamfile.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Streams a file to _webout according to content type
4 @details Will set headers using appropriate functions (SAS 9 vs Viya) and send
5 content as a binary stream.
6
7 Usage:
8
9 filename mc url
10 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
11 %inc mc;
12
13 %mp_streamfile(contenttype=csv,inloc=/some/where.txt,outname=myfile.txt)
14
15 <h4> SAS Macros </h4>
16 @li mf_getplatform.sas
17 @li mp_binarycopy.sas
18
19 @param contenttype= Either TEXT, ZIP, CSV, EXCEL (default TEXT)
20 @param inloc= /path/to/file.ext to be sent
21 @param inref= fileref of file to be sent (if provided, overrides `inloc`)
22 @param outname= the name of the file, as downloaded by the browser
23
24 @author Allan Bowe
25 @source https://github.com/sasjs/core
26
27**/
28
29%macro mp_streamfile(
30 contenttype=TEXT
31 ,inloc=
32 ,inref=0
33 ,outname=
34)/*/STORE SOURCE*/;
35
36%let contentype=%upcase(&contenttype);
37%local platform; %let platform=%mf_getplatform();
38
39
40/**
41 * check engine type to avoid the below err message:
42 * > Function is only valid for filerefs using the CACHE access method.
43 */
44%local streamweb;
45%let streamweb=0;
46data _null_;
47 set sashelp.vextfl(where=(upcase(fileref)="_WEBOUT"));
48 if xengine='STREAM' then call symputx('streamweb',1,'l');
49run;
50
51%if &contentype=ZIP %then %do;
52 %if &platform=SASMETA and &streamweb=1 %then %do;
53 data _null_;
54 rc=stpsrv_header('Content-type','application/zip');
55 rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
56 run;
57 %end;
58 %else %if &platform=SASVIYA %then %do;
59 filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip'
60 contenttype='application/zip'
61 contentdisp="attachment; filename=&outname";
62 %end;
63%end;
64%else %if &contentype=EXCEL %then %do;
65 /* suitable for XLS format */
66 %if &platform=SASMETA and &streamweb=1 %then %do;
67 data _null_;
68 rc=stpsrv_header('Content-type','application/vnd.ms-excel');
69 rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
70 run;
71 %end;
72 %else %if &platform=SASVIYA %then %do;
73 filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
74 contenttype='application/vnd.ms-excel'
75 contentdisp="attachment; filename=&outname";
76 %end;
77%end;
78%else %if &contentype=XLSX %then %do;
79 %if &platform=SASMETA and &streamweb=1 %then %do;
80 data _null_;
81 rc=stpsrv_header('Content-type',
82 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
83 rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
84 run;
85 %end;
86 %else %if &platform=SASVIYA %then %do;
87 filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
88 contenttype=
89 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
90 contentdisp="attachment; filename=&outname";
91 %end;
92%end;
93%else %if &contentype=TEXT %then %do;
94 %if &platform=SASMETA and &streamweb=1 %then %do;
95 data _null_;
96 rc=stpsrv_header('Content-type','application/text');
97 rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
98 run;
99 %end;
100 %else %if &platform=SASVIYA %then %do;
101 filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
102 contenttype='application/text'
103 contentdisp="attachment; filename=&outname";
104 %end;
105%end;
106%else %if &contentype=CSV %then %do;
107 %if &platform=SASMETA and &streamweb=1 %then %do;
108 data _null_;
109 rc=stpsrv_header('Content-type','application/csv');
110 rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
111 run;
112 %end;
113 %else %if &platform=SASVIYA %then %do;
114 filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
115 contenttype='application/csv'
116 contentdisp="attachment; filename=&outname";
117 %end;
118%end;
119%else %if &contentype=HTML %then %do;
120 %if &platform=SASVIYA %then %do;
121 filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json"
122 contenttype="text/html";
123 %end;
124%end;
125%else %do;
126 %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!;
127 %return;
128%end;
129
130%if &inref ne 0 %then %do;
131 %mp_binarycopy(inref=&inref,outref=_webout)
132%end;
133%else %do;
134 %mp_binarycopy(inloc="&inloc",outref=_webout)
135%end;
136
137%mend mp_streamfile;