我对Proc sql很新,觉得我正撞在墙上。基本上,我想制作一些不同类型的小计,我一直在努力。我可以弄清楚如何在两个sql语句中执行它,但我无法弄清楚如何使用子查询将它们组合成一个语句,这似乎应该是可能的。
示例数据
*job, gender
*florist, female
*florist, female
*florist, male
*florist, male
*Manager, female
*Manager, female
*Manager, female
*Manager, male
*nurse, female
*nurse, female
*nurse, male
我想从我的代码中得到的是:
Job Title| Gender | All answers to gender | Job by gender
Florist | female | 7 | 2
Florist | male | 4 | 2
Manager | female | 7 | 3
Manager | male | 4 | 1
nurse | female | 7 | 2
nurse | male | 4 | 1
我当前的代码(分两步执行)是
proc sql;
create table newdata as
select gender, job,
count(gender) as GenderCount
from datasource where gender in ('f','m')
group by gender;
select * from newdata;
quit
proc sql;
create table newdata2 as
select *,
count(gender) as JobsByGender
from newdata
group by q1, q21;
select distinct * from newdata2 order by q21, q1;
quit;
答案 0 :(得分:1)
您可以加入子查询以获得预期结果:
proc sql;
create table newdata as
select bd.job,
bd.gender,
sq.count_gender,
count(bd.job) as JobsByGender
from basedata bd
inner join
(select gender,
count(gender) as count_gender
from base_data
group by gender) sq
on sq.gender = bd.gender
group by bd.job, bd.gender;
quit;
子查询创建一个看起来像
的数据集Gender | count_gender
---------------------
Male | 4
Female | 7
内部联接会根据on sq.gender = bd.gender
老实说,大部分只是通用SQL。如果您将来要做更多的proc sql语句,我强烈建议您查看可以在http://www.sqlcourse.com/和http://www.sqlcourse2.com/找到的教程
这些希望能让您理解您正在撰写的查询,而不是使用您不理解的示例代码。