以元数据方式注册SAS库 - 以编程方式

时间:2013-06-11 12:58:23

标签: sas sas-metadata

我正在编写一个部署脚本,并希望以编程方式在元数据中注册一个简单的(和空的)BASE库,例如下面的库。

libname MYLIB 'C:\temp';

可以找到示例XML语法here ..我只是不确定如何将其与proc元数据结合起来执行更新(例如,如何生成元数据ID?)

2 个答案:

答案 0 :(得分:2)

@ user2173800你有没有收到上述问题的解决方案? 这就是我想出的:

以下代码在元数据下创建名为 BASE_Metalib 的SAS库 文件夹: /共享数据/库/ BASE_Metalib (假设此文件夹已存在于元数据中)。该代码还会重新注册为此库定义的此目录下的所有表。以下代码使用Metadata Datastep函数与元数据进行接口。

/*Creating a Metadata Library with BASE Engine and register all the tables under it */


options metaserver="taasasf2"
        metaport=8561
        metauser="testuser" 
        metapass="test123"
        metarepository="Foundation";

%Let MetaLibName=BASE_Metalib; /* Name of the SAS Library with BASE Engine to be created */


data _null_;
    length luri uri muri $256;


    rc=0;

    Call missing(luri,uri,muri);

    /* Create a SASLibrary object in the Shared Data folder. */

    rc=metadata_newobj("SASLibrary",
                       luri,
                       "&MetaLibname.",
                        "Foundation",
                        "omsobj:Tree?@Name=%bquote('&Metalibname.')",
                        "Members");
    put rc=;
    put luri=;

    /* Add PublicType,UsageVersion,Engine,Libref,IsDBMSLibname attribute values. */

        rc=metadata_setattr(luri, 
                            "PublicType",
                            "Library");
        put rc=;
        put luri=;

        rc=metadata_setattr(luri, 
                            "UsageVersion",
                            "1000000.0");
        put rc=;
        put luri=;

        rc=metadata_setattr(luri, 
                            "Engine",
                            "BASE");
        put rc=;
        put luri=;

        rc=metadata_setattr(luri, 
                            "Libref",
                            "SASTEST");
        put rc=;
        put luri=;

        rc=metadata_setattr(luri, 
                            "IsDBMSLibname",
                            "0");
        put rc=;
        put luri=;


/* Set Directory Object via UsingPackages Association for the SAS Library Object */

           rc=metadata_newobj("Directory",
                       uri,
                       "");
         put uri=;



        rc=metadata_setassn(luri,
                        "UsingPackages",
                        "Replace",
                        uri);
       put rc=;

       rc=metadata_setattr(uri,"DirectoryName","/shrproj/files/ANA_AR2_UWCRQ/data");

       put rc=;

/* Set Server Context Object via DeployedComponents Association for the SAS Library Object */


   rc=metadata_getnobj("omsobj:ServerContext?@Name='SASApp'",1,muri);

      put muri=;

      rc=metadata_setassn(luri,
                        "DeployedComponents",
                        "Append",
                        muri);

      put rc=;

Run;



proc metalib;
     omr (library="&Metalibname.");
     report;
run;

答案 1 :(得分:0)

我终于解决了这一点-有几件事要考虑!

1)确保所有必需的对象都存在(以避免孤立的元数据)

2)检查以确保成功创建对象

3)检查以避免两次创建库(幂等)

4)一般优先选择以避免数据步骤元数据功能和相应的无限循环风险

程序的XML部分如下所示:

/**
* Prepare the XML and create the library
*/
data _null_;
  file &frefin;
  treeuri=quote(symget('treeuri'));
  serveruri=quote(symget('serveruri'));
  directoryuri=quote(symget('directoryuri'));
  libname=quote(symget('libname'));
  libref=quote(symget('libref'));
  IsPreassigned=quote(symget('IsPreassigned'));
  prototypeuri=quote(symget('prototypeuri'));

  /* escape description so it can be stored as XML */
  libdesc=tranwrd(symget('libdesc'),'&','&');
  libdesc=tranwrd(libdesc,'<','&lt;');
  libdesc=tranwrd(libdesc,'>','&gt;');
  libdesc=tranwrd(libdesc,"'",'&apos;');
  libdesc=tranwrd(libdesc,'"','&quot;');
  libdesc=tranwrd(libdesc,'0A'x,'&#10;');
  libdesc=tranwrd(libdesc,'0D'x,'&#13;');
  libdesc=quote(trim(libdesc));

  put "<AddMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata> "/
      '<SASLibrary Desc=' libdesc ' Engine="BASE" IsDBMSLibname="0" '/
      '  IsHidden="0" IsPreassigned=' IsPreassigned ' Libref=' libref /
      '  UsageVersion="1000000" PublicType="Library" name=' libname '>'/
      '  <DeployedComponents>'/
      '    <ServerContext ObjRef=' serveruri "/>"/
      '  </DeployedComponents>'/
      '  <PropertySets>'/
      '    <PropertySet Name="ModifiedByProductPropertySet" '/
      '      SetRole="ModifiedByProductPropertySet" UsageVersion="0" />'/
      '  </PropertySets>'/
      "  <Trees><Tree ObjRef=" treeuri "/></Trees>"/
      '  <UsingPackages> '/
      '    <Directory ObjRef=' directoryuri ' />'/
      '  </UsingPackages>'/
      '  <UsingPrototype>'/
      '    <Prototype ObjRef=' prototypeuri '/>'/
      '  </UsingPrototype>'/
      '</SASLibrary></Metadata><NS>SAS</NS>'/
      '<Flags>268435456</Flags></AddMetadata>';
  run;

有关完整代码,请查看github repo