【问题标题】:how to find a missing case using proc sql in sas?如何在 sas 中使用 proc sql 查找丢失的案例?
【发布时间】:2023-03-02 23:06:01
【问题描述】:

我想在 sas 中使用 proc sql 来识别案例或记录​​是否缺少某些信息。我有两个数据集。一个是整个数据收集的记录,显示访问期间收集了哪些表格。第二个是在访问期间应该收集哪些表格的规范。我尝试了很多选项,包括使用not in 的数据步骤和sql 代码,但无济于事......

示例数据如下


*****  dataset crf is a listing of all forms that have been filled out at each visit ;
*****  cid is an identifier for a study center  ;
*****  pid is an identifier for a participant  ;

data crf;
  input visit cid pid form ;
cards;
1 10 101 10
1 10 101 11
1 10 101 12
1 10 102 10
1 10 102 11
2 10 101 11
2 10 101 13
2 10 102 11
2 10 102 12
2 10 102 13
;
run;


*****  dataset crfrule is a listing of all forms that should be filled out at each visit  ;
*****  so, visit 1 needs to have forms 10, 11, and 12 filled out  ;
*****  likewise, visit 2 needs to have forms 11 - 14 filled out  ;

data crfrule;
  input visit form ;
cards;
1 10
1 11
1 12
2 11
2 12
2 13
2 14
;
run;


*****  We can see from the two tables that participant 101 has a complete set of records for visit 1 ;
*****  However, participant 102 is missing form 12 for visit 1 ;
*****  For visit 2, 101 is missing forms 12 and 14, whereas 102 is missing form 14  ;


*****  I want to be able to know which forms were **NOT**  filled out by each person at each visit (i.e., which forms are missing for each visit)  ;


*****  extracting unique cases from crf ;
proc sql;
  create table visit_rec as
    select distinct cid, pid, visit
      from crf;
quit;



*****  building the list of expected forms by visit number ;
proc sql;
  create table expected as 
    select  x.*,
            y.*

    from visit_rec as x right join crfrule as y
      on x.visit = y.visit

    order by visit, cid, pid, form;
quit;


*****  so now I have a list of which forms that **SHOULD** have been filled out by each person  ;

*****  now, I just need to know if they were filled out or not...  ;

我一直在尝试的策略是将expected 合并回crf 表中,并带有一些指示每次访问缺少哪些表单。

理想情况下,我想生成一个包含以下内容的表:visit、cid、pid、missing_form

非常感谢任何指导。

【问题讨论】:

  • 到目前为止,我已经尝试了许多版本的this answer
  • 这些都是很好的答案!

标签: sql sas missing-data


【解决方案1】:

EXCEPT 会做你想做的事。我不一定知道这通常是最有效的解决方案(如果您在 SAS 中执行此操作,几乎可以肯定不是),但鉴于您到目前为止所做的,这确实有效:

create table want as
    select cid,pid,visit,form from expected
    except select cid,pid,visit,form from crf
;

请小心 EXCEPT - 它非常挑剔(请注意 select * 不起作用,因为您的表格的顺序不同)。

【讨论】:

    【解决方案2】:

    我建议使用嵌套查询,也可以分两步完成。这个呢:

    proc sql;
       create table temp as
       select distinct c.*
            , (d.visit is null and d.form is null and d.pid is null) as missing_form 
       from (
          select distinct a.pid, b.* from
          crf a, crfrule b
          ) c 
       left join crf d
       on     c.pid = d.pid 
          and c.form = d.form 
          and c.visit = d.visit
       order by c.pid, c.visit, c.form
       ;
    quit;
    

    它为您提供了一个列表,其中包含 pid、表单、访问和一个布尔值的所有可能(即预期)组合,指示它是否存在。

    【讨论】:

    • +1 稍作修改以将别名添加到 ORDER BY 列,但除此之外是一个非常好的答案!
    【解决方案3】:

    您可以使用左连接并使用 where 子句过滤掉右表中缺少记录的记录。

     select 
      e.* 
     from
      expected e left join 
      crf c on
       e.visit = c.visit and 
       e.cid = c.cid and
       e.pid = c.pid and
       e.form = c.form
     where c.visit is missing
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2019-09-24
      相关资源
      最近更新 更多