使用proc sql

时间:2018-07-26 17:28:59

标签: sql sas outer-join

我在SAS中使用不同的字段名创建了多个表,我想将这些表堆叠到一个表中并将其导出到Excel。我知道这不是标准的,但是对我来说可以将表导出到Excel,而不是多个表。

我该如何在proc sql中做到这一点?

enter image description here

3 个答案:

答案 0 :(得分:3)

如果您使用的是SAS 9.4 TS1M3 +,请改用ODS EXCEL。

ods excel file = 'C:\_localdata\demo.xlsx' options(sheet_interval = 'none') style=meadow;

   proc print data=sashelp.cars (obs=5);
   proc print data=sashelp.air (obs=5);
   proc print data=sashelp.class (obs=5);
   run;

ods excel close;

答案 1 :(得分:1)

@Reeza答案很干净,但是如果您想在proc sql中做同样的事情,那么您需要在insert语句中使用您想做的列名(数据类型应该匹配)。让我通过一个例子来说明

 /* first create table with most columns you want*/
 proc sql;
 create table class as
 select * from sashelp.class; 

 /*first create table with one less column*/
 proc sql;
 create table class1(drop=height) as
 select * from sashelp.class;

/*createsecond table with one less column*/
proc sql;
create table class2(drop = height sex)
as select * from class;
 /* insert can be done into first table with lesser columns by explicitly mentioning  columns in parenthesis as shown below */
 proc sql;
insert into class(name,sex, age, weight)
select * from class1;

 /* insert can be done into first table with  more lesser columns by explicitly 
   mentioning  columns in parenthesis as shown below */
 proc sql;
insert into class(name,age, weight)
select * from class2;

然后您可以将proc导出到excel

答案 2 :(得分:1)

如何使用外部联合?

如果我对问题的理解正确,则需要所有数据集中的所有列。

这是一个例子:

data test1;
x=1;
y=2;
run;
data test2;
    x=2;
    y=2;
    z=1;
run;
data test3;
    x=1;
    o=14;
    p=12;
run;

proc sql;

    create table TEST_O as
        select * from test1
        OUTER UNION
        select * from test2
        OUTER UNION
        select * from test3
    ;
quit;

当然,它们之间没有相同的列名,但是您可以预处理数据集并动态重命名列,或者只是在select中重命名它们,并使用并集/联合,如下所示:

proc sql;

    create table TEST_O2 as
        select x as col1, y as col2 from test1
        UNION all
        select x as col1, y as col2, z as col3 from test2
        UNION all
        select  x as col1, o as col2, p as col3 from test3
    ;
quit;