我习惯于在一个组内创建计数变量,每次使用以下计数值+1:
data objective ;
set eg ;
count + 1 ;
by id age ;
if first.age then count = 1 ;
run ;
但是我想做 reverse ,即每个id组中第一个age值的值为10,每个后续行的值为-1,前一行的值为-1 :
data eg ;
input id age desire ;
cards;
1 5 10
1 4 9
1 3 8
1 2 7
1 1 6
2 10 10
2 9 9
2 8 8
2 7 7
2 6 6
2 5 5
2 4 4
2 3 3
2 2 2
2 1 1
3 7 10
3 6 9
3 5 8
3 4 7
3 3 6
3 2 5
3 1 4
;
run;
data objective ;
set eg ;
count - 1 ;
by id age ;
if first.age_ar then count = 10 ;
run ;
是否有办法执行此操作,因为count-1
无法识别。
答案 0 :(得分:2)
您可以在不使用retain的情况下添加-1,如下所示:
data objective;
set eg;
count + -1;
by id descending age;
if first.id then count = 10;
run;
答案 1 :(得分:1)
试试这个(请参阅代码中的注释以获得解释):
data objective ;
retain count 10; /*retain last countvalue for every observation, 10 is optional as initial value*/
set eg ;
count=count - 1 ; /*count -1 does not work, but count=count-1 with count as retainvariable*/
by id age notsorted;/*notsorted because age is ordered descending*/
if first.id then count = 10 ;/*not sure why you hade age_ar here, should be id to get your desired output*/
run ;
输出: