使用 forward/backward 方法从上一行创建缺失的行
Creating missing rows from previous row with forward/backward method
假设您有一个 table,其中包含用户名、计数器和每个计数器的分数。
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
我试图用 lag
和 if 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;
我想不出解决办法。
我认为这是一个前瞻性问题。您可以合并 firstobs=2 以向前看下一条记录的计数器值是多少。
下面使用了一个我认为是从 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;
假设您有一个 table,其中包含用户名、计数器和每个计数器的分数。
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
我试图用 lag
和 if 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;
我想不出解决办法。
我认为这是一个前瞻性问题。您可以合并 firstobs=2 以向前看下一条记录的计数器值是多少。
下面使用了一个我认为是从 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;