简体   繁体   中英

Can I set up relations between data set in SAS?

I am moving from Relational Database and new to SAS. Now I need to import several CSV files into SAS, and there are relationships between those files. 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?

Since the concept of a foreign key exists in your head, it also exists in 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.

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.

In SAS, you have the concept of Integrity Constraint , as mjsqu referred to in comments. 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.

Integrity constraints include:

  • Check (list of valid values written into the dataset)
  • Not Null (may not have a missing value)
  • 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:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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