SAS的SQL过程(SQL Procedure in SAS)(三)

4.子查询

例如

proc sql;

Select LastName, FirstName

From work.fa

Where not exists

    (select *

from airline.flightschedule

where fa.EmpID=

flightschedule.EmpID);

5.连接条件设定

PROC SQL支持两种连接方式,分别是内连(inner joins)和外连(outer joins)

内联:查询结果仅包含两连接表中彼此相对应的数据记录。最多一次允许32个表内联。

外联:包括左外联(Left outer join),右外联(Right outer join),全外联(Full outer join).

例如

select l.date,

l.flightnumber label=’ Flight Number’,

l.destination label =’ Left’

r.destination label=’ Right’,

delay

from airline.marchflights as l

left join

airline.flightdelays as r

on l.date=r.date and

l.flightnumer=r.flightnumber

order by delay;

将SQL中的连接与数据步中的合并相比较而言,如

data merged;

merge one two;

by x;

run;

select coalesce(one.x,two x)

label=’ x’ , a, b

from one full join two

on one.x=two.x;

注:函数Coalesce返回第一个非缺失值

SQL连接不需事先对字段进行排序,连接条件不需要相同名字的字段,可以设置不等关系,如select columns

From table1 as a, table2 as b

Where a.itemnumber=b.iternumber

And a.cost>b.price;

何时选择SQL连接或者数据步合并(Data step merge)?

(1) 对小型表的合并而言,数据步更有效率

(2) 在合并大型,未排序的表时,SQL连接更有效率

(3) 合并一个大型,排序过的表和一个小型表时,SQL连接更有效率

.使用CREATE TABLE表达式有三种方式

CREATE TABLE table-name(column-name type(length), <column-name,type(length)>…);

CREATE TABLE table-name LIKE table-name;

CREATE TABLE table-name AS query-expression;

(1) 创建表

方式1A:定义列,其后填充数据。如

proc sql;

create table airline.discount

(dest char(3),

BeginDate num format=date9.,

EndDate num format=date9.,

Discount num);

方式1B:从另外一个表copy其定义,其后填充数据。如

proc sql;

create table airline.delaycat

(drop = delayCategory DestinationType)

like airline.flightdelays;

方式2:根据查询结果定义表的行和列。如

proc sql;

create table airline.fa as

select LastName, FirstName, Salary

from airline.payrollmaster, airline.staffmaster

where payrollmaster.EmpID=staffmaster.EmpID

and JobCode contains ’ FA’;

(2) 载入数据到表中

方式A:Set语句。如

proc sql;

insert into discount

set Destination=’ LHR’,

BeginDate=’ 01Mar2000’d,

EndDate=’ 05MAR2000’d,Discount=.33

Set Destination=’ CPH’

BeginDate=’ 03MAR2000’d,

endDate=’ 10MAR2000’d,Discount=.15;

也可以在SET表达式中嵌套使用select查询。如下

proc sql;

insert into discount

set Destination=’ LHR’ , BeginDate=select max(Date)

from airline.flightdelays;

方式B:Values语句

proc sql;

insert into discount

values(’ LHR’ , ’ 01MAR2000’d, ’ 05MAR2000’d,.33)

values(’ CPH’ , ’ 03MAR2000’在,’ 10MAR2000’d,.15)

方式C:查询表达式方式。例如

proc sql;

insert into discount(Destination , Discount)

select Destionation,Rate*.25

from work.fares

where Type=’ special’ ;

7.完整性约束

计有5种完整性约束

(1) NOT NULL

(2) CHECK

(3) UNIQUE

(4) PRIMARY KEY

(5) FOREIGN KEY

实施完整性约束的一般格式如下

PROC SQL;

CREATE TABLE table

(column-specification,…

<constraint-specification , …>;

重新定义Discount表如下,其中限制了票的打折幅度。

Proc sql;

Create table discount

(Destination char(3),

BeginDate date label=’ Begins’,

EndDate date label=’ End’,

Discount num,

CONSTRAINT ok_discount check

(discount le .5);

Describe表达式显示表或者视图的定义。Describe表达式的格式如下

PROC SQL;

DESCRIBE TABLE table-name, <, table-name>…;

DESCRIBE VIEW proc-sql-view <,proc-sql-view>…;

DESCRIBE TABLE CONSTRAINTS table-name <,table-name>…;

例如,显示distcount的结构:

proc sql;

describe table discount;

8.创建视图

一般格式为

CREATE VIEW view-name AS query-expression;

例如:

proc sql;

create view airline.fa as

select LastName, FirstName , Gender, int((today()-DateOfBirth)/365.25) as Age, substr(JobCode,3,1) as Level, Salary

from airline.payrollmaster,airline.staffmaster

where Jobcode contains ’ FA’ and staffmaster.EmpID=payrollmaster.EmpID;

使用视图。例如

proc tabulate data=airline.fa;

class Level;

var Age;

table Level*Age*mean;

run;

posted on 2008-08-22 15:57  zgw21cn  阅读(2032)  评论(0编辑  收藏  举报

导航