The data does not come from SAS tables but is read directly from the SAS metadata server. The connection is established via options such as 'metaserver', 'metaport', etc. The script queries the metadata API to find specific objects.
1 Code Block
DATA STEP
Explanation : This DATA _NULL_ block is the core of the program. It does not create any output SAS tables. It initializes variables, retains the value of the 'MODE' macro, then constructs a query for metadata. It uses the `metadata_resolve` function to count the number of 'ForeignKey' objects matching the query. If found, it iterates over each object, retrieves its URI, name, ID, and associated table information via `metadata_getnobj`, `metadata_getattr`, and `metadata_getnasn`. Finally, it displays the information in the SAS log and, if the mode is 'DELETE', attempts to delete the object with `metadata_delobj` while checking the operation's return code.
Copied!
data _null_;
/* Initialize variables. */
length FK_id PT_id $17 type FK_uri PT_uri $50 PT_engine $64 PT_name $32 FK_name $60;
call missing ( of _character_ );
retain mode "&MODE";
/* Define a query for ForeignKey FKobjects that do not have a PartnerUniqueKey association */
FK_obj="omsobj:ForeignKey?ForeignKey[not(PartnerUniqueKey/*)]";
/* Determine how many foreign key objects meet the criteria */
FK_Count=metadata_resolve(FK_obj,type,FK_id);
put "NOTE: Found " FK_Count "ForeignKey objects that do not have an associated PartnerUniqueKey";
/* If any were found, iterate through each one to get the attributes and associations */
if FK_Count > 0 then do i = 1 to FK_Count;
/* Get the URI for the nth ForeignKey found */
FK_rc=metadata_getnobj(FK_obj,i,FK_uri);
/* Get the name and ID of the ForeignKey FKobject */
FK_rc=metadata_getattr(FK_uri,"Name",FK_name);
FK_rc=metadata_getattr(FK_uri,"Id",FK_id);
/* get the table name and id this ForeignKey is associated with */
PT_rc=metadata_getnasn(FK_uri,"Table",1,PT_uri);
if PT_rc > 0 then do;
/* Get the name and ID of the table. */
PT_rc=metadata_getattr(PT_uri,"SASTableName",PT_name);
PT_rc=metadata_getattr(PT_uri,"Id",PT_id);
end;
/* REPORT and DELETE */
put "NOTE: Found foreign key: " FK_name "with id: " FK_id "for table: " PT_name "with id: " PT_id;
if mode = "DELETE" then do;
FK_rc = metadata_delobj(FK_uri);
if FK_rc then put 'ERROR: metadata_delobj failed with return code ' FK_rc;
else put 'NOTE: Foreign key successfully deleted';
end;
end;
run;
put "NOTE: Found foreign key: " FK_name "with id: " FK_id "for table: " PT_name "with id: " PT_id;
33
IF mode = "DELETE"THENDO;
34
FK_rc = metadata_delobj(FK_uri);
35
IF FK_rc THEN put 'ERROR: metadata_delobj failed with return code ' FK_rc;
36
ELSE put 'NOTE: Foreign key successfully deleted';
37
END;
38
END;
39
RUN;
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.
« Managing a SAS Metadata repository requires periodic "garbage collection" to prevent performance degradation and synchronization issues. Orphaned Foreign Keys—those missing a corresponding Unique Key—often result from incomplete manual deletions or failed migrations. This script provides a critical administrative safeguard by identifying and pruning these dead links. »
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.