Published on :

Retrieving Metadata Libraries

This code is also available in: Deutsch Español Français
Awaiting validation
This script uses the `PROC METADATA` procedure to query the metadata repository (OMR - SAS© 9 Metadata Server). It sends an XML request to list 'SASLibrary' type objects. The XML response is then read via a dynamically created XML mapping (SXLEMAP) to produce a SAS© table containing the ID, name, libref, and engine of each library. This code is typical of SAS© 9 or hybrid environments, but does not allow querying the native SAS© Viya infrastructure (which uses microservices/CAS).
Data Analysis

Type : EXTERNE


Data comes from a query of the SAS Metadata Server via PROC METADATA.

1 Code Block
DATA STEP
Explanation :
Calculation and storage in a macro variable of the flags necessary for the metadata query (OMI_SUCCINCT, OMI_GET_METADATA, OMI_ALL_SIMPLE).
Copied!
1DATA _null_;
2 flags=2048+256+8;
3 call symputx('flags',flags,'l');
4RUN;
2 Code Block
PROC METADATA
Explanation :
Execution of the query to the metadata server to retrieve all 'SASLibrary' objects. The XML result is stored in the temporary file reference 'response'.
Copied!
1filename response temp;
2/* get list of libraries */
3PROC METADATA in=
4 '<GetMetadataObjects>
5 <Reposid>$METAREPOSITORY</Reposid>
6 <Type>SASLibrary</Type>
7 <Objects/>
8 <NS>SAS</NS>
9 <Flags>&flags</Flags>
10 <Options/>
11 </GetMetadataObjects>'
12 out=response;
13RUN;
3 Code Block
DATA STEP
Explanation :
Debugging step that writes the raw XML response content to the SAS log.
Copied!
1/* write the response to the log for debugging */
2DATA _null_;
3 INFILE response lrecl=32767;
4 INPUT;
5 put _infile_;
6RUN;
4 Code Block
DATA STEP
Explanation :
Dynamic generation of an SXLEMAP file (XML Map) to define how to parse the returned XML file and extract the desired attributes (Id, Name, Libref, Engine) as columns.
Copied!
1filename sxlemap temp;
2DATA _null_;
3 file sxlemap;
4 put '<SXLEMAP version="1.2" name="SASLibrary">';
5 put '<TABLE name="SASLibrary">';
6 put '<TABLE-PATH syntax="XPath">//Objects/SASLibrary</TABLE-PATH>';
7 put '<COLUMN name="LibraryId">><LENGTH>17</LENGTH>';
8 put '<PATH syntax="XPath">//Objects/SASLibrary/ @Id</PATH></COLUMN>';
9 put '<COLUMN name="LibraryName"><LENGTH>256</LENGTH>>';
10 put '<PATH syntax="XPath">//Objects/SASLibrary/ @Name</PATH></COLUMN>';
11 put '<COLUMN name="LibraryRef"><LENGTH>8</LENGTH>';
12 put '<PATH syntax="XPath">//Objects/SASLibrary/ @Libref</PATH></COLUMN>';
13 put '<COLUMN name="Engine">><LENGTH>12</LENGTH>';
14 put '<PATH syntax="XPath">//Objects/SASLibrary/ @Engine</PATH></COLUMN>';
15 put '</TABLE></SXLEMAP>';
16RUN;
5 Code Block
PROC SORT Data
Explanation :
Assignment of the XML engine using the response file and the generated map, followed by the creation of the output table sorted by library name.
Copied!
1LIBNAME _XML_ xml xmlfileref=response xmlmap=sxlemap;
2 
3/* sort the response by library name */
4PROC SORT DATA=_XML_.saslibrary out=&outds;
5 BY libraryname;
6RUN;
6 Code Block
DATA STEP
Explanation :
Cleanup of the environment by releasing file (filename) and library (libname) references.
Copied!
1/* clear references */
2filename sxlemap clear;
3filename response clear;
4LIBNAME _XML_ clear;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Allan Bowe


Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« This script demonstrates a "low-level" but highly efficient method for querying the SAS 9 Metadata Server (OMR). By bypassing standard interface functions and using PROC METADATA with a dynamic XML Map, you can retrieve bulk structural information in a single network round-trip. This is the gold standard for auditing or documenting large-scale environments where performance is a priority.

Strategic Insights & Best Practices
Mastering the Bitmask Flags: The initial calculation (2048+256+8) is the "brain" of the request. These flags instruct the server to include objects from the current repository, process the request synchronously, and format the output for XML consumption. Misconfiguring these bits is the most common reason for receiving empty or malformed responses.

Dynamic XML Mapping (SXLEMAP): Creating the sxlemap file on the fly is a sophisticated touch. By using XPath expressions (e.g., //Objects/SASLibrary/@Id), you filter the potentially massive metadata response down to only the relevant attributes. This transforms complex, nested XML into a clean, tabular SAS dataset ready for immediate reporting.

Infrastructure Limitations: This code is strictly for the Open Metadata Architecture (OMA). While it works perfectly for SAS 9.4, it is incompatible with SAS Viya's native caslibs. In a Viya environment, you should replace this logic with the CASLIB _ALL_ LIST statement or by querying the Identities and Folders microservices via PROC HTTP. »