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