使用 proc sql 将具有不同结构的多个 table 堆叠为一个 table

Stacking multiple tables with different structure into one table using proc sql

我在 SAS 中创建了多个具有不同字段名称的 table,我想将这些 table 堆叠成一个 table 并将其导出到 Excel。我知道这不是标准的,但它适用于我将 table 导出到 Excel 而不是多个。

我如何在过程中做到这一点 sql?

如果您使用的是 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;

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

 /* 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

使用外部联合怎么样?

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

这是一个例子:

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 中重命名它们并使用 union/union 全部如下:

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;