Hi all,
I would like to create some macro that will replace any string in input file(actually formatted file with sas code) to any other string.
I quickly created small code that ,as I thought initially,will solve the task.
%macro replaceInFile(inFileName,outFileName,from,to);
data _null_;
length str strNew $32767;
infile &inFileName dsd dlm='0D'x lrecl=32767;
input str;
strNew=tranwrd(str,"&from","&to");
file &outFileName lrecl=32767;
put strNew;
run;
%mend replaceInFile;
The macro simply use/in out filenames to read one file and create copy of it with replace strings.
Replace I made usingt tranwrd function, and it works ok - in new file all strings that had to be replaced were actually replaced.
But during further tests I experienced another issues - for some files disappears formtting(start spaces or tabs) and also character ' in some cases also disppear in new file.
In attached file is example of such cases.
If you'll execute that macro for this file - new output file will looks like nigtmare - no formatting, char ' disappears for some expressions etc.
So now I hesitate if it's possible to create some general universal solution for such goals.
Maybe there are some ready smarter approaches for such cases?
Thanks!
This works in AIX:
sed -e's/oldttext/newtext/g' oldfile>newfile
The command in the single quotes is what you would do manually in vi
You can of course issue this command from inside SAS with the X statement or the SYSTEM() function or the CALL SYSTEM() routine.
Hi,
This is just my 2p's worth. If I had to do this, I would probably write a small VBA macro in Word and use the search/replace functionality in there. Should be pretty simple, loop over list of files in directory, then a replace() command. Reason is you will hit some walls within importing the text files to SAS as data and then exporting them whilst retaining formatting. Word, which is built to handle text *should* do a better job of it.
There are also freeware out there which do a similar job: Actual Search and Replace - Tool for Text Files
Hi Rw9,
Not sure if VBA will work under AIX(on Windows it works for sure).
I also not sure if I'll be able to install that enchanced tool for texts formatting, due security reasons:)
But anyway thanks, will take a look for that text tool that you propose.
Ah, sorry just assumed Win. There should be something out there for your OS, found this page at a quick search:
http://www.ibm.com/developerworks/aix/library/au-textedit.html
Could just use the OS commands to search replace.
Ok, clear.
So I have variant of implementation without SAS, that is nice but I still hope that maybe there is some SAS way, maybe some cool option that I just need to put in my macro and everything will work correctly:)
Thanks!
Come on, SAS as a streaming text editor? For all things, on a UNIX system???
Do you always hammer in your screws and try to eat soup with a fork?
UNIX was designed as a text-handling tool. There is a host of text manipulation tools like, eg, awk.
Hi Kurt,
Company is moving from Windows to AIX, so I'm new in UNIX.
Offcource SAS isn't best tool for seriosu file manipulations but if the goal is more-less simple why not use SAS?
On Windows I used VBS scripts+X command when I need to made some serious code related with file system etc., but when I had to just copy/rename etc. file I think SAS can be used.
I supposed that replace one word to another in file and save previous file formatting is belong to those "easier" part of tasks, but suppsoe I was wrong due all replier here advise rid of SAS for this task.
Anyway thanks.
This works in AIX:
sed -e's/oldttext/newtext/g' oldfile>newfile
The command in the single quotes is what you would do manually in vi
You can of course issue this command from inside SAS with the X statement or the SYSTEM() function or the CALL SYSTEM() routine.
Hi Kurt,
yep, after a few tests it looks like this line solve the task, works quick and not demage any spec. character in file.
Thanks!
Have a look and see if it does what you are after.
I haven't used it in a while, so it may require some amendments.
Hi Scott,
Your macro looks like has more features, including one that I need, but guys here provide also solutions in one line, so I'll probably will go to study UNIX:)
Thanks!
This search string: "unix command replace text in file" is given 15M hits as it that common in Unix. (inventing the wheel?)
The only exception I can think of is rewriting some dynamic web-parts where this is part of an AJAX uhh ASAX approach
Hi Jaap,
As I replied to Kurt - I split tasks related to file manipulations on two parts - simple and complicated, simple task I implement using SAS functions etc., complicated - using X command, bat files, vbs scripts etc.
But innitially I supposed that this concrette task belongs to simper part, du it just replace one word to another in file, but now I'm not sure that I was right, suppose will really need use some unix commands better.
Thanks!
VI editor under UNIX environment.
Hi Ksharp,
I need to create code that will do it in loop for many files(>100 sas codes), so if VI editor is just similar to N++(maybe with more features) it anyway not ok due I need to apply changes to batch of files.
Anyway thanks!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: