How can I calculate the cumulative sum by a group variable in SAS?

sas

#1

Hi,

Please look at the below snapshot:

Above, I want to calculate the sum of total salary by Gender (Male/ Female). Please help me with the methods to create it.

Regards,
Steve


#2

Hi Steve,

After importing the file you can use this piece of code:

proc sort data = salary;
by Gender;
run;

data sal_by_gender(keep = total gender);
set salary;
by Gender;
if first.gender = 1 then total = 0;
total+sal;
if last.gender then output;
run;

To get a better understanding of first. and last. used here,please refer to:

http://www.analyticsvidhya.com/blog/2015/04/first-last-sas

Also is there any specific reason you don’t want to use proc sql,as that would have been much shorter than using first. and last. in this case.