cumulative totals

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

cumulative totals

Hi All ,

i have the below data set

Obsfamidyearfaminc
119640000
219740500
319841000
429645000
529745400
629845800
739675000
839776000
939877000

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

ObsfamidyearfamincSum
11964000040000
21974050040500
31984100041000
42964500085000
52974540085900
62984580086800
739675000160000
839776000161900
939877000163800

Could any one please advise the options...


Accepted Solutions
Solution
‎04-14-2015 08:42 PM
Trusted Advisor
Posts: 1,137

Re: cumulative totals

Please try

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

View solution in original post


All Replies
Solution
‎04-14-2015 08:42 PM
Trusted Advisor
Posts: 1,137

Re: cumulative totals

Please try

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: 9,865

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 334 views
  • 9 likes
  • 4 in conversation