简体   繁体   English

SAS获取表主键

[英]SAS getting table primary key

I'm completely new in SAS 4GL... 我对SAS 4GL完全陌生...

Is it possible to extract from table, which columns are primary key or parts of compound primary key? 是否可以从表中提取哪些列是主键或复合主键的一部分? I need their values to be merged into one column of an output dataset. 我需要将它们的值合并到输出数据集的一列中。

The problem is, that as an input I can get different tables, and I don't know theirs definition. 问题是,作为输入,我可以获得不同的表,而我不知道它们的定义。

If an index is defined, then you can find out what variable(s) is/are used in that index. 如果定义了索引,则可以找出该索引中使用了哪些变量。 See for example: 参见例如:

data blah(index=(name));
set sashelp.class;
run;

proc contents data=blah out=blahconts;
run;

blahconts has columns that indicate that name is in a simple index, and that it has 1 index total. blahconts列指示该name位于简单索引中,并且总共具有1个索引。

Also, you can have foreign key contraints, such as the following from this SAS documentation example : 另外,您可以具有外键约束,例如此SAS文档示例中的以下内容:

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;

That writes the constraint information to the output window. 这会将约束信息写入输出窗口。 From the output dataset you can see that the variable is in a simple index. 从输出数据集中,您可以看到变量位于简单索引中。 You can wrap either of these (the PROC CONTENTS or the DESCRIBE TABLE CONSTRAINTS ) in ODS OUTPUT to get the information to a dataset: 您可以将这些( PROC CONTENTSDESCRIBE TABLE CONSTRAINTS )包装在ODS OUTPUT中,以将信息获取到数据集:

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM