Macros for SAS Application Developers
https://github.com/sasjs/core
mp_gsubfile.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Performs a text substitution on a file
4  @details Makes use of the GSUB function in LUA to perform a text substitution
5  in a file - either in-place, or writing to a new location. The benefit of
6  using LUA is that the entire file can be loaded into a single variable,
7  thereby side stepping the 32767 character limit in a data step.
8 
9  Usage:
10 
11  %let file=%sysfunc(pathname(work))/file.txt;
12  %let str=replace/me;
13  %let rep=with/this;
14  data _null_;
15  file "&file";
16  put "&str";
17  run;
18  %mp_gsubfile(file=&file, patternvar=str, replacevar=rep)
19  data _null_;
20  infile "&file";
21  input;
22  list;
23  run;
24 
25  @param [in] file= (0) The file to perform the substitution on
26  @param [in] patternvar= A macro variable containing the Lua
27  [pattern](https://www.lua.org/pil/20.2.html) to search for. Due to the use
28  of special (magic) characters in Lua patterns, it is safer to pass the NAME
29  of the macro variable containing the string, rather than the value itself.
30  @param [in] replacevar= ()
31  The name of the macro variable containing the replacement _string_.
32  @param [out] outfile= (0) The file to write the output to.
33  If zero, then the file is overwritten in-place.
34 
35  <h4> SAS Macros </h4>
36  @li ml_gsubfile.sas
37 
38  <h4> Related Macros </h4>
39  @li mp_gsubfile.test.sas
40 
41  @version 9.4
42  @author Allan Bowe
43 **/
44 
45 %macro mp_gsubfile(file=0,
46  patternvar=,
47  replacevar=,
48  outfile=0
49 )/*/STORE SOURCE*/;
50 
51  %if "%substr(&sysver.XX,1,4)"="V.04" %then %do;
52  %put %str(ERR)OR: Viya 4 does not support the IO library in lua;
53  %return;
54  %end;
55 
56  %ml_gsubfile()
57 
58 %mend mp_gsubfile;