【问题标题】:SAS getting table primary keySAS获取表主键
【发布时间】:2014-03-31 12:27:38
【问题描述】:

我是 SAS 4GL 的新手……

是否可以从表中提取哪些列是主键或复合主键的一部分?我需要将它们的值合并到输出数据集的一列中。

问题是,作为输入,我可以获得不同的表,而我不知道它们的定义。

【问题讨论】:

    标签: sas data-integration


    【解决方案1】:

    如果定义了索引,那么您可以找出该索引中使用了哪些变量。例如:

    data blah(index=(name));
    set sashelp.class;
    run;
    
    proc contents data=blah out=blahconts;
    run;
    

    blahconts 的列表明 name 在一个简单索引中,并且它总共有 1 个索引。

    此外,您还可以使用外键约束,例如来自this SAS documentation example 的以下内容:

    proc sql;
       create table work.mystates
          (state      char(15), 
           population num,
           continent  char(15),
    
              /* contraint specifications */
           constraint prim_key    primary key(state),
           constraint population  check(population gt 0),
           constraint continent   check(continent in ('North America', 'Oceania')));      
    
       create table work.uspostal
          (name      char(15),
           code      char(2) not null,          /* constraint specified as    */
                                                /* a column attribute         */
    
           constraint for_key foreign key(name) /* links NAME to the          */
                      references work.mystates   /* primary key in MYSTATES    */
    
                         on delete restrict     /* forbids deletions to STATE */
                                                /* unless there is no         */
                                                /* matching NAME value        */
    
                         on update set null);   /* allows updates to STATE,   */
                                                /* changes matching NAME      */
                                                /* values to missing          */ 
    quit;
    
    proc contents data=uspostal out=postalconts;
    run;
    proc sql;
    describe table constraints uspostal;
    quit;
    

    将约束信息写入输出窗口。从输出数据集中,您可以看到该变量位于一个简单的索引中。您可以将其中任何一个(PROC CONTENTSDESCRIBE TABLE CONSTRAINTS)包装在 ODS OUTPUT 中以将信息获取到数据集:

    ods output  IntegrityConstraints=postalICs;
    proc contents data=uspostal out=postalconts;
    run;
    ods output close;
    

    ods output  IntegrityConstraints=postalICs;
    proc sql;
    describe table constraints uspostal;
    quit;
    ods output close;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多