【问题标题】:PROC SQL MERGE MISMATCHPROC SQL 合并不匹配
【发布时间】:2020-05-30 16:22:47
【问题描述】:

ATTACHED SCREENSHOT OF DESIRED OUTPUT需要的条件是 “A 中的主题 = B 中的主题 和 A NE的VISIT(不等于)B的VISIT"

我想通过使用 Proc SQL 过程从下表 A 和 B 中找到确切的不匹配和缺失的 VISIT,有人可以帮我吗?

表 A

SUBJECT Test    VISIT
1001    ABCB    1
1001    ABCD    2
1001    ABCD    3
1001    ABCD    5

表 B

SUBJECT Test    VISIT1
1001    ABCD    2
1001    ABCD    1
1001    ABCD    4

预期输出:

SUBJECT Test    VISIT   VISIT1
1001    ABCD    3
1001    ABCD    5    
1001    ABCD            4

访问 3 和 5 出现在数据集 A 中,不在 B 中,访问 4 出现在 DATASET2 中,不在数据集 A 中,就像 WISE 数据集代码-

DATA A;
  LENGTH SUBJECT 8 Test $10 visit 8;
  INPUT SUBJECT Test $ visit ;
  DATALINES;
  1001 ABCD 1
  1001 ABCD 2
  1001 ABCD 3
  1001 ABCD 5
;
RUN;

DATA B;
  LENGTH SUBJECT 8 Test $10 visit1 8;
  INPUT SUBJECT Test $ visit1 ;
  DATALINES;
  1001 ABCD 2
  1001 ABCD 1
  1001 ABCD 4
;
RUN;

提前致谢!

我尝试的代码如下(但没有按预期工作)-

    ****************(VISIT ) in A and not in B****;
proc sql;
create table SS1 as
select distinct a.* FROM
A a where a.visit not in(select s.visit1 from B s WHERE A.SUBJECT = S.SUBJECT );

create  table   INRAVE as
select * from SS1 A
left join 
B B
on a.subject=b.SUBJECT  and a.VISIT NE b.VISIT1
where   b.SUBJECT is not  null  
;
quit;
****************VISIT in B and not in A****;
proc sql;
create table SS2 as
select distinct a.* from 
B a where a.VISIT1 not in(select S.VISIT from A s WHERE A.SUBJECT = S.SUBJECT );

create  table   INVENDOR as
select * from SS2 A
left join 
A B
on a.subject=b.SUBJECT  and a.VISIT1 NE b.VISIT 
where   b.SUBJECT is not  null  
;
quit;

data ALL;;
set inrave invendor;
where subject=subject ;
RUN;

【问题讨论】:

  • 那么你尝试了什么 SQL 代码?
  • 嗨,汤姆,我已经添加了代码

标签: sas sas-macro proc-sql


【解决方案1】:

看来你对SQL很了解,不如试试union all,就像这样:

proc sql noprint;
    create table C as 
    select *, 'A' as Source from A
    where catx('@',SUBJECT,Test,visit) not in (
        select distinct catx('@',SUBJECT,Test,visit1) from B 
    )
    union all corr 
    select *, 'B' as Source from B(rename=VISIT1=VISIT)
    where catx('@',SUBJECT,Test,visit) not in (
        select distinct catx('@',SUBJECT,Test,visit) from A 
    )
    ;

    create table D(drop=TmpVISIT Source) as 
    select *,
        case when Source = 'B' then . else TmpVISIT end as VISIT,
        case when Source = 'B' then TmpVISIT else . end as VISIT1
    from C(rename=VISIT=TmpVISIT);
quit;

我从数据集 A 中获取所有 obs,在数据集 B 中不重复,并使用数据集 B 做相反的事情。
好吧,我也得到了另一个解决方案,更短:

proc sql noprint;
    select catx('@',SUBJECT,Test,visit) into :Ununique separated by '" "' from (
        select * from A union all select * from B(rename=visit1=visit)
    )
    group by SUBJECT, Test, visit
    having count(*) > 1;
quit;

data D;
    set A B;
    if catx('@',SUBJECT,Test,coalesce(visit1,visit)) in ("&Ununique") then delete;
run;

然而,这种方法受到宏变量最大长度的限制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多