SAS Utility: Replace the word(s) in SAS program(s)

Posted: November 10, 2013 in SAS Tips & Tricks

Scenario:
In your programs directory(folder) there are hundreds of SAS programs and you need to replace a specific word or sentence in majority of those programs.
Example of directory path is: ../project/production/programs
Sentence you like to replace: He made it –> She resolved that

Method-1 (Manual): We will open each and every program and replace the word/sentence manually.

Solution:
Method-2 (automation): Below SAS macro program with the help of perl will take care of this task.

Step1: You need to prepare a .txt file with the list of sas programs, in which you would like to replace the word/sentence. Save this txt file in the same directory(folder) where all of your programs located.
The txt file should look like this:
files

Step2: Run this macro program to complete your task.

options nofmterr mlogic symbolgen mprint;

%macro tot_ttl_chg;

filename in1 “../project/production/programs/files.txt”;
data one;
infile in1 missover pad;
length filename $50;
input filename;
run;

proc sql;
select count(distinct filename)
into : numfiles
from one
where filename ne ‘ ‘;
quit;

%do i=1 %to &numfiles;

data _null_;
set one(firstobs=&i obs=&i);
call symput(“totname”,trim(left(filename)));
run;

x perl -pi -e ‘s|He made it|She resolved that|g’ &totname;

%end;

%mend tot_ttl_chg;
%tot_ttl_chg;

Leave a comment