Loading...
Searching...
No Matches
mx_append2pgm.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Appends a text file to a SASjs Stored Program, Viya SAS program, or
4 SAS 9 Stored Process
5 @details Extracts the source code from a SASjs Stored Program, Viya SAS
6 program (file in SAS Drive), or SAS 9 Stored Process, appends the contents
7 of a provided text file, then deletes and recreates the target item with the
8 combined content.
9
10 This is useful for dynamically modifying deployed programs, for example to
11 add test-specific configuration or runtime settings.
12
13 Usage:
14
15 %* compile macros ;
16 filename mc url
17 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
18 %inc mc;
19
20 %* write some content to append;
21 filename append temp;
22 data _null_;
23 file append;
24 put "libname mylib '/some/path';";
25 run;
26
27 %* append to existing program;
28 %mx_append2pgm(/Public/app/common/settings, inref=append)
29
30 @param [in] loc The full path to the Viya SAS program, SAS 9 Stored Process,
31 or SASjs Stored Program in Drive or Metadata, WITHOUT the .sas extension
32 (SASjs only)
33 @param [in] inref= (0) Fileref pointing to the content to be appended to the
34 target program.
35 @param [in] mdebug= (0) Set to 1 to show debug messages in the log
36
37 <h4> SAS Macros </h4>
38 @li mf_getplatform.sas
39 @li mf_getuniquefileref.sas
40 @li mm_createstp.sas
41 @li mm_deletestp.sas
42 @li ms_createfile.sas
43 @li ms_deletefile.sas
44 @li mv_createfile.sas
45 @li mv_deletefoldermember.sas
46 @li mx_getcode.sas
47
48 <h4> Related Macros </h4>
49 @li mx_append2pgm.test.sas
50 @li mx_getcode.sas
51 @li mx_createjob.sas
52
53 @author Allan Bowe
54
55**/
56
57%macro mx_append2pgm(loc
58 ,inref=0
59 ,mdebug=0
60)/*/STORE SOURCE*/;
61
62%local platform name shortloc coderef combref work tmpfile viyaref;
63%let platform=%mf_getplatform();
64
65%if &mdebug=1 %then %do;
66 %put &sysmacroname entry vars:;
67 %put _local_;
68%end;
69%if &syscc ne 0 %then %do;
70 %put syscc=&syscc - &sysmacroname will not execute in this state;
71 %return;
72%end;
73
74/* extract name & path from loc */
75data _null_;
76 length name shortloc $500;
77 loc=symget('loc');
78 name=scan(loc,-1,'/');
79 shortloc=substr(loc,1,length(loc)-length(name)-1);
80 call symputx('name',name,'l');
81 call symputx('shortloc',shortloc,'l');
82run;
83
84/* create a combined fileref with original + appended content */
85%let combref=%mf_getuniquefileref();
86%let work=%sysfunc(pathname(work));
87%let tmpfile=&combref..sas;
88filename &combref "&work/&tmpfile" lrecl=32000;
89
90%if &platform=SASVIYA %then %do;
91 /* On Viya, read the SAS program file from SAS Drive using filesrvc */
92 %let viyaref=%mf_getuniquefileref();
93 filename &viyaref filesrvc folderpath="&shortloc";
94 data _null_;
95 file &combref lrecl=32000 termstr=crlf;
96 infile &viyaref("&name..sas") lrecl=32000 end=last;
97 input;
98 put _infile_;
99 run;
100 filename &viyaref clear;
101 %symdel _FILESRVC_&viyaref._URI;
102%end;
103%else %do;
104 /* For SAS9 and SASJS, use mx_getcode */
105 %let coderef=%mf_getuniquefileref();
106 %mx_getcode(&loc, outref=&coderef)
107 data _null_;
108 file &combref lrecl=32000 termstr=crlf;
109 infile &coderef lrecl=32000 end=last;
110 input;
111 put _infile_;
112 run;
113 filename &coderef clear;
114%end;
115
116/* append the new content */
117data _null_;
118 file &combref lrecl=32000 termstr=crlf mod;
119 infile &inref lrecl=32000;
120 input;
121 put _infile_;
122run;
123
124/* delete and recreate the target item */
125%if &platform=SASJS %then %do;
126 %ms_deletefile(&loc..sas)
127 %ms_createfile(&loc..sas, inref=&combref, mdebug=&mdebug)
128%end;
129%else %if &platform=SASVIYA %then %do;
130 %mv_deletefoldermember(path=&shortloc, name=&name..sas, contenttype=file)
131 %mv_createfile(path=&shortloc, name=&name..sas, inref=&combref)
132%end;
133%else %do;
134 /* SAS 9 */
135 %mm_deletestp(target=&loc)
136 %mm_createstp(stpname=&name
137 ,filename=&tmpfile
138 ,directory=&work
139 ,tree=&shortloc
140 ,stptype=2
141 ,mDebug=&mdebug
142 ,minify=NO
143 )
144%end;
145
146filename &combref clear;
147
148%mend mx_append2pgm;