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

Hello,

 

I would like to create libname statement in a do loop before going into a parallel process.

 

I would like to obtain libname as follow:

 

libname libr1 "\\...Documents\Test\Parallel Processing\XML\test1.xml';

libname libr2 "\\...Documents\Test\Parallel Processing\XML\test2.xml';

libname libr3 "\\...Documents\Test\Parallel Processing\XML\test3.xml';

libname libr4 "\\...Documents\Test\Parallel Processing\XML\test4.xml';

 

and so on.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

To do this with a DO loop (rather than a macro %DO loop):

 

data _null_;

   do n=1 to 4;

      call execute ('libname libr' || put(n, 1.) || ' "\\...Documents\Test\Parallel Processing\XML\test' || put(n, 1.) || '.xml"; ');

   end;

run;

 

It becomes mildly more complex if you need to define more than 9 LIBNAME statements, because of the 1. format in the PUT function.  But that can be expanded to 2. and the LEFT function applied to the result.

View solution in original post

6 REPLIES 6
Astounding
PROC Star

To do this with a DO loop (rather than a macro %DO loop):

 

data _null_;

   do n=1 to 4;

      call execute ('libname libr' || put(n, 1.) || ' "\\...Documents\Test\Parallel Processing\XML\test' || put(n, 1.) || '.xml"; ');

   end;

run;

 

It becomes mildly more complex if you need to define more than 9 LIBNAME statements, because of the 1. format in the PUT function.  But that can be expanded to 2. and the LEFT function applied to the result.

alepage
Barite | Level 11

Hello,

 

The solution you have proposed works well.

 

However, I need to use the following filename for my preliminary tests:

 

%let FileName1=data_2_500000;

%let FileName2=data_5_500000;

%let FileName3=data_10_500000;

%let FileName4=data_20_500000;

 

So I need to generate the following instructions:

 

libname lib1 xmlv2 "\\...\xml\data_2_500000.xml";

libname lib2 xmlv2 "\\...\xml\data_5_500000.xml";

libname lib3 xmlv2 "\\...\xml\data_10_500000.xml";

libname lib4 xmlv2 "\\...\xml\data_20_500000.xml";

 

How can we do that?

 

 

Reeza
Super User
There's also a LIBNAME function.

Untested and likely won't work but gives you the idea.

data want;
do i=1 to 4;
ref=lib||put(i, 8.-l);
str = "path to library"||i||".xml";
rc=libname(ref, str);
end;
run;
alepage
Barite | Level 11

Hello Reeza,

 

I have test the following code and it seems to work.

Please note that if the code below is executed without the instruction LIBNAME _ALL_ LIST; the libnames that you have just create won't appear in the log file.

 

Here's the code:

 

%macro DeclareLib;

%let FileName1=data_2_500000;

%let FileName2=data_5_500000;

%let FileName3=data_10_500000;

%let FileName4=data_20_500000;

%do i=1 %to 4;

%let mylib=\\...\Documents\Test\Parallel Processing\XML\&&FileName&i...xml;

%put &mylib;

%if (%sysfunc(libname(lib&i,&mylib.,xml,xmltype=oracle))) %then %put %sysfunc(sysmsg());

%end;

 

LIBNAME _ALL_ LIST;

%mend DeclareLib;

 

%DeclareLib;

 

 

Reeza
Super User
If you only have 4 though, plain vanilla libnames are more efficient.
alepage
Barite | Level 11

I agree with you but believe me I have much more than 4 libnames to declare.

Regards,

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3695 views
  • 2 likes
  • 3 in conversation