SAS,计算行差异

问题描述 投票:-1回答:1
data test;
input ID month d_month;
datalines;
1 59 0 
1 70 11
1 80 21
2 10 0 
2 11 1
2 13 3
3 5  0
3 9  4
4 8  0
;
run;

我有两列数据ID和Month。第1列是ID,相同的ID可能有多行(1-5)。第二列是注册月份。我想创建第三列。它计算每个ID的当前月份和初始月份之间的差异。

sas row difference
1个回答
0
投票

你可以这样做。

data test;
input ID month d_month;
datalines;
1 59 0 
1 70 11
1 80 21
2 10 0 
2 11 1
2 13 3
3 5  0
3 9  4
4 8  0
;
run;

data calc;
    set test;
    by id;
    retain current_month;

    if first.id then do;
        current_month=month;
        calc_month=0;
    end;

    if ^first.id then do;
        calc_month = month - current_month ;
    end;
run;

KRS

© www.soinside.com 2019 - 2024. All rights reserved.