/******************************************************************************
 * Programme : Automatisation SAS : Comment assigner vos bibliothèques dynamiquement via les métadonnées
 * Reference : ASSIGN0F4F
 * Source    : https://www.wearecas.eu/en/sampleCode/ASSIGN0F4F
 ******************************************************************************/

/* --- BLOC 1 --- */
%macro assign_metalib(libref=/* libref that needs to be assigned */
  );
%if %sysfunc(libref(&libref)) %then %do;
  data _null_;
    length lib_uri LibName $200;
    call missing(of _all_);
    nobj=metadata_getnobj("omsobj:SASLibrary? @Libref='&libref'",1,lib_uri);
    if nobj=1 then do;
       rc=metadata_getattr(lib_uri,"Name",LibName);
       call symputx('libname',libname,'L');
    end;
    else if nobj>1 then putlog "ERROR: More than one library with libref='&libref'";
    else putlog "ERROR: Library '&libref' not found in metadata";
  run;

  libname &libref meta library="&libname";
  %if %sysfunc(libref(&libref)) %then 
    %put ERROR: assign_metalib macro could not assign &libref;
%end;
%else %put NOTE: Library &libref is already assigned;
%mend;

