Solved
Contributor
Posts: 46

cumulative totals

Hi All ,

i have the below data set

 Obs famid year faminc 1 1 96 40000 2 1 97 40500 3 1 98 41000 4 2 96 45000 5 2 97 45400 6 2 98 45800 7 3 96 75000 8 3 97 76000 9 3 98 77000

and i want to get the cumulative totals   as mentioned in column  sum:

 Obs famid year faminc Sum 1 1 96 40000 40000 2 1 97 40500 40500 3 1 98 41000 41000 4 2 96 45000 85000 5 2 97 45400 85900 6 2 98 45800 86800 7 3 96 75000 160000 8 3 97 76000 161900 9 3 98 77000 163800

Accepted Solutions
Solution
‎04-14-2015 08:42 PM
Posts: 1,147

Re: cumulative totals

proc sort data=have;

by year;

run;

data want;

set have;

by year;

retain sum;

if first.year then sum=faminc;

else sum+faminc;

run;

proc sort data=want;

by famid year;

run;

Thanks,

Jag

Thanks,
Jag

All Replies
Solution
‎04-14-2015 08:42 PM
Posts: 1,147

Re: cumulative totals

proc sort data=have;

by year;

run;

data want;

set have;

by year;

retain sum;

if first.year then sum=faminc;

else sum+faminc;

run;

proc sort data=want;

by famid year;

run;

Thanks,

Jag

Thanks,
Jag
Frequent Contributor
Posts: 115

Re: cumulative totals

Hi

data have;
infile datalines dsd dlm='09'x truncover;
input famid :1. year :2. famic 5.;
datalines;
1 96 40000
1 97 40500
1 98 41000
2 96 45000
2 97 45400
2 98 45800
3 96 75000
3 97 76000
3 98 77000
;

data want;
array sumlook(3) _temporary_;
call missing(of sumlook{*});

do _n_=1 by 1 until(last.famid);
set have;
by famid;
array summing(3) _temporary_;
sumlook(_n_)=famic;
summing(_n_)+sumlook(_n_);
end;

do _n_=1 by 1 until(last.famid);
set have;
by famid;
sum=summing(_n_);
output;
end;
run;

Regards,

Naveen Srinivasan

L&T Infotech

Super User
Posts: 10,787

Re: cumulative totals

Is that always three obs for each famid group ?

```data have;
infile datalines   truncover;
input famid  year  famic ;
datalines;
1 96 40000
1 97 40500
1 98 41000
2 96 45000
2 97 45400
2 98 45800
3 96 75000
3 97 76000
3 98 77000
;
run;
data want;
set have;
array x{0:2} _temporary_;
x{mod(_n_,3)}+     famic;
sum=x{mod(_n_,3)};
run;

```

Xia Keshan

🔒 This topic is solved and locked.