在 SAS 中使用基字段和基项目创建百分比

问题描述 投票:0回答:1

数据样本

data master;
    input coh $ status $ years count;
    datalines;
2012 Active 0 53
2012 Active 1 40
2012 Active 2 14
2012 Graduated 1 7
2012 Graduated 2 33
2013 Active 0 41
2013 Active 1 32
2013 Active 2 11
2013 Graduated 1 4
2013 Graduated 2 24
2014 Active 0 42
2014 Active 1 34
2014 Active 2 13
2014 Graduated 1 1
2014 Graduated 2 23
;
run;

我正在运行的脚本基于“years = 0”创建一个过程列表+百分比

proc tabulate data=master;
where pmajr = 'AIT' and years<9; 
TITLE1 "table1 ";
class coh  years status;
table coh*(status all), (years) *(count pctn<years>="%") /printmiss misstext='0';  *norow nocol nopercent nocum;
run;

这给了我一个行百分比,但是我真正需要的是根据年份“0”计算百分比。例如,第 0 年(计数 = 53)应为 100%。第 1 年(计数 = 40)百分比应为 75%。对于第 2 年(计数 = 14),百分比应为 26%,依此类推。

换句话说,我需要按行计算百分比,但基于“years =0”列。

请参阅随附的片段作为示例:此处

谢谢!

dataframe sas percentage tabulate
1个回答
0
投票

要计算百分比,只需将分子除以分母即可。看起来您想使用 YEARS=0 时的 COUNT 值作为每个 COH 的分母。

data have;
  input coh $ status $ years count;
datalines;
2012 Active 0 53
2012 Active 1 40
2012 Active 2 14
2012 Graduated 1 7
2012 Graduated 2 33
2013 Active 0 41
2013 Active 1 32
2013 Active 2 11
2013 Graduated 1 4
2013 Graduated 2 24
2014 Active 0 42
2014 Active 1 34
2014 Active 2 13
2014 Graduated 1 1
2014 Graduated 2 23
;

data want;
  merge have
        have(keep=coh years count rename=(years=_year count=year0)
             where=(_year=0))
  ;
  by coh  ;
  percent = divide(count,year0);
  drop _year ;
run;

结果

Obs    coh      status     years    count    year0    percent

  1    2012    Active        0        53       53     1.00000
  2    2012    Active        1        40       53     0.75472
  3    2012    Active        2        14       53     0.26415
  4    2012    Graduate      1         7       53     0.13208
  5    2012    Graduate      2        33       53     0.62264
  6    2013    Active        0        41       41     1.00000
  7    2013    Active        1        32       41     0.78049
  8    2013    Active        2        11       41     0.26829
  9    2013    Graduate      1         4       41     0.09756
 10    2013    Graduate      2        24       41     0.58537
 11    2014    Active        0        42       42     1.00000
 12    2014    Active        1        34       42     0.80952
 13    2014    Active        2        13       42     0.30952
 14    2014    Graduate      1         1       42     0.02381
 15    2014    Graduate      2        23       42     0.54762
© www.soinside.com 2019 - 2024. All rights reserved.