Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_assignlib.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Assigns a meta engine library using LIBREF
4  @details Queries metadata to get the library NAME which can then be used in
5  a libname statement with the meta engine.
6 
7  usage:
8 
9  %macro mp_abort(iftrue,mac,msg);%put &=msg;%mend;
10 
11  %mm_assignlib(SOMEREF)
12 
13  <h4> SAS Macros </h4>
14  @li mp_abort.sas
15 
16  @param libref the libref (not name) of the metadata library
17  @param mAbort= If not assigned, HARD will call %mp_abort(), SOFT will silently return
18 
19  @returns libname statement
20 
21  @version 9.2
22  @author Allan Bowe
23 
24 **/
25 
26 %macro mm_assignlib(
27  libref
28  ,mAbort=HARD
29 )/*/STORE SOURCE*/;
30 
31 %if %sysfunc(libref(&libref)) %then %do;
32  %local mp_abort msg; %let mp_abort=0;
33  data _null_;
34  length liburi LibName $200;
35  call missing(of _all_);
36  nobj=metadata_getnobj("omsobj:SASLibrary?@Libref='&libref'",1,liburi);
37  if nobj=1 then do;
38  rc=metadata_getattr(liburi,"Name",LibName);
39  /* now try and assign it */
40  if libname("&libref",,'meta',cats('liburi="',liburi,'";')) ne 0 then do;
41  putlog "&libref could not be assigned";
42  call symputx('msg',sysmsg(),'l');
43  if "&mabort"='HARD' then call symputx('mp_abort',1,'l');
44  end;
45  else do;
46  put (_all_)(=);
47  call symputx('libname',libname,'L');
48  call symputx('liburi',liburi,'L');
49  end;
50  end;
51  else if nobj>1 then do;
52  if "&mabort"='HARD' then call symputx('mp_abort',1);
53  call symputx('msg',"More than one library with libref=&libref");
54  end;
55  else do;
56  if "&mabort"='HARD' then call symputx('mp_abort',1);
57  call symputx('msg',"Library &libref not found in metadata");
58  end;
59  run;
60 
61  %if &mp_abort=1 %then %do;
62  %mp_abort(iftrue= (&mp_abort=1)
63  ,mac=&sysmacroname
64  ,msg=&msg
65  )
66  %return;
67  %end;
68  %else %if %length(&msg)>2 %then %do;
69  %put NOTE: &msg;
70  %return;
71  %end;
72 
73 %end;
74 %else %do;
75  %put NOTE: Library &libref is already assigned;
76 %end;
77 %mend;