SAS PROC SQL - 在两个字符变量上使用左连接的问题

时间:2016-10-06 18:26:10

标签: sql sas enterprise-guide

    DataSet: Test1

    Name        Type        Length  Format      Informat    
    RowID       Numeric     8        6.           6.        
    COL2        Character   6       $6.           $6.       
    COL3        Numeric     8    NUMERIC12.      NUMERIC12. 
    DATE        Numeric     8        11.                   11.      
    TIME        Character   8      $CHAR8.             $CHAR8.      
    Amount      Numeric     8                   


  DataSet: Test2

Name                Type        Length  Format      Informat    
RowID               Numeric     8   9.              9.      
COL2                Character   32  $32.             $32.       
COL3                Date        8   DATETIME27.6    DATETIME27.6    
COL4                Character   17  $17.            $17.        
TIME                Character   8   $CHAR8.         $CHAR8.     
COL5                Numeric     8   NUMERIC12.       NUMERIC12. 
AMOUNT              Numeric     8                   

样本数据

Test1

RowID    COL2      COL3     DATE         TIME      AMOUNT

3330    123456     123      20110523    14.14.50    2.00
3330    334567     123      20110523    19.13.34    2.00
3330    889789     123      20110523    20.01.11    2.00
3330    45678      1643     20110523    06.53.05    6.00


TEST2

RowID    COL2                                           COL3             COL4                   TIME       COL5    Amount

3330    0010181002233611096xyBC3TLnDkVB7   23MAY2011:19:14:50.000000    20110523 14:14:50    14:14:50      123     2.00                                                             
3330    0010181005005029491mnopqrbT2cySA   24MAY2011:00:13:34.000000    20110523 19:13:34    19:13:34      123     2.00          
3330    001018100222213220332ghijkl63BR1   23MAY2011:11:53:05.000000    20110523 06:53:05    06:53:05      1643    6.00       
3330    00101810021738472682abcdef7vUcte   23MAY2011:13:30:03.000000    20110523 08:30:03    06:53:05      5575    1.00         

我想在Test1.COL3 = Test2.COL5和Test1.Time = Test2.TIME上使用Test2连接Test1和所需的输出看起来像

 Final

 RowID   COL2      COL3     DATE         TIME      AMOUNT     RowID          COL2                              COL3                               COL4            TIME       COL5    Amount

3330    123456     123      20110523    14.14.50    2.00      3330     0010181002233611096xyBC3TLnDkVB7     23MAY2011:19:14:50.000000       20110523 14:14:50    14:14:50      123     2.00   
3330    334567     123      20110523    19.13.34    2.00      3330     0010181005005029491mnopqrbT2cySA     24MAY2011:00:13:34.000000       20110523 19:13:34    19:13:34      123     2.00  
3330    889789     123      20110523    20.01.11    2.00         .           .                  .             .          .      .     .
3330    45678      1643     20110523    06.53.05    6.00      3330     001018100222213220332ghijkl63BR1     23MAY2011:11:53:05.000000    20110523 06:53:05    06:53:05      1643       6.00

我运行以下代码是SAS

  proc sql;
  create table final as
  select * from 
  (
  (select * from Test1) A
  left join
  (select * from Test2) B
  on Test1.COL3=Test2.COL5 and Test1.Time=Test2.TIME
  )  
  quit;

并且我没有得到所需的输出,即使两个数据集中的TIME列长度相同,格式和格式

我的结果是

  Final

  RowID  COL2      COL3     DATE         TIME      AMOUNT     RowID    COL2                 COL3             COL4            TIME    COL5    Amount

  3330  123456     123      20110523    14.14.50    2.00         .           .                 .              .           .     .    .
  3330  334567     123      20110523    19.13.34    2.00         .           .                 .              .           .     .     .
  3330  889789     123      20110523    20.01.11    2.00         .           .                 .              .          .      .     .
   3330 45678      1643     20110523    06.53.05    6.00         .           .                 .              .           .      .     .

I do not understand what is wrong. 

2 个答案:

答案 0 :(得分:0)

14.14.50 ne 14:14:50

修复您的格式,或使用INPUT将它们都设为数字。

答案 1 :(得分:0)

尝试使用

PROC sql;
CREATE TABLE final AS 
SELECT    * 
FROM      ( 
          ( 
                 SELECT * 
                 FROM   test1) A 
LEFT JOIN 
          ( 
                 SELECT * 
                 FROM   test2) B 
ON        A.col3=B.col5 
AND       Replace(A.time,':','.')=Replace(B.time,':','.') )
quit;