根据SAS中相似但不相等的列将两个表连接在一起

时间:2020-04-21 14:23:43

标签: sql join merge sas left-join

我试图将这两个表结合在一起。我希望这些列对齐,其中表“ enroll”中的“病人”列与表“ calendar”中的“病人ID”列匹配。 “患者”和“患者ID”相似但不相等。此外,表“注册”中的“已分配日期”列应与表“日历”中的“已跟踪日期”列匹配。由于这只是较大数据集的一小段,因此列应对齐。但是,在较大的数据集中可能不是这种情况,因此,我试图突出显示两个表中的列未对齐的位置。先感谢您。

我当前的工作代码给了我

错误:变量患者已被定义为字符和 数字。

data enroll;
    input patient status :$12. dateassigned &:anydtdte.;
    format date yymmdd10.;
    datalines;

500-001   enrolled    01-jan-2019      
500-002   enrolled    15-jan-2019     
500-003   removed     23-Jan-2019     
500-004   enrolled    05-feb-2019     
500-005   enrolled    17-feb-2019     
587-001   enrolled    20-feb-2019
587-002   enrolled    25-feb-2019
587-003   enrolled    03-mar-2019
594-001   enrolled    04-feb-2018
594-002   enrolled    09-feb-2018
648-001   enrolled    15-mar-2019
648-002   enrolled    22-mar-2019
648-003   enrolled    27-mar-2019
648-004   enrolled    30-mar-2019
;

data calendar;
    input visitnumber patientID :$12. datetracked &:anydtdte.;
    format date yymmdd10.;
    datalines;

500 500-001-rdf   01-jan-2019      
500 500-002-fgh   15-jan-2019     
500 500-003-ehd   23-Jan-2019     
500 500-004-ern   05-feb-2019     
500 500-005-qmd   17-feb-2019     
587 587-001-wcs   20-feb-2019
587 587-002-qlc   25-feb-2019
587 587-003-qhr   03-mar-2019
594 594-001-qwn   04-feb-2018
594 594-002-agj   09-feb-2018
648 648-001-wuf   15-mar-2019
648 648-002-qbf   22-mar-2019
648 648-003-olr   27-mar-2019
648 648-004-wmf   30-mar-2019
;


proc sort data=enroll;
by patient dateassigned;
run;

data calendar;
length patient $8.;
set calendar;
patient=substr(patientID,1,7);
dateassigned=datetracked;
run;

proc sort data=calendar;
by patient dateassigned;
run;

data want;
merge enroll(in=a) calendar(in=b);
by patient dateassigned;
if a and b;
run;

1 个答案:

答案 0 :(得分:0)

您要在哪个表中突出显示行是否不对齐? 我将使用proc sql语句,进行内部联接。

然后,您可以在A.注册或B.日历中突出显示是否存在未找到对齐的行:

/* Inner join to have view on aligned lines */
    proc sql;
      create table aligned as
      select a.*,
             b.*
      from enroll a inner join calendar b on a.patient = substr(b.patientID,1,7) and 
      a.dateassigned = b.datetracked;
    quit;

/* A.Except statement to have view on non aligned lines  - Enroll table*/

    proc sql;
      create table non_aligned_enroll as
      select * from enroll
      except 
      select a.* from enroll a inner join calendar b on a.patient = 
      substr(b.patientID,1,7) and a.dateassigned = b.datetracked;
    quit;

/* B.Except statement to have view on non aligned lines  - Enroll table*/

    proc sql;
      create table non_aligned_calendar as
      select * from calendar
      except 
      select b.* from enroll a inner join calendar b on a.patient = 
      substr(b.patientID,1,7) and a.dateassigned = b.datetracked;
    quit;