DATA Step, Macro, Functions and more

Replace in file, universal solution.

Accepted Solution Solved
Reply
Regular Contributor
Posts: 160
Accepted Solution

Replace in file, universal solution.

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!


Attachment

Accepted Solutions
Solution
‎06-03-2014 07:24 AM
Super User
Posts: 6,936

Re: Replace in file, universal solution.

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 7,401

Re: Replace in file, universal solution.

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

Regular Contributor
Posts: 160

Re: Replace in file, universal solution.

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 reasonsSmiley Happy

But anyway thanks, will take a look for that text tool that you propose.

Super User
Super User
Posts: 7,401

Re: Replace in file, universal solution.

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.

Regular Contributor
Posts: 160

Re: Replace in file, universal solution.

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 correctlySmiley Happy

Thanks!

Super User
Posts: 6,936

Re: Replace in file, universal solution.

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 160

Re: Replace in file, universal solution.

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.

Solution
‎06-03-2014 07:24 AM
Super User
Posts: 6,936

Re: Replace in file, universal solution.

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Regular Contributor
Posts: 160

Re: Replace in file, universal solution.

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!

Super Contributor
Posts: 297

Re: Replace in file, universal solution.

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.

Attachment
Regular Contributor
Posts: 160

Re: Replace in file, universal solution.

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 UNIXSmiley Happy

Thanks!

Valued Guide
Posts: 3,208

Re: Replace in file, universal solution.

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

---->-- ja karman --<-----
Regular Contributor
Posts: 160

Re: Replace in file, universal solution.

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!

Super User
Posts: 9,681

Re: Replace in file, universal solution.

VI  editor under UNIX environment.

Regular Contributor
Posts: 160

Re: Replace in file, universal solution.

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!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 18 replies
  • 498 views
  • 3 likes
  • 6 in conversation