代码之家  ›  专栏  ›  技术社区  ›  frank

如何基于sas中的特定列合并2个表

  •  -2
  • frank  · 技术社区  · 7 年前

    我想合并2个表,但出现了错误

    proc sql;
    select * from Table1
    outer union corr
    select * from table2;
    

    ERROR: The type of column EntryId from the left hand side of the OUTER UNION set operation is
           different from EntryId on the right hand side
    

    如果我理解正确并基于 UNION ALL two SELECTs with different column types - expected behaviour? ,第一列是不同的数据类型,无法继续使用union(这是真的)

    RecordID num label='RecordID' format=20. informat=20.
    and
    RecordID num label='RecordID' format=11. informat=11.
    

    但是,我想使用一个格式相同的列

    Pseu char(64) label='Pseu' format=$64. informat=$64.
    Pseu char(64) label='Pseu' format=$64. informat=$64.
    

    在每个表中,它们是第3列和第4列。

    有没有办法将这些表合并在一起,使用该列作为参考,而不是原始列?

    我试着没有用:

    proc sql;
    select * from Table1
    outer union corr
    select * from table2
    on Table1.Pseu=Table2.Pseu;
    
    ERROR: Found "on" when expecting ;
    

    它遵循上给出的外并集对应示例 http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473694.htm ,并基于我想要的:

    table1              
    R   y   p       
    1   A   100     
    2   B   101     
    3   R   102     
    
    table2              
    R   z   p       
    4   A   102     
    5   R   103     
    6   T   104     
    
    MERGED              
    p   R   y   R   z
    100 1   A       
    101 2   B       
    102 3   R   4   A
    103         5   R
    104         6   T
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   jarlh    7 年前

    可能是这样的:

    proc sql;
    select * from Table1
    outer union corr
    select p, r as r2, z from table2
    

    具有列r的列别名。

    使用常规联合:

    select p, r, y, null, null from Table1
    outer union corr
    select p, null, null, r as r2, z from table2
    
        2
  •  -1
  •   frank    7 年前

    我的搜索和jarlh提供的答案是正确的。

    问题是由于要联合的数据集中列的大小和数量引起的。我必须确保联合的数据集中没有重复的列名(在我总共600列中,有些列有类似的名称),因此我必须重命名列