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.
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.
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.
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?
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;
I agree with you but believe me I have much more than 4 libnames to declare.
Regards,
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.