Not applicable
Posts: 0

# How to calculate cumulative variable by group?

DATA Temp;
INPUT Group Salary;
CARDS;
1 5000
1 6000
1 2100
2 1110
2 5000
2 5000
3 5000
3 5000
;
RUN;

Data Temp1;
set Temp;
Cum_salary + Salary;
RUN;

But how can i calculate cumulative salary by group?
Super Contributor
Posts: 3,176

## Re: How to calculate cumulative variable by group?

Have you looked at PROC FREQ with the OUT= option?

Scott Barry
SBBWorks, Inc.

proc freq cumulative frequency site:sas.com
Not applicable
Posts: 0

## Re: How to calculate cumulative variable by group?

Data Temp1;
set Temp;
by group;
if first.Group then Cum_salary=Salary;
else Cum_salary + Salary;
RUN;
Not applicable
Posts: 0

## Re: How to calculate cumulative variable by group?

Thanks~

But if set two group?

DATA Temp;
INPUT Group1 Group2 Salary;
CARDS;
1 A 5000
1 A 6000
1 B 2100
2 A 1110
2 B 5000
2 B 5000
3 A 5000
3 A 5000
;
RUN;

Data Temp1;
set Temp;
by Group1 Group2;
if first.Group1 & first.Group2 then cum_salary= salary;
else cum_salary+ salary;
run;
Not applicable
Posts: 0

## Re: How to calculate cumulative variable by group?

try this.

DATA Temp;
INPUT Group1 Group2 \$ Salary;
CARDS;
1 A 5000
1 A 6000
1 B 2100
2 A 1110
2 B 5000
2 B 5000
3 A 5000
3 A 5000
;
RUN;

proc sort data=temp;
by Group1 Group2;
run;

Data Temp1;
set Temp;
by Group1 Group2;
if first.Group2 then cum_salary= salary;
else cum_salary+ salary;
run;
Not applicable
Posts: 0