Hello Everyone,
I have multiple sas programs(around 1000). I have a requirement that I have to replace a string with new string , the old string have contents inside " " which will keep changing. for e.g
in first program I have this string
Old String = Name abc "I have three books"; this I have to replace with new string
New String = Name abc "I need coffee" ;
In second program I have this string
Old String = Name sam "I am going to office"; this I have to replace with new string
New String = Name sam "I will be dancing"
so on we have strings like this in different programs.
The basic idea is I have to swap String value after NAME to new string value. pattern will be same
Old String have first value as NAME second is name of person and then after space inside " " value which needs to be replaced.
Kindly suggest any approach or macro?
I would use either powershell or bash-script, depending on the os SAS is installed on. Or an editor like notepad++.
Sorry, but i haven't written any bash-script in the last 12 years, unfortunately i am bound to sas on windows 😞
You appear to have two problems here. First is how to find and replace the string.
Sounds like you should be able to use a regular expression, but that depends a lot on how precisely you can describe the pattern.
Are you talking about a full line of code from the file? Or could these four "word" sequences appear in the middle of a longer line from the file? Or perhaps only at the front or the end of the line?
Second is you want to edit a bunch of TEXT files (programs are stored in text files).
Do you want to edit all of the files in a particular folder?
Do you have a list of the files you want to edit?
Where do you want to place the NEW versions of the files?
If you can solve the find and replace issue you might be able to modify all of the files in one data step by perhaps using wildcards in infile statement and the FILENAME= and FILEVAR= options on the infile and file statement. Or perhaps by using a list of file names to drive the generation of INFILE and FILE statements using the FILEVAR= option on both.
Hi,
since SAS file are basic text files, I would perform the replacements directly from SAS and reserve powershell for more complex tasks.
This is how you could achieve what you want
%MACRO replaceCodeLine();
%*Define here your replacement strategy;
%*codeLine is one line of code in the current SAS program beeing read;
if index(codeLine,'I have three books') then codeLine=tranwrd(codeLine,'I have three books','I need coffee');
else if index(codeLine,'I am going to office') then codeLine=tranwrd(codeLine,'I am going to office','I will be dancing');
%*You could use regex as well depending on your replacement needs;
%MEND replaceCodeLine;
%MACRO proceedCurrentSasPgm(fullpath=);
%*Performs replacement for the current SAS file;
FILENAME saspgm "&fullpath.";
DATA _CurrentSasPgmDS;
*Read each line of code;
infile saspgm dsd truncover;
input codeLine $32767.;
RUN;
DATA _NULL_;
SET _CurrentSasPgmDS;
*Perform replacement;
%replaceCodeLine();
file saspgm;
*write out to file;
put codeLine;
RUN;
*Clean up;
PROC DATASETS lib=work nolist; delete _CurrentSasPgmDS; RUN;QUIT;
filename saspgm clear;
%MEND proceedCurrentSasPgm;
*Get list of *.sas files in directory and subdirectories;
%let filepath=C:\Temp\test\*.sas;
filename tmp pipe "dir /b /s /a:-D ""&filepath."" 2>&1";
DATA _NULL_;
infile tmp dlm="¬";
length Fullpath $2000;
input Fullpath;
*Loop through the list of SAS files and perform replacements in each one;
call execute('%nrstr(%proceedCurrentSasPgm(fullpath='||strip(fullPath)||'));');
RUN;
filename tmp clear;
- Cheers -
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.