2013-01-24 6 views
1

케이스 또는 레코드에 정보가 누락되어 있는지 확인하기 위해 sas에서 proc sql를 사용하고 싶습니다. 나는 두 개의 데이터 세트를 가지고있다. 하나는 방문 중 수집 된 양식을 보여주는 전체 데이터 수집 기록입니다. 두 번째는 이 방문하는 동안 어떤 형태로을 수집해야하는지에 대한 명세입니다. 나는sas에서 proc 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을 병합하는 노력 해왔다 전략입니다 ... 데이터 단계와 아무 소용 not in를 사용하여 SQL 코드를 포함하여 많은 옵션을 시도 각 방문에 대해 누락 된 양식의 일부 표시등을 사용하여 crf 테이블로 다시 이동하십시오. , 모든지도가 대단히 감사합니다

CID, PID, missing_form를 방문하십시오

최적, 내가 가진 것 테이블을 생성하고 싶습니다.

+0

나는 [이 답변] (http://stackoverflow.com/questions/8946593/how-can-i-use-proc-sql-to-find-all-the-records의 여러 버전을 시도했다 - 유일한 - - 하나 - 테이블 -하지만 - 지금까지 내 시도에). –

+0

이들은 모두 훌륭한 해답입니다. –

답변

0

왼쪽 조인을 사용하고 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 
; 
2

EXCEPT는 원하는대로 할 것입니다. 이것이 가장 효율적인 솔루션이라는 것을 꼭 알 필요는 없습니다. (SAS에서이 작업을 수행하는 경우 거의 확실하지 않습니다.) 그러나 지금까지해온 작업을 감안할 때 작동합니다 :

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

EXCEPT와 조심하십시오 - 매우 까다 롭습니다 (select *는 작동하지 않습니다. 테이블의 순서가 다르기 때문에).

2

두 단계로 나뉘어 질 수있는 중첩 쿼리를 제안합니다. 무슨 일이에 대해 :

그것은 당신에게 PID, 양식의 모든 가능한 (즉 예상) 조합 목록을 제공
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; 

, 방문하고 부울이 존재하는지 여부를 나타내는.

+0

+1 'ORDER BY' 열에 별칭을 추가하기 위해 약간의 수정을가했지만 그렇지 않은 경우 매우 좋은 답변입니다! – BellevueBob

관련 문제