我可以在 SAS 中建立数据集之间的关系吗?
Can I set up relations between data set in SAS?
我正在从关系数据库迁移到 SAS。现在我需要将几个 CSV 文件导入 SAS,并且这些文件之间存在关系。就像数据库中表之间的关系一样。我想知道,SAS 中是否存在相同的概念,例如我需要设置的外键,或者我应该直接导入这些文件而不考虑关系,因为 SAS 中没有这样的东西?
既然外键的概念存在于你的脑海中,那么它也存在于SAS中。但它(通常)不是您实际用来标记数据字段的 "a supported attribute"。就必须做大量的前期数据定义而言,SAS 的开销很低,特别是对于临时工作。
只需按原样导入文件。
来自关系数据库,您可能应该将 "proc SQL" 视为在 SAS 中使用数据操作技能的最快和最好的方式。
在 SAS 中,您有 Integrity Constraint 的概念,如评论中提到的 mjsqu。这就是你如何加强数据集之间的关系。它使用起来非常简单,并且语法对于来自强大 SQL 背景的人来说应该相对熟悉。
完整性约束包括:
- 检查(写入数据集的有效值列表)
- Not Null(可能没有缺失值)
- 唯一(不能有重复值)
- 主键
- 外键(也称为 "referential constraint",因为它是唯一检查另一个 table 的外键)
下面是一些 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;
您还可以在 PROC DATASETS
中添加约束,这对于 SAS 用户来说会更熟悉(对于不熟悉 SQL 的用户来说,语法可能稍微简单一些)。
我正在从关系数据库迁移到 SAS。现在我需要将几个 CSV 文件导入 SAS,并且这些文件之间存在关系。就像数据库中表之间的关系一样。我想知道,SAS 中是否存在相同的概念,例如我需要设置的外键,或者我应该直接导入这些文件而不考虑关系,因为 SAS 中没有这样的东西?
既然外键的概念存在于你的脑海中,那么它也存在于SAS中。但它(通常)不是您实际用来标记数据字段的 "a supported attribute"。就必须做大量的前期数据定义而言,SAS 的开销很低,特别是对于临时工作。
只需按原样导入文件。
来自关系数据库,您可能应该将 "proc SQL" 视为在 SAS 中使用数据操作技能的最快和最好的方式。
在 SAS 中,您有 Integrity Constraint 的概念,如评论中提到的 mjsqu。这就是你如何加强数据集之间的关系。它使用起来非常简单,并且语法对于来自强大 SQL 背景的人来说应该相对熟悉。
完整性约束包括:
- 检查(写入数据集的有效值列表)
- Not Null(可能没有缺失值)
- 唯一(不能有重复值)
- 主键
- 外键(也称为 "referential constraint",因为它是唯一检查另一个 table 的外键)
下面是一些 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;
您还可以在 PROC DATASETS
中添加约束,这对于 SAS 用户来说会更熟悉(对于不熟悉 SQL 的用户来说,语法可能稍微简单一些)。