Cumulative sum by emp_id in data step

Reply
Super Contributor
Posts: 436

Cumulative sum by emp_id in data step

May I request someone to help me writing a data step to get a cumilative sum by emp_id as below?

 

Have dataset:

 

emp_id  Salary

1            100

2            200

3            300

1            100

2            200

4            400

 

Want dataset:

 

emp_id  Cum_Salary

1            200

2            400

3            300

 

4            400

Contributor
Posts: 20

Re: Cumulative sum by emp_id in data step

data have;
input emp_id salary;
cards;
1 100
2 200
3 300
1 100
2 200
4 400
;
run;
proc sort data=have;
by emp_id;
run;
data want(drop=salary);
set have;
by emp_id;
if first.emp_id then cum_salary=0;
cum_salary+salary;
if last.emp_id;
run;

Super Contributor
Posts: 490

Re: Cumulative sum by emp_id in data step


data have;
input emp_id  Salary;
cards;
1 100
2 200
3 300
1 100
2 200
4 400
;run;


proc sort  data=have;
    by emp_id;
run;

data totalby (keep=emp_id cumilative_sum);
   set have;
   by emp_id;
   if First.emp_id then cumilative_sum = 0;
   cumilative_sum + Salary;
   if Last.emp_id;
run;
Super Contributor
Posts: 490

Re: Cumulative sum by emp_id in data step

Or 

proc sql;
	create table want as
   select emp_id , sum(salary) 
      from have
      group by emp_id;
      quit;
Valued Guide
Posts: 860

Re: Cumulative sum by emp_id in data step

data have;
input emp_id  Salary;
cards;
1            100
2            200
3            300
1            100
2            200
4            400
;

proc sql;
create table want as
select emp_id, sum(salary) as icheated
from have
group by emp_id;

Ask a Question
Discussion stats
  • 4 replies
  • 357 views
  • 0 likes
  • 4 in conversation