2017-03-21 6 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; 

いくつかのスコアは、一部のカウンタbeween不足している、とあなたは以前のカウンタと同じスコアを載せていきたいと思います。だから、結果は以下のようになります。

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

私はもう一つあなたのために同様の質問があります。 [リンク](http://stackoverflow.com/questions/42961591/filling-in-missing-values-withward-backward-method-with-lag-in-sas)を確認してください。私は残念ながら最後のステップで立ち往生しています。 – user3714330

関連する問題