【问题标题】:Getting duplicate values in sas inspite of using distinct, how can I delete them?尽管使用 distinct 在 sas 中获取重复值,但如何删除它们?
【发布时间】:2018-10-15 22:55:32
【问题描述】:

PROGRAM(附加两个表并存储在后一个表中):

proc sql;
   create table tdstagng.aa as
   select distinct * from tdstagng.aa
   outer union corr
   select distinct * from WORK.DYNAMIC
   ORDER by Cdate;`
quit;

输出

此图像是代码的输出,Cdate 是获取当前日期的列。)程序的目标是构建历史数据以标记随时间的变化。

在 2018 年 10 月 15 日的日期中,即使整行相同(而不是该日期的 7 行,有 14 行),也有重复的值,我该如何摆脱这种情况?我用箭头标记了重复的行。

【问题讨论】:

    标签: sql sas enterprise-guide


    【解决方案1】:

    您选择了两个不同的集合,然后将它们连接起来,而不是仅仅合并它们。这就是导致重复行的原因。

    您是否尝试删除 outer 关键字?这是使用 SASHELP.CLASS 数据的示例。

    23   proc sql ;
    24   create table test1 as
    25   select * from sashelp.class where name like 'A%'
    26   union corr
    27   select * from sashelp.class where sex = 'F'
    28   ;
    NOTE: Table WORK.TEST1 created, with 10 rows and 5 columns.
    
    29   create table test2 as
    30   select * from sashelp.class where name like 'A%'
    31   outer union corr
    32   select * from sashelp.class where sex = 'F'
    33   ;
    NOTE: Table WORK.TEST2 created, with 11 rows and 5 columns.
    

    或者做一个子查询?

    45
    46   create table test3 as
    47   select distinct * from
    48   (
    49   select * from sashelp.class where name like 'A%'
    50   outer union corr
    51   select * from sashelp.class where sex = 'F'
    52   )
    53   ;
    NOTE: Table WORK.TEST3 created, with 10 rows and 5 columns.
    

    【讨论】:

    • Outer 进行附加,如果我删除它不会附加我的表格,是的,我尝试整体做不同的,但它给了我错误
    • 我不明白。 UNION 组合了这些集合。除了保留重复项之外,OUTER 还添加了什么?
    【解决方案2】:
    proc sql;
       create table tdstagng.aa as
       select distinct * from tdstagng.aa
       union
       select distinct * from WORK.DYNAMIC
       ORDER by Cdate;
    quit;
    

    尝试删除外部和 CORR。

    【讨论】:

    • 我这样做了,现在我没有重复。外部联合 corr 是我通过查询生成器附加两个表并随后检查代码时生成的代码。
    【解决方案3】:

    CDate 列中的日期格式是什么? 这可能是由于 DD/MM/YYYY HH:MM 格式——原始日期值中隐藏了时间;

    如果是这种情况,您可以使用函数datepart(CDate) 定义计算列——它只保留日期时间值之外的日期

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多