Macros for SAS Application Developers
https://github.com/sasjs/core
mp_replace.sas File Reference

Performs a text substitution on a file. More...

Go to the source code of this file.

Detailed Description

Performs a find and replace on a file, either in place or to a new file. Can be used on files where lines are longer than 32767.

Works by reading in the file byte by byte, then marking the beginning and end of each matched string, before finally doing the replace.

Full credit for this highly efficient and syntactically satisfying SAS logic goes to Bartosz Jabłoński, founder of the SAS Packages framework.

Usage:

%let file="%sysfunc(pathname(work))/file.txt";
%let str=replace/me;
%let rep=with/this;
data _null_;
  file &file;
  put 'blahblah';
  put "blahblah&str.blah";
  put 'blahblahblah';
run;
%mp_replace(&file, findvar=str, replacevar=rep)
data _null_;
  infile &file;
  input;
  list;
run;

Note - if you are running a version of SAS that will allow the io package in LUA, you can also use this macro: mp_gsubfile.sas

Parameters
[in]infileThe QUOTED path to the file on which to perform the substitution
[in]findvar=Macro variable NAME containing the string to search for
[in]replacevar=Macro variable NAME containing the replacement string
[out]outfile=(0) Optional QUOTED path to the adjusted output file (to avoid overwriting the first file).

SAS Macros

Related Macros

Version
9.4
Author
Bartosz Jabłoński
Allan Bowe

Definition in file mp_replace.sas.