2017-03-21 1 views
0

각 카운터에 대한 사용자 이름, 카운터 및 점수가있는 테이블이 있다고 가정하십시오.앞/뒤로 방법으로 이전 행에서 누락 된 행 만들기

data have; 
input user $ counter score; 
cards; 
A 1 50 
A 3 30 
A 6 90 
B 1 20 
B 4 20 
; 
run; 

일부 점수는 일부 카운터에서 누락되었으며 이전 카운터와 동일한 점수를 지정하려고합니다. 결과는 다음과 같이 표시됩니다 그래서 :

A 1 50 
A 2 50 
A 3 30 
A 4 30 
A 5 30 
A 6 30 
B 1 20 
B 2 20 
B 3 20 
B 4 20 

나는 lagif first.user then 그것을 해결하기 위해 노력하지만, 아래와 같이 카운터 1 일 이후 카운터 3 점프 :

data have_new; 
set have; 
by user; 
if first.user then do; 
x = counter; 
y = score; 
end; 

else do; 
counter = x +1; 
score = y; 
end; 
run; 

것은 내가 올 수 없습니다 솔루션으로

답변

1

나는 이것을 미리보기 문제점으로 본다. firstobs = 2와 병합하여 다음 레코드의 counter 값이 무엇인지 미리 볼 수 있습니다.

다음은 Mark Keintz의 많은 지체 및 납 종이 (예 : http://support.sas.com/resources/papers/proceedings16/11221-2016.pdf)에서 배운 것입니다. BY 문과 함께 추가 SET 문을 사용하여 첫 번째 문을 작성합니다. 그리고 마침내. 변수.

data want; 

    *This SET statement with BY statement is just to have by group processing; 
    set have(keep=user); 
    by user; 

    *Look ahead; 
    merge have have(firstobs=2 keep=counter rename=(counter=_NextCounter)); 

    output; 

    *If there is a gap between the counter of this record and the next counter; 
    *increment the counter and output; 
    if last.user=0 then do while(counter ne _NextCounter-1); 
    counter=counter+1; 
    output; 
    end; 

    drop _:; 
run; 
+0

완전히 작동합니다. 고마워. – user3714330

+0

나는 당신을 위해 하나 더 비슷한 질문을했습니다. [link] (http://stackoverflow.com/questions/42961591/filling-in-missing-values-withward- backward-method-with-lag-in-sas)를 확인하십시오. 불행히도 마지막 단계에서 붙어 있습니다. – user3714330