Published on :
Administration INTERNAL_CREATION

Delete a Metadata Document

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The `%mm_deletedocument` macro is designed to manage the deletion of 'Document' objects in the SAS© metadata system. It takes a `target` parameter as input, which specifies the full path of the document to be deleted. Before any deletion attempt, it performs a rigorous check of the object's existence and type via the `metadata_pathobj` function. If the object is not found or is not recognized as a 'Document', the macro issues a warning and stops prematurely to avoid unnecessary or erroneous operations. In case of positive validation, the macro dynamically constructs an XML request (`<DeleteMetadata>`) targeting the URI of the document to be deleted, and submits it to the SAS© metadata server using `proc metadata`. After this procedure is executed, a second check is performed to confirm that the document has been successfully removed from the repository. If the deletion fails (the document is still present), an error message is logged and the system return code (`syscc`) is set to 4, indicating an unsuccessful operation.
Data Analysis

Type : INTERNAL_CREATION


The script does not use external data sources or traditional SAS datasets. It interacts directly with the SAS metadata service. The necessary information is either provided as a macro parameter (`target`) or dynamically generated (XML request) within the script itself. No data from SASHELP or other unmanaged sources is required.

1 Code Block
Macro Definition
Explanation :
This block defines the SAS macro `%mm_deletedocument`. It is designed to be called with a `target` parameter, which is the full path of the metadata 'Document' object to be deleted.
Copied!
1%macro mm_deletedocument(
2target=
3)/*/STORE
4SOURCE*/;
5 
2 Code Block
DATA STEP
Explanation :
This `DATA STEP _null_` initializes local variables (`type`, `uri`) and uses the `metadata_pathobj` function to query the metadata repository to check the existence of the object specified by `&target` and obtain its type ('Note') and URI. The retrieved information is stored in the macro variables `type` and `stpuri`. If the object is not a 'Document' or is not found, a warning message is issued, and the macro terminates.
Copied!
1/**
2 * Check document exist
3 */
4%local type;
5DATA _null_;
6 LENGTH type uri $256;
7 rc=metadata_pathobj("","&target",'Note',type,uri);
8 call symputx('type',type,'l');
9 call symputx('stpuri',uri,'l');
10RUN;
11%IF &type ne Document %THEN %DO;
12 %put %str(WARN)ING: No Document found at ⌖
13 %return;
14%END;
3 Code Block
DATA STEP / PROC METADATA Data
Explanation :
This block prepares temporary files for the input (`__in`) and output (`__out`) of the `proc metadata` procedure. A `DATA STEP _null_` is used to generate an XML metadata deletion document, including the URI of the document to be deleted (`&stpuri`), and write it to the `__in` file. The `proc metadata` is then invoked with these files to submit the deletion request to the SAS metadata server.
Copied!
1filename __in temp lrecl=10000;
2filename __out temp lrecl=10000;
3DATA _null_ ;
4 file __in ;
5 put "<DeleteMetadata><Metadata><Document Id='&stpuri'/>";
6 put "</Metadata><NS>SAS</NS><Flags>268436480</Flags><Options/>";
7 put "</DeleteMetadata>";
8RUN ;
9PROC METADATA in=__in out=__out verbose;RUN;
4 Code Block
DATA STEP
Explanation :
This `DATA STEP _null_` reads and displays the content of the `__out` file, which contains the metadata server's response following the deletion operation. This allows viewing the operation status. Then, the fileref definitions for `__in` and `__out` are cleared.
Copied!
1/* list the result */
2DATA _null_;INFILE __out; INPUT; list; RUN;
3 
4filename __in clear;
5filename __out clear;
5 Code Block
DATA STEP
Explanation :
This block performs a final check to ensure that the document has been successfully deleted. The `metadata_pathobj` function is called again to check for the existence of the object at `&target`. If the document is still present (`&isgone = Document`), an error message is displayed, the system return code (`syscc`) is set to 4, and the macro terminates, indicating that the deletion failed.
Copied!
1/**
2 * Check deletion
3 */
4%local isgone;
5DATA _null_;
6 LENGTH type uri $256;
7 call missing (of _all_);
8 rc=metadata_pathobj("","&target",'Note',type,uri);
9 call symputx('isgone',type,'l');
10RUN;
11%IF &isgone = Document %THEN %DO;
12 %put %str(ERR)OR: Document not deleted from ⌖
13 %let syscc=4;
14 %return;
15%END;
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.