我正在尝试创建一个表来计算各个办事处每个职位的出现次数。
所以,如果我的数据如下:
Office Position
A Manager
A Supervisor
A Entry Level
A Entry Level
B Manager
B Entry Level
我希望我的代码返回:
Office Managers Supervisors EntryLevel
A 1 1 2
B 1 0 1
我的代码如下。问题是此代码计算总发生次数,而不是每个办公室的唯一计数。结果如下
A 2 1 3
B 2 1 3
CREATE TABLE OfficeTest AS
SELECT DISTINCT Office,
(Select COUNT(Position) FROM OfficeData WHERE Make_Name = 'Manager') as Managers,
(Select COUNT(Position) FROM OfficeData WHERE Make_Name = 'Supervisor') as Supervisors,
(Select COUNT(Position) FROM OfficeData WHERE Make_Name = 'Entry Level') as EntryLevel
FROM OfficeData
GROUP BY Office;
有关如何解决此问题的任何想法?
答案 0 :(得分:1)
我能想到的最简单的方法就是这样:
SELECT Office,
COUNT(CASE WHEN Make_Name = 'Manager' THEN Position END) AS Managers,
COUNT(CASE WHEN Make_Name = 'Supervisor' THEN Position END) AS Supervisors,
COUNT(CASE WHEN Make_Name = 'Entry Level' THEN Position END) AS EntryLevel
FROM OfficeData
GROUP BY Office
COUNT
忽略MISSING
个值;如果Position
不是CASE
子句中指定的值,则会返回MISSING
值并且不会被计算在内。这样每种情况都只考虑您比较的Position
的值。
如评论中所述,另一个选择是转动表格。 SAS等效项是TRANSPOSE
过程。我没有SAS系统来使用它来创建和测试查询,但here's the documentation以防您想要查看它。
答案 1 :(得分:1)
只是为了清除Danny的评论,SUM代码看起来像:
proc sql;
CREATE TABLE want AS
SELECT office,
SUM( (position='Manager') ) as Managers,
SUM( (position='Supervisor') ) as Supervisors,
SUM( (position='Entry Level') ) as EntryLevel
FROM OfficeData
GROUP BY office
;quit;
(position='Manager')
位解析为0或1,具体取决于当前记录的true是否为真。我发现SUM版本更简洁,更清晰,但两者都适用于您的情况。此外,它可以轻松扩展到多个标准,例如(postion='Manager')*(sex='F')
,只计算女性经理。
答案 2 :(得分:0)
SUM与CASE语句应解决此问题。以下是参考代码
proc sql;
create table result as
select age
, sum(case sex when 'F' then 1 else 0 end) as Female
, sum(case sex when 'M' then 1 else 0 end) as Male
from sashelp.class
group by age;
quit;
proc print data=result;run;