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.
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.
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".
That is a good idea. I will try.
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.
That is what I am thinking but I do not know how to write the code.. Can you provide code? Thanks.
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.
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
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.
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?
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?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.