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> Dependencies </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  call symputx('msg',sysmsg(),'l');
42  if "&mabort"='HARD' then call symputx('mp_abort',1,'l');
43  end;
44  else do;
45  put (_all_)(=);
46  call symputx('libname',libname,'L');
47  call symputx('liburi',liburi,'L');
48  end;
49  end;
50  else if nobj>1 then do;
51  if "&mabort"='HARD' then call symputx('mp_abort',1);
52  call symputx('msg',"More than one library with libref=&libref");
53  end;
54  else do;
55  if "&mabort"='HARD' then call symputx('mp_abort',1);
56  call symputx('msg',"Library &libref not found in metadata");
57  end;
58  run;
59 
60  %if &mp_abort=1 %then %do;
61  %mp_abort(iftrue= (&mp_abort=1)
62  ,mac=&sysmacroname
63  ,msg=&msg
64  )
65  %return;
66  %end;
67  %else %if %length(&msg)>2 %then %do;
68  %put NOTE: &msg;
69  %return;
70  %end;
71 
72 %end;
73 %else %do;
74  %put NOTE: Library &libref is already assigned;
75 %end;
76 %mend;