首页 > 解决方案 > 尽管使用 distinct 在 sas 中获取重复值,但如何删除它们?

问题描述

代码输出程序(附加两个表并将其存储在后一个表中):

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 行),也有重复的值,我该如何摆脱这个?我用箭头标记了重复的行。

标签: sqlsasenterprise-guide

解决方案


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

您是否尝试删除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.

推荐阅读