Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mcf_stpsrv_header.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Provides a replacement for the stpsrv_header function
4 @details The stpsrv_header is normally a built-in function, used to set the
5 headers for SAS 9 Stored Processes as documented here:
6 https://go.documentation.sas.com/doc/en/itechcdc/9.4/stpug/srvhead.htm
7
8 The purpose of this custom function is to provide a replacement when running
9 similar code as a web service against
10 [sasjs/server](https://github.com/sasjs/server). It operates by creating a
11 text file with the headers. The location of this text file is determined by
12 a macro variable (`sasjs_stpsrv_header_loc`) which needs to be injected into
13 each service by the calling process, eg:
14
15 %let sasjs_stpsrv_header_loc = C:/temp/some_uuid/stpsrv_header.txt;
16
17 Note - the function works by appending headers to the file. If multiple same-
18 named headers are provided, they will all be appended - the calling process
19 needs to pick up the last one. This will mean removing the attribute if the
20 final record has an empty value.
21
22 The function takes the following (positional) parameters:
23
24 | PARAMETER | DESCRIPTION |
25 |------------|-------------|
26 | name $ | name of the header attribute to create|
27 | value $ | value of the header attribute|
28
29 It returns 0 if successful, or -1 if an error occured.
30
31 Usage:
32
33 %let sasjs_stpsrv_header_loc=%sysfunc(pathname(work))/stpsrv_header.txt;
34
35 %mcf_stpsrv_header(wrap=YES, insert_cmplib=YES)
36
37 data _null_;
38 rc=stpsrv_header('Content-type','application/text');
39 rc=stpsrv_header('Content-disposition',"attachment; filename=file.txt");
40 run;
41
42 data _null_;
43 infile "&sasjs_stpsrv_header_loc";
44 input;
45 putlog _infile_;
46 run;
47
48
49 @param [out] wrap= (NO) Choose YES to add the proc fcmp wrapper.
50 @param [out] insert_cmplib= (NO) Choose YES to insert the package into the
51 CMPLIB reference.
52 @param [out] lib= (work) The output library in which to create the catalog.
53 @param [out] cat= (sasjs) The output catalog in which to create the package.
54 @param [out] pkg= (utils) The output package in which to create the function.
55 Uses a 3 part format: libref.catalog.package
56
57**/
58
59%macro mcf_stpsrv_header(wrap=NO
60 ,insert_cmplib=NO
61 ,lib=WORK
62 ,cat=SASJS
63 ,pkg=UTILS
64)/*/STORE SOURCE*/;
65
66%if &wrap=YES %then %do;
67 proc fcmp outcat=&lib..&cat..&pkg;
68%end;
69
70function stpsrv_header(name $, value $);
71 length loc $128 val $512;
72 loc=symget('sasjs_stpsrv_header_loc');
73 val=trim(name)!!': '!!value;
74 length fref $8;
75 rc=filename(fref,loc);
76 if (rc ne 0) then return( -1 );
77 fid = fopen(fref,'a');
78 if (fid = 0) then return( -1 );
79 rc=fput(fid, val);
80 rc=fwrite(fid);
81 rc=fclose(fid);
82 rc=filename(fref);
83 return(0);
84endsub;
85
86%if &wrap=YES %then %do;
87 quit;
88%end;
89
90%if &insert_cmplib=YES %then %do;
91 options insert=(CMPLIB=(&lib..&cat));
92%end;
93
94%mend mcf_stpsrv_header;