繁体   English   中英

如何将四个表中的四个数字放入一个 2 x 2 的表中?

[英]How to put four numbers from four tables into a 2 by 2 table?

我正在使用 SAS 进行分析。 每次分析完后,我都会得到四张表。 Table11Table12Table12 Tabe21Table12 Table22 每张桌子只有一个号码。 我想将这四个数字(来自四个表)放入一个 2x2 矩阵中。 我怎样才能使用 SAS proc sql做到这一点?

例如,当我有1 (表 1 中唯一的数字)、 2 (表 2 中唯一数字)、 3 (表 3 中唯一数字)、 4 (表 4 中唯一数字)时,我想生产花药表,里面有一个 2 x 2 的表,里面有这四个数字。

一个完全hacky,可怕的方法可能是这样的:

SELECT (SELECT num FROM table1), (SELECT num FROM table2)
    UNION ALL 
SELECT (SELECT num FROM table3), (SELECT num FROM table4)

您可能必须在联合的每个部分之后添加一个“虚拟”表(如 DB2 中的SELECT ... FROM sysibm.sysdummy1 )。

我从未使用过SAS,所以我不知道具体情况。 也许那个(标准 SQL)解决方案对你有用。 但就像我说的那样,它非常骇人听闻,所以可能有更好的方法来做到这一点。

可能具有等效的执行计划(使用隐式交叉连接):

SELECT table11.num, table12.num
FROM table11, table12
    UNION ALL 
SELECT table21.num, table22.num
FROM table21, table22

很明显,您想采用工作保障技术。

with
    t11(r, c, num) as (SELECT 1, 1, num from table11),
    t12(r, c, num) as (SELECT 1, 2, num from table12),
    t21(r, c, num) as (SELECT 2, 1, num from table21),
    t22(r, c, num) as (SELECT 2, 2, num from table22)
select
    coalesce(t11.num, t21.num) as col1,
    coalesce(t12.num, t22.num) as col2
from 
    (t11 cross join t12)
    full outer join
    (t21 cross join t22)
        on t11.r = t21.r or t12.r = t22.r

或者,如果您不想浪费 c 列中的值,这可能会更好。

    case
        when t11.r * t11.c = t12.c - t12.r
        then t11.num else t21.num
    end as col1,
    case
        when t21.r * t22.r * t22.c - t21.c = t21.r + t21.c + t22.r + t22.c
        then t22.num else t12.num
    end as col2

您已经收到了上面的一些优点/解决方案,但是您可能想尝试在加入之前自动检查每个数据集的存在。 类似于以下内容:

data c12;
c12=32;
run;

data c21;
c21=40;
run;

data c22;
c22=12;
run;

%sysfunc(ifc(%sysfunc(exist(c11)),
             proc print data = a; run;,
             data _null_; file print; put 'Dataset "c11: was not created'; run;))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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