Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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/* a narrow LRECL truncates the wide lines of the library as they
14 are written, so widen it for the write as well as the include */
15%local optval;
16%let optval=%sysfunc(getoption(lrecl));
17options lrecl=1024;
18
19data _null_;
20 file "%sysfunc(pathname(work))/ml_gsubfile.lua";
21 put 'local fpath, outpath, file, fcontent ';
22 put ' ';
23 put '-- configure in / out paths ';
24 put 'fpath = sas.symget("file") ';
25 put 'outpath = sas.symget("outfile") ';
26 put 'if ( outpath == 0 ) ';
27 put 'then ';
28 put ' outpath=fpath ';
29 put 'end ';
30 put ' ';
31 put '-- open file and perform the substitution ';
32 put 'file = io.open(fpath,"r") ';
33 put 'fcontent = file:read("*all") ';
34 put 'file:close() ';
35 put 'fcontent = string.gsub( ';
36 put ' fcontent, ';
37 put ' sas.symget(sas.symget("patternvar")), ';
38 put ' sas.symget(sas.symget("replacevar")) ';
39 put ') ';
40 put ' ';
41 put '-- write the file back out ';
42 put 'file = io.open(outpath, "w+") ';
43 put 'io.output(file) ';
44 put 'io.write(fcontent) ';
45 put 'io.close(file) ';
46run;
47
48/* execute the lua code by using a .lua extension */
49%inc "%sysfunc(pathname(work))/ml_gsubfile.lua" /source2;
50
51options lrecl=&optval;
52
53%mend ml_gsubfile;