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

问题描述 投票:0回答:1

我需要合并帮助。我有两个表如下─如表1

ID      ID1     ID2     ID3     ID4     ID5
1005    2005    3005    4005    5005    7105
3005    4005    5005    7105        
4005    5005    7105            
5005    7105                
2005    3005    4005    5005    7105    
7105                    

表2

ID      Names
1005    John
3005    Rick
4005    Sam
5005    Harry
2005    Mary
7105    Deena

我需要一种有效的方式与列合并表1与表2,我可以在不同的datasteps合并,但有什么办法,我可以做更有效的方法是什么?

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;

结果,我想所有的列(examplebelow):

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                                       

谢谢!

merge sas
1个回答
1
投票

下面是格式为基础的解决方案:

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;这会使得基于合并的解决方案小事,虽然可能比使用的格式仍然较慢。

© www.soinside.com 2019 - 2024. All rights reserved.