SUM USING GROUP BY IN SAS

In this post, I will discuss about “How to perform sum function based on particular column in SAS”. In one of my blog , I have already explained about Sorting in SAS using Employee Data. Once sorting is done based on the Emp_id, I will sum the salary the based on the emp_id. Below is the code for the same.

DATA EMP_SUM_DET(drop=SALARY);
SET EMP_DET;
BY EMP_ID;
if First.EMP_ID then TotalSalary=0;
TotalSalary+SALARY;
if Last.EMP_ID;
run;


Here, we are using If, first and last function . For each employee, we are setting the intial TotalSalary=0 .
Here , another interesting fact is drop function(drop=SALARY) as we don’t need SALARY field in the output.
Once you executes the below code, below output will appear.

Related posts