Dear all,
I have the following problem:
I use the macro described here:
http://www2.sas.com/proceedings/sugi24/Coders/p086-24.pdf.
Code:
%macro replace (dataset = , chkvar = , search = , replace =,
newvar = );
data &dataset (drop = textpos) newvars;
length &newvar $ 200;
set &dataset;
textpos = index(upcase(&chkvar), upcase("&search"));
oldvar = &chkvar;
if textpos ne 0 then
do;
if textpos ne 1 then
&newvar = substr(&chkvar, 1, (textpos - 1)) ||"&replace"||substr(&chkvar, (textpos+length("&search")));
else &newvar = "&replace"||
substr(&chkvar, (textpos + length("&search")));
output newvars;
end;
else &newvar = &chkvar;
output &dataset;
run;
proc print data = newvars;
title “SEARCH FOR ‘&search’ AND REPLACE WITH ‘&replace’”;
var oldvar &newvar;
run;
%mend replace;
This macro replaces a specified string in a cell. The problem is, that it only replaces the first specified string that appears in a cell, when the specified string occurs more than once, the macro does not replace all specified string appearing in the cell but only the first one. How could the macro be fixed in order to replace the specified string as often as the specified string occurs in the cell?
Thanks a lot for your help.
Valentin