Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mm_createlibrary.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Create a SAS Library
4 @details Currently only supports BASE engine
5
6 This macro is idempotent - if you run it twice (for the same libref or
7 libname), it will only create one library. There is a dependency on other
8 macros in this library - they should be installed as a suite (see README).
9
10 Usage:
11
12 %mm_createlibrary(
13 libname=My New Library
14 ,libref=mynewlib
15 ,libdesc=Super & <fine>
16 ,engine=BASE
17 ,tree=/User Folders/sasdemo
18 ,servercontext=SASApp
19 ,directory=/tmp/tests
20 ,mDebug=1)
21
22 <h4> SAS Macros </h4>
23 @li mf_verifymacvars.sas
24 @li mm_createfolder.sas
25 @li mp_abort.sas
26
27
28 @param [in] libname= (My New Library) Library name (as displayed to user,
29 256 chars). Duplicates are not created (case sensitive).
30 @param [in] libref= (mynewlib) Library libref (8 chars). Duplicate
31 librefs are not created, HOWEVER- the check is not case sensitive - if
32 *libref* exists, *LIBREF* will still be created.
33 Librefs created will always be uppercased.
34 @param [in] engine= Library engine (currently only BASE supported)
35 @param [in] tree= The metadata folder uri, or the metadata path, in which to
36 create the library.
37 @param [in] servercontext= (SASApp) The SAS server against which
38 the library is registered.
39 @param [in] IsPreassigned= set to 1 if the library should be pre-assigned.
40
41 @param [in] libdesc= Library description (optional)
42 @param [in] directory= (/tmp/somelib) Required for the BASE engine.
43 The metadata directory objects are searched to find an existing
44 one with a matching physical path.
45 If more than one uri found with that path, then the first one will be used.
46 If no URI is found, a new directory object will be created. The physical
47 path will also be created, if it doesn't exist.
48
49
50 @param [in] mDebug= set to 1 to show debug messages in the log
51 @param [in] frefin= fileref to use (enables change if there is a conflict).
52 The filerefs are left open, to enable inspection after running the
53 macro (or importing into an xmlmap if needed).
54 @param [out] frefout= fileref to use (enables change if there is a conflict)
55
56
57 @version 9.3
58 @author Allan Bowe
59
60**/
61
62%macro mm_createlibrary(
63 libname=My New Library
64 ,libref=mynewlib
65 ,libdesc=Created automatically using the mm_createlibrary macro
66 ,engine=BASE
67 ,tree=/User Folders/sasdemo
68 ,servercontext=SASApp
69 ,directory=/tmp/somelib
70 ,IsPreassigned=0
71 ,mDebug=0
72 ,frefin=mm_in
73 ,frefout=mm_out
74)/*/STORE SOURCE*/;
75
76%local mD;
77%if &mDebug=1 %then %let mD=;
78%else %let mD=%str(*);
79%&mD.put Executing &sysmacroname..sas;
80%&mD.put _local_;
81
82%let libref=%upcase(&libref);
83
84/**
85 * Check Library does not exist already with this libname
86 */
87data _null_;
88 length type uri $256;
89 rc=metadata_resolve("omsobj:SASLibrary?@Name='&libname'",type,uri);
90 call symputx('checktype',type,'l');
91 call symputx('liburi',uri,'l');
92 putlog (_all_)(=);
93run;
94%if &checktype = SASLibrary %then %do;
95 %put %str(WARN)ING: Library (&liburi) already exists with libname (&libname);
96 %return;
97%end;
98
99/**
100 * Check Library does not exist already with this libref
101 */
102data _null_;
103 length type uri $256;
104 rc=metadata_resolve("omsobj:SASLibrary?@Libref='&libref'",type,uri);
105 call symputx('checktype',type,'l');
106 call symputx('liburi',uri,'l');
107 putlog (_all_)(=);
108run;
109%if &checktype = SASLibrary %then %do;
110 %put %str(WARN)ING: Library (&liburi) already exists with libref (&libref) ;
111 %return;
112%end;
113
114
115/**
116 * Attempt to create tree
117 */
118%mm_createfolder(path=&tree)
119
120/**
121 * check tree exists
122 */
123data _null_;
124 length type uri $256;
125 rc=metadata_pathobj("","&tree","Folder",type,uri);
126 call symputx('foldertype',type,'l');
127 call symputx('treeuri',uri,'l');
128run;
129%if &foldertype ne Tree %then %do;
130 %put %str(WARN)ING: Tree &tree does not exist!;
131 %return;
132%end;
133
134/**
135 * Create filerefs for proc metadata call
136 */
137filename &frefin temp;
138filename &frefout temp;
139
140%mp_abort(iftrue= (
141 &engine=BASE & %mf_verifymacvars(libname libref engine servercontext tree)=0
142 )
143 ,mac=&sysmacroname
144 ,msg=%str(Empty inputs: libname libref engine servercontext tree)
145)
146
147%if &engine=BASE %then %do;
148 /**
149 * Check that the ServerContext exists
150 */
151 data _null_;
152 length type uri $256;
153 rc=metadata_resolve("omsobj:ServerContext?@Name='&ServerContext'",type,uri);
154 call symputx('checktype',type,'l');
155 call symputx('serveruri',uri,'l');
156 putlog (_all_)(=);
157 run;
158 %if &checktype ne ServerContext %then %do;
159 %put %str(ERR)OR: ServerContext (&ServerContext) does not exist!;
160 %return;
161 %end;
162
163 /**
164 * Get prototype info
165 */
166 data _null_;
167 length type uri str $256;
168 str="omsobj:Prototype?@Name='Library.SAS.Prototype.Name.xmlKey.txt'";
169 rc=metadata_resolve(str,type,uri);
170 call symputx('checktype',type,'l');
171 call symputx('prototypeuri',uri,'l');
172 putlog (_all_)(=);
173 run;
174 %if &checktype ne Prototype %then %do;
175 %put %str(ERR)OR: Prototype Library.SAS.Prototype.Name.xmlKey.txt not found;
176 %return;
177 %end;
178
179 /**
180 * Check that Physical location exists
181 */
182 %if %sysfunc(fileexist(&directory))=0 %then %do;
183 %put %str(ERR)OR: Physical directory (&directory) does not appear to exist!;
184 %return;
185 %end;
186
187 /**
188 * Check that Directory Object exists in metadata
189 */
190 data _null_;
191 length type uri $256;
192 rc=metadata_resolve("omsobj:Directory?@DirectoryRole='LibraryPath'"
193 !!" and @DirectoryName='&directory'",type,uri);
194 call symputx('checktype',type,'l');
195 call symputx('directoryuri',uri,'l');
196 putlog (_all_)(=);
197 run;
198 %if &checktype ne Directory %then %do;
199 %put NOTE: Directory object does not exist for (&directory) location;
200 %put NOTE: It will now be created;
201
202 data _null_;
203 file &frefin;
204 directory=quote(symget('directory'));
205 put "<AddMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata> "/
206 '<Directory UsageVersion="1000000" IsHidden="0" IsRelative="0"'/
207 ' DirectoryRole="LibraryPath" Name="Path" DirectoryName=' directory '/>'/
208 "</Metadata><NS>SAS</NS>"/
209 "<Flags>268435456</Flags></AddMetadata>";
210 run;
211
212 proc metadata in= &frefin out=&frefout %if &mdebug=1 %then verbose;;
213 run;
214 %if &mdebug=1 %then %do;
215 data _null_;
216 infile &frefout lrecl=1048576;
217 input; put _infile_;
218 run;
219 %end;
220 %put NOTE: Checking to ensure directory (&directory) object was created;
221 data _null_;
222 length type uri $256;
223 rc=metadata_resolve("omsobj:Directory?@DirectoryRole='LibraryPath'"
224 !!" and @DirectoryName='&directory'",type,uri);
225 call symputx('checktype2',type,'l');
226 call symputx('directoryuri',uri,'l');
227 %if &mdebug=1 %then putlog (_all_)(=);;
228 run;
229 %if &checktype2 ne Directory %then %do;
230 %put %str(ERR)OR: Directory (&directory) object was NOT created!;
231 %return;
232 %end;
233 %else %put NOTE: Directory (&directoryuri) successfully created!;
234 %end;
235
236 /**
237 * check SAS version
238 */
239 %if %sysevalf(&sysver lt 9.3) %then %do;
240 %put %str(WARN)ING: Version 9.3 or later required;
241 %return;
242 %end;
243
244 /**
245 * Prepare the XML and create the library
246 */
247 data _null_;
248 file &frefin;
249 treeuri=quote(symget('treeuri'));
250 serveruri=quote(symget('serveruri'));
251 directoryuri=quote(symget('directoryuri'));
252 libname=quote(symget('libname'));
253 libref=quote(symget('libref'));
254 IsPreassigned=quote(symget('IsPreassigned'));
255 prototypeuri=quote(symget('prototypeuri'));
256
257 /* escape description so it can be stored as XML */
258 libdesc=tranwrd(symget('libdesc'),'&','&amp;');
259 libdesc=tranwrd(libdesc,'<','&lt;');
260 libdesc=tranwrd(libdesc,'>','&gt;');
261 libdesc=tranwrd(libdesc,"'",'&apos;');
262 libdesc=tranwrd(libdesc,'"','&quot;');
263 libdesc=tranwrd(libdesc,'0A'x,'&#10;');
264 libdesc=tranwrd(libdesc,'0D'x,'&#13;');
265 libdesc=tranwrd(libdesc,'$','&#36;');
266 libdesc=quote(trim(libdesc));
267
268 put "<AddMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata> "/
269 '<SASLibrary Desc=' libdesc ' Engine="BASE" IsDBMSLibname="0" '/
270 ' IsHidden="0" IsPreassigned=' IsPreassigned ' Libref=' libref /
271 ' UsageVersion="1000000" PublicType="Library" name=' libname '>'/
272 ' <DeployedComponents>'/
273 ' <ServerContext ObjRef=' serveruri "/>"/
274 ' </DeployedComponents>'/
275 ' <PropertySets>'/
276 ' <PropertySet Name="ModifiedByProductPropertySet" '/
277 ' SetRole="ModifiedByProductPropertySet" UsageVersion="0" />'/
278 ' </PropertySets>'/
279 " <Trees><Tree ObjRef=" treeuri "/></Trees>"/
280 ' <UsingPackages> '/
281 ' <Directory ObjRef=' directoryuri ' />'/
282 ' </UsingPackages>'/
283 ' <UsingPrototype>'/
284 ' <Prototype ObjRef=' prototypeuri '/>'/
285 ' </UsingPrototype>'/
286 '</SASLibrary></Metadata><NS>SAS</NS>'/
287 '<Flags>268435456</Flags></AddMetadata>';
288 run;
289
290
291 proc metadata in= &frefin out=&frefout %if &mdebug=1 %then verbose ;;
292 run;
293
294 %if &mdebug=1 %then %do;
295 data _null_;
296 infile &frefout lrecl=1048576;
297 input;put _infile_;
298 run;
299 %end;
300 %put NOTE: Checking to ensure library (&libname) was created;
301 data _null_;
302 length type uri $256;
303 rc=metadata_pathobj("","&tree/&libname","Library",type,uri);
304 call symputx('libtype',type,'l');
305 call symputx('liburi',uri,'l');
306 %if &mdebug=1 %then putlog (_all_)(=);;
307 run;
308 %if &libtype ne SASLibrary %then %do;
309 %put %str(ERR)OR: Could not find (&libname) at (&tree)!!;
310 %return;
311 %end;
312 %else %put NOTE: Library (&libname) successfully created in (&tree)!;
313%end;
314%else %do;
315 %put %str(ERR)OR: Other library engine types are not yet supported!!;
316%end;
317
318
319/**
320 * Wrap up
321 */
322%if &mdebug ne 1 %then %do;
323 filename &frefin clear;
324 filename &frefout clear;
325%end;
326
327%mend mm_createlibrary;