简体   繁体   中英

SAS, calculate row difference

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;

I have two columns of data ID and Month. Column 1 is the ID, the same ID may have multiple rows (1-5). The second column is the enrolled month. I want to create the third column. It calculates the different between the current month and the initial month for each ID.

you can do it like that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM