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,

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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