计算列百分比sas

时间:2016-01-20 03:50:00

标签: sas percentage

我有以下数据集:

Date        Occupation      Tota_Employed
1/1/2005      Teacher         45
1/1/2005      Economist       76
1/1/2005      Artist          14
2/1/2005      Doctor          26
2/1/2005      Economist       14
2/1/2005      Mathematician   10

and so on until November 2014

我想要做的是计算一个占用百分比的列,这样我的数据将如下所示:

Date        Occupation      Tota_Employed   Percent_Emp_by_Occupation
1/1/2005      Teacher         45               33.33
1/1/2005      Economist       76               56.29
1/1/2005      Artist          14               10.37
2/1/2005      Doctor          26               52.00
2/1/2005      Economist       14               28.00
2/1/2005      Mathematician   10               20.00

其中percent_emp_by_occupation的计算方法是将total_employed除以每个日期(月和年)除以每个职业的总和得到百分比:

老师的例子:(45/135)* 100,其中135是45 + 76 + 14的总和

我知道我可以通过proc制表获得一个表,但是想知道是否还有通过另一个程序,特别是因为我想将它作为一个单独的数据集。

这样做的最佳方法是什么?提前致谢。

3 个答案:

答案 0 :(得分:1)

从日期中提取月份和年份并创建一个键:

.dummy {
  margin-top: 100%;
}

.row {
  display:flex;
  flex-wrap:wrap;
  justify-content:center;
}

.item-item {
  flex:0 0 277px;
  margin: 15px 0 0 1%;
  text-align: center;
  padding-top: calc(50% - 30px);
  border: solid black 5px;
}

在月份级别累计总数:

data ds;
set ds;
month=month(date);
year=year(date);
key=catx("_",month,year);
run;

使用每月总计更新原始数据:

Proc sql;
create table month_total as
select key,sum(total_employed) as monthly_total
from ds
group by key;
quit;

这将导致以下数据集:

日期占用Tota_Employed monthly_total 1/1/2005老师45 135 1/1/2005经济学家76 135 1/1/2005艺术家14 135

最后将百分比计算为:

Proc sql;
create table ds as
select a.*,b.monthly_total
from ds as a left join month_total as b
on a.key=b.key;
quit;

答案 1 :(得分:1)

你走了:

proc sql;
create table occ2 as 
select
    occ.*,
    total_employed/employed_by_date as percentage_employed_by_date format=percent7.1
from 
    occ     a
    join
    (select 
        date,
        sum(total_employed) as employed_by_date
    from occ
    group by date)  b
        on
        a.date = b.date
;
quit;

生成如下表格:

enter image description here

最后一个想法:您可以在一次传递数据时创建所需的所有计算总数。我查看了您之前询问过此数据的问题,并假设您使用proc means按日期和职业汇总了初始数据。您也可以在同一过程中按日期计算总计。我没有您的数据,所以我将使用每个SAS安装附带的sashelp.class数据集来说明这个概念。

在这个例子中,我想得到按性别和年龄划分的学生总数,但我也希望按性别划分学生总数,因为我会按性别计算学生的百分比。以下是如何汇总数据并获得2个不同级别摘要的计数。

proc summary data=sashelp.class;
class sex age;
types sex sex*age;
var height;
output out=summary (drop=_freq_) n=count;
run;

types语句标识了我的类变量的摘要级别。在这种情况下,我想要只是性别的数量,以及按年龄划分的性别数量。这是输出的样子。

enter image description here

_TYPE_变量标识摘要级别。性别总数为_TYPE_=2,而按性别划分的性别数为_TYPE_=3

然后用一个简单的SQL查询来计算性别中的百分比。

proc sql;
create table summary2 as
select
    a.sex,
    a.age,
    a.count,
    a.count/b.count as percent_of_sex format=percent7.1
from
    summary (where=(_type_=3))  a  /* sex * age  */
    join
    summary (where=(_type_=2))  b  /* sex  */
        on
        a.sex = b.sex
;
quit;

enter image description here

答案 2 :(得分:0)

答案是回顾过去几天您提出的有关相同数据的问题并研究这些答案。你的答案就在那里。

在您查看这些答案时,请花点时间感谢他们并给某人一张帮助您的支票。

相关问题