Merge SAS合并具有相同数据的多个列

Merge SAS合并具有相同数据的多个列,merge,sas,Merge,Sas,我需要帮助合并。我有两张桌子如下- 表1 表2 ID Names 1005 John 3005 Rick 4005 Sam 5005 Harry 2005 Mary 7105 Deena 我需要一种有效的方法来合并表1和表2中的列。我可以在单独的数据步骤中进行合并,但有没有一种方法可以更有效地进行合并 proc sql; create merge1 as select * from table1 a left join table2 b on a

我需要帮助合并。我有两张桌子如下- 表1

表2

ID      Names
1005    John
3005    Rick
4005    Sam
5005    Harry
2005    Mary
7105    Deena
我需要一种有效的方法来合并表1和表2中的列。我可以在单独的数据步骤中进行合并,但有没有一种方法可以更有效地进行合并

proc sql;
create merge1 as 
select *
from table1 a
left join table2 b on a.id = b.id;
quit;

proc sql;
create merge2 as 
select *
from merge1 a
left join table2 b on a.id = b.id;
quit;
我想要的所有列的结果(下面的示例):


谢谢

以下是基于格式的解决方案:

data table1;
  length id id1 id2 id3 id4 id5 8;
  infile datalines missover;
  input id id1 id2 id3 id4 id5;
  cards;
1005    2005    3005    4005    5005    7105
3005    4005    5005    7105        
4005    5005    7105            
5005    7105                
2005    3005    4005    5005    7105    
7105                    
;
run;

data table2;
  length id 8 names $ 10;
  input id names;
  cards;
1005    John
3005    Rick
4005    Sam
5005    Harry
2005    Mary
7105    Deena
;
run;

* Create a CNTLIN data set defining the required format;
data fmt_in;
  set table2;
  fmtname = 'names';
  start = id;
  label = names;
run;

* Run PROC FORMAT to generate the format from the CNTLIN data set;
proc format cntlin=fmt_in;
run;

* Apply the format to the input data set;
data out;
  set table1;
  namesID  = put(id, names.);
  namesID1 = put(id1, names.);
  namesID2 = put(id2, names.);
  namesID3 = put(id3, names.);
  namesID4 = put(id4, names.);
  namesID5 = put(id5, names.);
run;

这对于大型输入非常有效,因为它不需要多种排序。当然,一般来说,您的输入数据集
table1
应该标准化为高而薄,以便只有一列包含ID;这将使基于合并的解决方案变得微不足道,尽管可能仍然比使用格式慢。

您能指定输出应该是什么样子吗?我假设您希望将名为NAME、NAME1、NAME2等的列添加到表1中以创建输出-是这样吗?您必须使用合并/联接吗?创建从ID到名称的格式映射可能更容易编写且速度更快。我不需要联接…任何有效的方法都很好,但是,我需要在表上同时显示ID和名称您需要宽格式输出还是长格式输出(即仅3列:
ID
ID\u num
name
)可以接受吗?谢谢你的帮助!
ID    NamesID     ID1     NamesID1    ID2     NamesID2    ID3
1005    John     2005     Mary       3005     Rick       4005
3005    Rick     4005     Sam        5005     Harry      7105
4005    Sam      5005     Harry      7105     Deena 
5005    Harry    7105     Deena         
2005    Mary     3005     Rick       4005     Sam        5005
7105    Deena                                       
data table1;
  length id id1 id2 id3 id4 id5 8;
  infile datalines missover;
  input id id1 id2 id3 id4 id5;
  cards;
1005    2005    3005    4005    5005    7105
3005    4005    5005    7105        
4005    5005    7105            
5005    7105                
2005    3005    4005    5005    7105    
7105                    
;
run;

data table2;
  length id 8 names $ 10;
  input id names;
  cards;
1005    John
3005    Rick
4005    Sam
5005    Harry
2005    Mary
7105    Deena
;
run;

* Create a CNTLIN data set defining the required format;
data fmt_in;
  set table2;
  fmtname = 'names';
  start = id;
  label = names;
run;

* Run PROC FORMAT to generate the format from the CNTLIN data set;
proc format cntlin=fmt_in;
run;

* Apply the format to the input data set;
data out;
  set table1;
  namesID  = put(id, names.);
  namesID1 = put(id1, names.);
  namesID2 = put(id2, names.);
  namesID3 = put(id3, names.);
  namesID4 = put(id4, names.);
  namesID5 = put(id5, names.);
run;