BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Yura2301
Quartz | Level 8

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!


1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

18 REPLIES 18
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Yura2301
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Yura2301
Quartz | Level 8

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!

Kurt_Bremser
Super User

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.

Yura2301
Quartz | Level 8

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.

Kurt_Bremser
Super User

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.

Yura2301
Quartz | Level 8

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!

Scott_Mitchell
Quartz | Level 8

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.

Yura2301
Quartz | Level 8

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!

jakarman
Barite | Level 11

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 --<-----
Yura2301
Quartz | Level 8

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!

Ksharp
Super User

VI  editor under UNIX environment.

Yura2301
Quartz | Level 8

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!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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