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 [in] libref The libref (not name) of the metadata library
17  @param [in] mAbort= (HARD) If not assigned, HARD will call %mp_abort(), SOFT
18  will silently return
19 
20  @returns libname statement
21 
22  @version 9.2
23  @author Allan Bowe
24 
25 **/
26 
27 %macro mm_assignlib(
28  libref
29  ,mAbort=HARD
30 )/*/STORE SOURCE*/;
31 %local mp_abort msg;
32 %let mp_abort=0;
33 %if %sysfunc(libref(&libref)) %then %do;
34  data _null_;
35  length liburi LibName msg $200;
36  call missing(of _all_);
37  nobj=metadata_getnobj("omsobj:SASLibrary?@Libref='&libref'",1,liburi);
38  if nobj=1 then do;
39  rc=metadata_getattr(liburi,"Name",LibName);
40  /* now try and assign it */
41  if libname("&libref",,'meta',cats('liburi="',liburi,'";')) ne 0 then do;
42  putlog "&libref could not be assigned";
43  putlog liburi=;
44  /**
45  * Fetch the system message for display in the abort modal. This is
46  * not always helpful though. One example, previously received:
47  * NOTE: Libref XX refers to the same library metadata as libref XX.
48  */
49  msg=sysmsg();
50  if msg=:'ERROR: Libref SAVE is not assigned.' then do;
51  msg=catx(" ",
52  "Could not assign %upcase(&libref).",
53  "Please check metadata permissions! Libname:",libname,
54  "Liburi:",liburi
55  );
56  end;
57  else if msg="ERROR: User does not have appropriate authorization "!!
58  "level for library SAVE."
59  then do;
60  msg=catx(" ",
61  "ERROR: User does not have appropriate authorization level",
62  "for library %upcase(&libref), libname:",libname,
63  "Liburi:",liburi
64  );
65  end;
66  call symputx('msg',msg,'l');
67  if "&mabort"='HARD' then call symputx('mp_abort',1,'l');
68  end;
69  else do;
70  put (_all_)(=);
71  call symputx('libname',libname,'L');
72  call symputx('liburi',liburi,'L');
73  end;
74  end;
75  else if nobj>1 then do;
76  if "&mabort"='HARD' then call symputx('mp_abort',1);
77  call symputx('msg',"More than one library with libref=&libref");
78  end;
79  else do;
80  if "&mabort"='HARD' then call symputx('mp_abort',1);
81  call symputx('msg',"Library &libref not found in metadata");
82  end;
83  run;
84 
85  %put NOTE: &msg;
86 
87 %end;
88 %else %do;
89  %put NOTE: Library &libref is already assigned;
90 %end;
91 
92 %mp_abort(iftrue= (&mp_abort=1)
93  ,mac=mm_assignlib.sas
94  ,msg=%superq(msg)
95 )
96 
97 %mend mm_assignlib;