BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bal23
Lapis Lazuli | Level 10

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@Bal23

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

9 REPLIES 9
Patrick
Opal | Level 21

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".

Bal23
Lapis Lazuli | Level 10

That is a good idea. I will try.

Reeza
Super User

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. 

Bal23
Lapis Lazuli | Level 10

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

Reeza
Super User

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. 

 

 

Bal23
Lapis Lazuli | Level 10

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

Patrick
Opal | Level 21

@Bal23

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.  

Bal23
Lapis Lazuli | Level 10

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?

 

Bal23
Lapis Lazuli | Level 10

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-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
  • 9 replies
  • 2693 views
  • 2 likes
  • 3 in conversation