Hi @s_lassen
I finally managed to get it to work. Actually, I didn't use your code, but found another way. However, your code and comments got me thinking and led me on the right track, so it deserves an "Accepted Solution".
I could not find a way to check for success if the libname allocation. The libname statement writes a note only, if the fhysical library doesn't exist, but nothing else. I tried to let the Proc Contents fail and reset error conditions, but that couldn't fool our Log Scanner, and I didn't want the job to fail, so I ended up with redirecting the log and retrieve the "not exist" note from that.
I couldn't get the macro call working either, so I dropped the macro and wrote the code "inline". It turned out that not only the macro call, but all macro statements excute immediately when the code is executed by Call Execute, unless they are put in a %nrstr function. After discovering that, I could have made a macro instead, but it works as is, and I had other pressing matters to see to.
/***********************************************************************************/
/* caslibcontent erlu 3.12.2022 */
/* */
/* Danner indhold af CAS libraries specificeret i et inputdatasæt. */
/* */
/* Input skal som minumum indeholde kolonnerne */
/* Metaserver */
/* Libref */
/* CASLibname */
/* DBServer */
/* Input skal være sorteret på Metaserver. */
/* */
/* */
/***********************************************************************************/
* Initier outputdatasæt;
data &_OUTPUT;
length
LIBNAME $8
MEMNAME $32
NAME $32
TYPE 8
LENGTH 8
VARNUM 8
LABEL $256
FORMAT $32
FORMATL 8
FORMATD 8
NOBS 8
SASLibraryID $17
CASLibname $60
DBServer $30
MetaServer $30
;
stop;
run;
/*---------------------------------------------------------------------------------*/
/* Hovedprogram til at gennemløbe input med CAS libnames og danne output med Proc */
/* Content resultater fra alle libnames */
/*---------------------------------------------------------------------------------*/
* håndter ikke-V7-navne i CAS;
options validmemname=EXTEND validvarname=ANY;
* Loop over proc-contents for alle libnames og saml tabelinformation fra CAS;
data _null_;
set &_INPUT;
by MetaServer;
retain current_session;
filename liblog temp;
* Initier ny CAS session, når vi starter på et nyt miljø;
if first.Metaserver then do;
call execute(catt('options cashost="', dbserver, '" casport=5570;'));
current_session = cats("&sysuserid", put((datetime()*1000),13.0),put(_N_,8.));
put current_session=;
call execute(catx(' ', 'cas', current_session, ';'));
end;
* Slet workdata inden behandling af nyt libname;
call execute('proc datasets lib=work nolist; delete tmp tmp2; run;');
* Rediriger log;
call execute('proc printto log=liblog new; run;');
* Alloker libname;
libref = substr(dbserver,1,1) || put(_N_,Z7.);
call execute(
catx(' ',
'libname',
Libref,
catt('CAS CASLIB="', CASLibname, '"'),
catt('SERVER="', DBserver, '"' ),
'PORT=5570;'
)
);
%* Tag log tilbage;
call execute('proc printto; run;');
%* Indlæs templog og sæt flag for om library findes;
call execute(
catx(' ',
'data _null_;',
'infile liblog end=eof;',
'retain result 0;',
'input;',
'put _infile_;',
'result = max(result,index(_infile_,"does not exist"));',
'if eof then do;',
'put result=;',
'call symputx("result",result);',
'end;',
'run;'
)
);
* TEST på result;
call execute(
catx(' ',
'%nrstr(%%if) %nrstr(&result) = 0 %nrstr(%%then) %nrstr(%%do);',
'%nrstr(%%put) **** Libname OK;',
catt('proc contents data=', Libref, '._all_ noprint out=tmp; run;'),
'proc sql; create table tmp2 as select',
'LIBNAME length=8,',
'MEMNAME length=32,',
'NAME length=32,',
'TYPE length=8,',
'LENGTH length=8,',
'VARNUM length=8,',
'LABEL length=256,',
'FORMAT length=32,',
'FORMATL length=8,',
'FORMATD length=8,',
'NOBS length=8,',
catt('"', ID, '"'), 'as SASLibraryID length=17,',
catt('"', CASLibname, '"'), 'as CASLibname length=60,',
catt('"', dbserver, '"'), 'as DBServer length=30,',
catt('"', MetaServer, '"'), 'as MetaServer length=30',
'from tmp; quit;',
'proc append base=&_OUTPUT data=tmp2 (keep=',
'LIBNAME MEMNAME NAME TYPE LENGTH VARNUM LABEL FORMAT FORMATL FORMATD NOBS SASLibraryID CASLibname DBServer MetaServer',
') force; run;',
'%nrstr(%%end);',
'%nrstr(%%else) %nrstr(%%do);',
'%nrstr(%%put) **** Libname findes ikke; %nrstr(%%end);'
)
);
%* Frigiv libname igen;
call execute(catx(' ',' libname', Libref, 'clear;'));
* Afslut igangværende CAS session, når vi er igennem et miljø;
if last.metaserver then do;
call execute(catx(' ', 'cas', current_session, 'terminate;'));
end;
run;
* Resæt til DWH standard;
options validmemname=COMPATIBLE validvarname=V7;
... View more