DATA Step, Macro, Functions and more

use macro to change library names

Accepted Solution Solved
Reply
Super Contributor
Posts: 345
Accepted Solution

use macro to change library names

I have a program that includes 20 + sas code files. For each one I need to open the file, change the library names, replace those old library names with my own library names.

Since it is mainly to copy and paste, I would like to get advice from you on how to replace.

 

For example, the old library names are listed below:

 

libname ou1 '/c/Data';

libname oud1 '/c/Data/sap';

 

I need to cross out these two lines and replace them with

libname ou1 '/c/Data/ini';

libname oud1 '/c/Data/ini/sap';

 

Any advice to do it with sas macro, since I need to copy and paste 20 more times? And how to save it in a library? Or any other way to do it more efficiently? Thanks.

 

 


Accepted Solutions
Solution
‎04-07-2017 08:58 AM
Respected Advisor
Posts: 4,173

Re: use macro to change library names

[ Edited ]

@wenling

There are multiple ways of doing what @Reeza proposes but it all boils down to not have fully qualified paths in your source code so you don't run into the issue you're having right now.

 

One approach you could take:

1. Have a macro variable with the root path somewhere centrally defined (i.e. autoexec or  startup code in EG or first %include of an "init.sas" when you run your programs)

%let dataRoot=/c/Data;

2. Use this macro variable as part of your paths in any subsequent code

libname ou1 "&dataRoot";
libname oud1 "&dataRoot/sap";

So now if the root path changes you only need to change it in a single place.

 

/c/data doesn't sound like but if you work in a commercial environment then issue a %put _all_; statement. There is a chance that some SAS Admin already created and maintains such a global macro variable.

IF this is in a commercial environment and they had to change the data folder path which will affect a lot of users then I'd also ask the question if it wouldn't be possible to implement a symbolic link with the old pathname so no code changes are required.

 

And last but not least:

For libnames which get used in many programs and/or by multiple users SAS best practice is to define these in SAS Metadata.  

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

Re: use macro to change library names

If that's a once off task and you need to replace exactly the same paths then what I would do is to open all the 20+ .sas files with Notepad++ and use "Replace All in All Opened Documents".

Super Contributor
Posts: 345

Re: use macro to change library names

That is a good idea. I will try.

Super User
Posts: 19,850

Re: use macro to change library names

In that process, remove those from all the libnames from your programs and put them in one master program.

 

You may as well refactor an inefficient process while you're at it. 

Super Contributor
Posts: 345

Re: use macro to change library names

That is what I am thinking but I do not know how to write the code.. Can you provide code? Thanks.

Super User
Posts: 19,850

Re: use macro to change library names

It's unclear who you're responding to but it literally means moving the library statements from their programs to a single program. 

You need to determine if that would streamline your issues and if this would make things easier. 

 

 

If you're doing rsubmits (server) work then it may not be possible. 

 

 

Super Contributor
Posts: 345

Re: use macro to change library names

Sorry if I had not made it clear. I was replying to your post. Would you please share you code? I believe it would be easier.Thanks

Solution
‎04-07-2017 08:58 AM
Respected Advisor
Posts: 4,173

Re: use macro to change library names

[ Edited ]

@wenling

There are multiple ways of doing what @Reeza proposes but it all boils down to not have fully qualified paths in your source code so you don't run into the issue you're having right now.

 

One approach you could take:

1. Have a macro variable with the root path somewhere centrally defined (i.e. autoexec or  startup code in EG or first %include of an "init.sas" when you run your programs)

%let dataRoot=/c/Data;

2. Use this macro variable as part of your paths in any subsequent code

libname ou1 "&dataRoot";
libname oud1 "&dataRoot/sap";

So now if the root path changes you only need to change it in a single place.

 

/c/data doesn't sound like but if you work in a commercial environment then issue a %put _all_; statement. There is a chance that some SAS Admin already created and maintains such a global macro variable.

IF this is in a commercial environment and they had to change the data folder path which will affect a lot of users then I'd also ask the question if it wouldn't be possible to implement a symbolic link with the old pathname so no code changes are required.

 

And last but not least:

For libnames which get used in many programs and/or by multiple users SAS best practice is to define these in SAS Metadata.  

Super Contributor
Posts: 345

Re: use macro to change library names

Thank you. Before I do this, my understanding is, I should either cross out the original paths or replace with blank, as you suggest on your first post. Do I understand correctly?

 

Super Contributor
Posts: 345

Re: use macro to change library names

Or, can I create another two macros, to replace these two statments wtih blank so that I need not open each one, cross out, or delete?

 

 

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 176 views
  • 1 like
  • 3 in conversation