【问题标题】:join 2 tables on different date formats using proc sql使用 proc sql 以不同的日期格式连接 2 个表
【发布时间】:2017-04-11 20:57:59
【问题描述】:

我正在尝试使用 proc sql 在 sas 中加入两个表。但是日期不同(表 1 的格式为 Datetime,表 2 的格式为 date9)。我想用 id (在两个表中都很常见)和日期来加入表。 DATEPART 似乎不起作用。有任何想法吗?这是我试图运行但不起作用的代码:

proc sql;
create table p.data1 as
select 
    a.*,
    b.var1 as var1_alt,
    Datepart(b.MonthEndDate) format date9. as EOMDate

from 
    p.base_1 a

    left join q.a_GLV b
    on a.ID = b.ID
    and a.MonthEndDate = b.MonthEndDate
order by
    a.ID,
    a.MonthEndDate
;
quit;

【问题讨论】:

  • 首先将它们转换为相同的日期类型

标签: sql join sas


【解决方案1】:

您需要在联接中使用 datepart,因为 datetimedate 在 SAS 中是不同的数字(秒数与天数)。

proc sql;
create table p.data1 as
select 
    a.*,
    b.var1 as var1_alt,
    Datepart(b.MonthEndDate) format date9. as EOMDate

from 
    p.base_1 a

    left join q.a_GLV b
    on a.ID = b.ID
    and a.MonthEndDate = datepart(b.MonthEndDate)
order by
    a.ID,
    a.MonthEndDate
;
quit;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2018-09-24
    • 1970-01-01
    相关资源
    最近更新 更多