Production Ready Macros for SAS Application Developers
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 
13  @param stp= the BIP Tree folder path plus Stored Process Name
14  @param stpcode= the source file (or fileref) containing the SAS code to load
15  into the stp. For multiple files, they should simply be concatenated first.
16  @param minify= set to YES in order to strip comments, blank lines, and CRLFs.
17 
18  @param frefin= change default inref if it clashes with an existing one
19  @param frefout= change default outref if it clashes with an existing one
20  @param mDebug= set to 1 to show debug messages in the log
21 
22  @version 9.3
23  @author Allan Bowe
24 
25 **/
26 
27 %macro mm_updatestpsourcecode(stp=
28  ,stpcode=
29  ,minify=NO
30  ,frefin=inmeta
31  ,frefout=outmeta
32  ,mdebug=0
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  put tsuri= value=;
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_)(=);
59 run;
60 
61 %if &tsuri=stopifempty %then %do;
62  %put WARNING: &stp.(StoredProcess) not found!;
63  %return;
64 %end;
65 
66 %if %length(&stpcode)<2 %then %do;
67  %put WARNING: No SAS code supplied!!;
68  %return;
69 %end;
70 
71 filename &frefin temp lrecl=32767;
72 
73 /* write header XML */
74 data _null_;
75  file &frefin;
76  put "<UpdateMetadata><Reposid>$METAREPOSITORY</Reposid>
77  <Metadata><TextStore id='&tsuri' StoredText='";
78 run;
79 
80 /* escape code so it can be stored as XML */
81 /* write contents */
82 %if %length(&stpcode)>2 %then %do;
83  data _null_;
84  file &frefin mod;
85  infile &stpcode lrecl=32767;
86  length outstr $32767;
87  input outstr ;
88  /* escape code so it can be stored as XML */
89  outstr=tranwrd(_infile_,'&','&amp;');
90  outstr=tranwrd(outstr,'<','&lt;');
91  outstr=tranwrd(outstr,'>','&gt;');
92  outstr=tranwrd(outstr,"'",'&apos;');
93  outstr=tranwrd(outstr,'"','&quot;');
94  outstr=tranwrd(outstr,'0A'x,'&#x0a;');
95  outstr=tranwrd(outstr,'0D'x,'&#x0d;');
96  outstr=tranwrd(outstr,'$','&#36;');
97  %if &minify=YES %then %do;
98  outstr=cats(outstr);
99  if outstr ne '';
100  if not (outstr=:'/*' and subpad(left(reverse(outstr)),1,2)='/*');
101  %end;
102  outstr=trim(outstr);
103  put outstr '&#10;';
104  run;
105 %end;
106 
107 data _null_;
108  file &frefin mod;
109  put "'></TextStore></Metadata><NS>SAS</NS><Flags>268435456</Flags>
110  </UpdateMetadata>";
111 run;
112 
113 
114 filename &frefout temp;
115 
116 proc metadata in= &frefin out=&frefout verbose;
117 run;
118 
119 %if &mdebug=1 %then %do;
120  /* write the response to the log for debugging */
121  data _null_;
122  infile &frefout lrecl=32767;
123  input;
124  put _infile_;
125  run;
126 %end;
127 
128 %mend;