简体   繁体   English

我可以在 SAS 中设置数据集之间的关系吗?

[英]Can I set up relations between data set in SAS?

I am moving from Relational Database and new to SAS.我正在从关系数据库迁移到 SAS。 Now I need to import several CSV files into SAS, and there are relationships between those files.现在我需要将几个 CSV 文件导入到 SAS 中,并且这些文件之间存在关系。 Just like the relationships between tables in database.就像数据库中表之间的关系一样。 I am wondering, does the same concept exist in SAS such as the foreign key which I need to set up, or should I just import those files directly regardless of the relationships because no such things in SAS?我想知道,SAS 中是否存在相同的概念,例如我需要设置的外键,还是我应该直接导入这些文件而不管关系如何,因为 SAS 中没有这样的东西?

Since the concept of a foreign key exists in your head, it also exists in SAS.由于外键的概念存在于您的脑海中,因此它也存在于 SAS 中。 But it is (generally) not "a supported attribute" that you'd actually use to tag data fields.但它(通常)不是您实际用来标记数据字段的“受支持的属性”。 SAS is low overhead in terms of having to do much upfront data definition, especially for ad hoc work. SAS 在必须进行大量前期数据定义方面的开销很低,特别是对于临时工作。

Just import your files as they are.只需按原样导入文件即可。

And coming from relational databases, you should probably look at "proc SQL" as the fastest & best way of using your data manipulation skills in SAS.来自关系数据库,您可能应该将“proc SQL”视为在 SAS 中使用数据操作技能的最快和最佳方式。

In SAS, you have the concept of Integrity Constraint , as mjsqu referred to in comments.在 SAS 中,您有Integrity Constraint的概念,正如评论中提到的 mjsqu 。 It is how you enforce relations between datasets.这是您如何强制执行数据集之间的关系。 It is quite simple to use, and the syntax should be relatively familiar to someone coming from a strong SQL background.它使用起来非常简单,对于具有强大 SQL 背景的人来说,语法应该比较熟悉。

Integrity constraints include:完整性约束包括:

  • Check (list of valid values written into the dataset)检查(写入数据集的有效值列表)
  • Not Null (may not have a missing value) Not Null(可能没有缺失值)
  • Unique (may not have duplicate values)唯一(不能有重复值)
  • Primary Key首要的关键
  • Foreign Key (also known as a "referential constraint", as it is the only one that checks another table)外键(也称为“引用约束”,因为它是唯一一个检查另一个表的)

Here's an example of some of SAS's Integrity Constraints in action:以下是一些 SAS 完整性约束的示例:

proc sql;
  create table employees (
    employee_id num primary key,
    name char(16),
    gender char(1),
    dob num format=date9.,
    division num not null);
  create table division_table (
    division num primary key,
    division_name char(16)
    );
  alter table employees
    add constraint gender check(gender in ('M','F'))
    add constraint division foreign key(division) references division_table
        on delete restrict on update restrict;
  *this insert fails because of Division not containing a value for 1;
  insert into employees (employee_id,name,gender,dob,division) values (1,'Johnny','M','06JAN1945'd,1);

  insert into division_table (division,division_name) values (1,'Division 1');
  *Now it succeeds;
  insert into employees (employee_id,name,gender,dob,division) values (1,'Johnny','M','06JAN1945'd,1);
quit;

You can also add constraints in PROC DATASETS , which will be more familiar for SAS users (and is probably slightly easier syntax for those unfamiliar with SQL).您还可以在PROC DATASETS添加约束,这对 SAS 用户来说会更熟悉(对于不熟悉 SQL 的人来说,语法可能稍微简单一些)。

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

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