Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grouping Summary Results

Similar presentations


Presentation on theme: "Grouping Summary Results"— Presentation transcript:

1 Grouping Summary Results

2 Group by clause proc sql; select Employee_Gender,
avg(Salary) as Average from orion.Employee_Payroll where Employee_Term_Date is missing group by Employee_Gender ; quit; Type answer here

3 Grouping Data Use the GROUP BY clause to: Classify the data into groups based on the values of one or more columns Calculate statistics for each unique value of the grouping columns

4 Grouping Data proc sql; select male,chd10yr, avg(chol) as avg_chol,
n(chol) as n_chol from s5238.chd5238 group by male, chd10yr ; quit; s103d08

5 Determine the total number of employees in each department.
proc sql; select Department, count(*) as Count from orion.Employee_Organization group by Department ; quit; s103d09

6 Calculate each male employee’s salary as a percentage of all male employees’ salaries. Display the employee ID, salary, and percentage in decreasing order of percentage. proc sql; title "Male Employee Salaries"; select Employee_ID, Salary format=comma12., Salary / sum(Salary) format=percent6.2 from orion.Employee_Payroll where Employee_Gender="M" and Employee_Term_Date is missing order by 3 desc ; quit; title; s103d10 I deliberately used this example AFTER the GROUP BY stuff to try to differentiate the way that PEOPLE talk about grouping things from the TECHNICAL SQL "GROUP BY" clause. My intent is to expose the students to more "business language" when framing problems that need to be solved with code. I expect that many of them will wonder “Where’s the group by?” I want to point out that when the boss asks for grouped output, it doesn't necessarily translate to GROUP BY in the SQL code. ...

7 Selecting Groups with the HAVING Clause
The WHERE clause is processed before a GROUP BY clause and determines which individual rows are available for grouping. The HAVING clause is processed after the GROUP BY clause and determines which groups will be displayed.

8 Selecting Groups with the HAVING Clause -- Display the names of the departments and the number of employees for departments with 25 or more employees proc sql; select Department, count(*) as Count from orion.Employee_Organization group by Department having Count ge 25 order by Count desc ; quit; s103d11

9 The payrollmaster data set.
libname misc "&path/MiscData"; proc contents data=misc.payrollmaster;run; proc freq data=misc.payrollmaster; tables jobcode; run;

10 Having clause libname misc "&path/MiscData"; proc sql;
select jobcode,avg(salary) as avg from misc.payrollmaster group by jobcode having Avg>40000 order by jobcode ; quit


Download ppt "Grouping Summary Results"

Similar presentations


Ads by Google