DATA Step, Macro, Functions and more

replace sas code automatically within sas programs

Reply
Contributor
Posts: 55

replace sas code automatically within sas programs

Hi

I have a question which I'm not sure whether this is actually possible.

Let's say you have over a 1000 sas programs stored on a network drive spread over many different folders.

And you have to change a fixed thing within these programs, f.i.

libname testlib z:\networkfolder1\networkfolder2

should be :

libname testlib y:\networkfolder1\networkfolder2

 

Is this possible from another SAS program somehow without opening every individual program ?

Thanks

Regards

B

Respected Advisor
Posts: 2,825

Re: replace sas code automatically within sas programs

[ Edited ]
Posted in reply to Billybob73

Yes, I have written code in a previous job that does this. Basically, you read each line of text from the file, then you write each line of text to a new file with any changes that are necessary.

 

In the long run, a better solution is to create macro variables that you set in your autoexec file, that define file or server locations such as

 

 

%let myfilelocation = y:\networkfolder1\networkfolder2;

Then in your SAS code, you use &myfilelocation instead of y:\networkfolder1\networkfolder2. If the location of the files/server ever changes, you don't have to change the SAS code, you just make one change to the autoexec file.

 

--
Paige Miller
Super User
Posts: 23,296

Re: replace sas code automatically within sas programs

Posted in reply to PaigeMiller

This gets asked occasionally on here. 

 

As @PaigeMiller suggested, removing the libname is a better method in the long run so you never have to repeat this. 

 

https://communities.sas.com/t5/Base-SAS-Programming/Searching-SAS-code-for-keywords/m-p/390472#M9367...

Super User
Posts: 3,858

Re: replace sas code automatically within sas programs

[ Edited ]
Posted in reply to Billybob73

It's for exactly this scenario that shared SAS libraries should be defined and stored in SAS metadata via SAS Management Console removing the need to store them in programs at all. Of course this is only an option if you have a remote SAS server that is metadata enabled. Otherwise the @PaigeMiller solution is good.

 

I suggest you consider removing the shared libnames from your SAS programs and replacing them with metadata-defined ones. That means if you need to change them in the future you only need to do it in one place - SAS Management Console.

 

 

 

 

Contributor
Posts: 55

Re: replace sas code automatically within sas programs

Hi,

These aren't the answers I was looking for. The library assignment was just an example.

Let's say I'd like to replace any fixed thing that is in a lot of files.

How would I proceed if I had like a 1000 programs.

Regards

B

Super User
Super User
Posts: 7,934

Re: replace sas code automatically within sas programs

Posted in reply to Billybob73

Billybob73 wrote:

Hi,

These aren't the answers I was looking for. The library assignment was just an example.

Let's say I'd like to replace any fixed thing that is in a lot of files.

How would I proceed if I had like a 1000 programs.

Regards

B


Write a program to modify the files. SAS programs are just text files.

What we did years ago was make a program that would process one file.  It would look for particular patterns (like LIBNAME, FILE, INFILE statements).  It would convert the original line to a comment and generate a new line.

Users could then manually check and save the results.

 

To run it for a 1000 programs just use another program to generate the list of files to process and then call the first program for each file.

 

You can do it all in SAS or do it all with some other tool.

Ask a Question
Discussion stats
  • 5 replies
  • 159 views
  • 3 likes
  • 5 in conversation