"IF" 在 proc SQL 中左连接"SAS"条件



我有一个表a,其中包含列a1a2

我有一个表b,其中包含列b1b2

我想left join b a condition1如果a2 is nullcondition2如果a2 is not null.

如何构造此查询?

如果我

理解正确:

proc sql;
    select . . .
    from a left join
         b
         on (a2 is null and condition1) or
            (a2 is not null and condition2);

这是对您的要求的直接翻译。 通常,以下内容通常具有更好的性能,因为这可以更好地利用索引(取决于条件的性质(:

proc sql;
    select a.*, coalesce(b1.b2, b2.b2) as b2
    from a left join
         b b1
         on (a2 is null and condition1) left join
         b b2
         on (a2 is not null and condition2);

最新更新