如何在SAS中根据匹配条件将一个表中的两列连接到另一张表

时间:2020-04-07 00:05:33

标签: sql join sas left-join proc-sql

我试图将表“ Update”中的“ Type2”和“ Measurement2”列连接到表“ Have”中。我希望列对齐,其中表“ Have”中的“ Subject1”列与表“ update”中的“ Subject2”列匹配,表“ Have”中的“ Procedure1”列与表“ Update”中的列“ Procedure2”匹配。提前。

data Have;
    input Subject1 Type1 :$12. Date1 &:anydtdte. Procedure1 :$12. Measurement1;
    format date yymmdd10.;
    datalines;

500   Initial    15 AUG 2017      Invasive    20 
500   Initial    15 AUG 2017     Surface      35   
428   Initial     3 JUL 2017     Outer        10 
765   Initial     20 JUL 2019     Other       19  
610   Initial     17 Mar 2018     Invasive    17 
;

data Update;
    input Subject2 Type2 :$12. Date2 &:anydtdte. Procedure2 :$12. Measurement2;
    format date yymmdd10.;
    datalines;

500   Followup   15 AUG 2018     Invasive     54 
428   Followup    15 AUG 2018      Outer      29 
765   Seventh     3 AUG 2018      Other       13 
500   Followup    3 JUL 2018      Surface     98 
610   Third       20 AUG 2019     Invasive    66  
;

2 个答案:

答案 0 :(得分:0)

您是否只是在寻找两个表之间的联接?

Select distinct have.*, update.type2, update.measurement2 
from have
left join update
on
have.subject1 = update.subject2
and have.procedure1 = update.procedure2

答案 1 :(得分:0)

根据MERGE语句中命名的 group 变量,使用BY根据关键字(您的主题和过程)组合两个数据集。这两个数据集都需要相同的BY变量。

示例代码:

  • MERGE需要排序的数据,因此必须首先发生。
  • 数据集选项rename=用于为BY语句创建公用名。
proc sort data=Have;    by Subject1 Procedure1;
proc sort data=Updates; by Subject2 Procedure2;

data combined;
  * trick: force these variables to be first two columns in output data set;
  retain subject procedure;

  merge
    have    (rename=(subject1=subject procedure1=procedure))
    updates (rename=(subject2=subject procedure2=procedure))
  ;
  by subject procedure;
run;

enter image description here

示例数据:

data Have;
attrib
  Subject1 length=8
  Type1 length=$12
  Date1 informat=anydtdte. format=yymmdd10.
  Procedure1 length=$12
  Measurement1 length=8
;
input 
Subject1& Type1&   Date1&       Procedure1& Measurement1&; datalines;
500       Initial  15 AUG 2017  Invasive    20 
500       Initial  15 AUG 2017  Surface     35   
428       Initial   3 JUL 2017  Outer       10 
765       Initial  20 JUL 2019  Other       19  
610       Initial  17 Mar 2018  Invasive    17 
;

data Updates;
attrib
  Subject2 length=8
  Type2 length=$12
  Date2 informat=anydtdte. format=yymmdd10.
  Procedure2 length=$12
  Measurement2 length=8
;
input 
Subject2& Type2&    Date2&       Procedure2& Measurement2&; datalines;
500       Followup  15 AUG 2018  Invasive    54 
428       Followup  15 AUG 2018  Outer       29 
765       Seventh    3 AUG 2018  Other       13 
500       Followup   3 JUL 2018  Surface     98 
610       Third     20 AUG 2019  Invasive    66  
;