Macros for SAS Application Developers
https://github.com/sasjs/core
mm_updatestpsourcecode.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Update the source code of a type 2 STP
4 @details Uploads the contents of a text file or fileref to an existing type 2
5 STP. A type 2 STP has its source code saved in metadata.
6
7 Usage:
8
9 %mm_updatestpsourcecode(stp=/my/metadata/path/mystpname
10 ,stpcode="/file/system/source.sas")
11
12 @param [in] stp= the BIP Tree folder path plus Stored Process Name
13 @param [in] stpcode= the source file (or fileref) containing the SAS code to load
14 into the stp. For multiple files, they should simply be concatenated first.
15 @param [in] minify= set to YES in order to strip comments, blank lines, and CRLFs.
16 @param mDebug= set to 1 to show debug messages in the log
17
18 @version 9.3
19 @author Allan Bowe
20
21 <h4> SAS Macros </h4>
22 @li mf_getuniquefileref.sas
23
24**/
25
26%macro mm_updatestpsourcecode(stp=
27 ,stpcode=
28 ,minify=NO
29 ,mdebug=0
30);
31
32/* first, check if STP exists */
33%local tsuri;
34%let tsuri=stopifempty ;
35
36data _null_;
37 format type uri tsuri value $200.;
38 call missing (of _all_);
39 path="&stp.(StoredProcess)";
40 /* first, find the STP ID */
41 if metadata_pathobj("",path,"StoredProcess",type,uri)>0 then do;
42 /* get sourcecode */
43 cnt=1;
44 do while (metadata_getnasn(uri,"Notes",cnt,tsuri)>0);
45 rc=metadata_getattr(tsuri,"Name",value);
46 %if &mdebug=1 %then %do;
47 put tsuri= value=;
48 %end;
49 if value="SourceCode" then do;
50 /* found it! */
51 rc=metadata_getattr(tsuri,"Id",value);
52 call symputx('tsuri',value,'l');
53 stop;
54 end;
55 cnt+1;
56 end;
57 end;
58 else put (_all_)(=);
59run;
60
61%if &tsuri=stopifempty %then %do;
62 %put %str(WARN)ING: &stp.(StoredProcess) not found!;
63 %return;
64%end;
65
66%if %length(&stpcode)<2 %then %do;
67 %put %str(WARN)ING: No SAS code supplied!!;
68 %return;
69%end;
70
71%local frefin frefout;
72%let frefin=%mf_getuniquefileref();
73%let frefout=%mf_getuniquefileref();
74
75/* write header XML */
76data _null_;
77 file &frefin;
78 put "<UpdateMetadata><Reposid>$METAREPOSITORY</Reposid>
79 <Metadata><TextStore id='&tsuri' StoredText='";
80run;
81
82/* escape code so it can be stored as XML */
83/* write contents */
84%if %length(&stpcode)>2 %then %do;
85 data _null_;
86 file &frefin lrecl=32767 mod;
87 infile &stpcode lrecl=32767;
88 length outstr $32767;
89 input outstr ;
90 /* escape code so it can be stored as XML */
91 outstr=tranwrd(_infile_,'&','&amp;');
92 outstr=tranwrd(outstr,'<','&lt;');
93 outstr=tranwrd(outstr,'>','&gt;');
94 outstr=tranwrd(outstr,"'",'&apos;');
95 outstr=tranwrd(outstr,'"','&quot;');
96 outstr=tranwrd(outstr,'0A'x,'&#x0a;');
97 outstr=tranwrd(outstr,'0D'x,'&#x0d;');
98 outstr=tranwrd(outstr,'$','&#36;');
99 %if &minify=YES %then %do;
100 outstr=cats(outstr);
101 if outstr ne '';
102 if not (outstr=:'/*' and subpad(left(reverse(outstr)),1,2)='/*');
103 %end;
104 outstr=trim(outstr);
105 put outstr '&#10;';
106 run;
107%end;
108
109data _null_;
110 file &frefin mod;
111 put "'></TextStore></Metadata><NS>SAS</NS><Flags>268435456</Flags>
112 </UpdateMetadata>";
113run;
114
115proc metadata in= &frefin out=&frefout;
116run;
117
118%if &mdebug=1 %then %do;
119 /* write the response to the log for debugging */
120 data _null_;
121 infile &frefout lrecl=32767;
122 input;
123 put _infile_;
124 run;
125%end;
126%else %do;
127 filename &frefin clear;
128 filename &frefout clear;
129%end;
130
131%mend mm_updatestpsourcecode;