Macros for SAS Application Developers
https://github.com/sasjs/core
ml_gsubfile.sas
Go to the documentation of this file.
1 /**
2  @file ml_gsubfile.sas
3  @brief Compiles the gsubfile.lua lua file
4  @details Writes gsubfile.lua to the work directory
5  and then includes it.
6  Usage:
7 
8  %ml_gsubfile()
9 
10 **/
11 
12 %macro ml_gsubfile();
13 data _null_;
14  file "%sysfunc(pathname(work))/ml_gsubfile.lua";
15  put 'local fpath, outpath, file, fcontent ';
16  put ' ';
17  put '-- configure in / out paths ';
18  put 'fpath = sas.symget("file") ';
19  put 'outpath = sas.symget("outfile") ';
20  put 'if ( outpath == 0 ) ';
21  put 'then ';
22  put ' outpath=fpath ';
23  put 'end ';
24  put ' ';
25  put '-- open file and perform the substitution ';
26  put 'file = io.open(fpath,"r") ';
27  put 'fcontent = file:read("*all") ';
28  put 'file:close() ';
29  put 'fcontent = string.gsub( ';
30  put ' fcontent, ';
31  put ' sas.symget(sas.symget("patternvar")), ';
32  put ' sas.symget(sas.symget("replacevar")) ';
33  put ') ';
34  put ' ';
35  put '-- write the file back out ';
36  put 'file = io.open(outpath, "w+") ';
37  put 'io.output(file) ';
38  put 'io.write(fcontent) ';
39  put 'io.close(file) ';
40 run;
41 
42 /* ensure big enough lrecl to avoid lua compilation issues */
43 %local optval;
44 %let optval=%sysfunc(getoption(lrecl));
45 options lrecl=1024;
46 
47 /* execute the lua code by using a .lua extension */
48 %inc "%sysfunc(pathname(work))/ml_gsubfile.lua" /source2;
49 
50 options lrecl=&optval;
51 
52 %mend ml_gsubfile;