假设您有一个包含每个计数器的用户名,计数器和分数的表。
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;
我无法提出解决方案。
答案 0 :(得分:1)
我认为这是一个前瞻性的问题。您可以与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;