【问题标题】:Count number of observations and their percentages in SAS计算 SAS 中的观察次数及其百分比
【发布时间】:2015-02-10 02:06:23
【问题描述】:

我是 SAS 新手,所以在这里寻找建议/想法..

基本上我想创建包含观察次数、特定组百分比和平均值的汇总表/报告。

考虑以下示例:

A/C No   Status   DaysToStatus
1        Suspend  10
2        Blocked  20
3
4        Suspend  20

我的预期输出/报告类似于:

Average Days To Suspend    Average Days to Blocked    % Suspend    %Blocked
15                         20                         50%          25%

我可以使用 Proc Report 功能轻松计算平均值,但即使使用 Compute 功能,我也很难整合观察结果及其百分比。

对我如何做这件事有什么想法吗?

【问题讨论】:

    标签: sas


    【解决方案1】:

    您尚未发布代码,因此方法可能与您的不同。我认为您不需要compute。使用三列构建您的报告:状态(组)、daystostatus 平均值和用于计算行数的 pctn 统计数据。使用proc report 语句中的missing 选项将百分比基于包括缺失状态在内的总数。

    proc report data=yourdata missing nowd;
      column status daystostatus pctn;
      define status / group;
      define daystostatus / analysis mean 'Avg days to status';
      define pctn / 'Pct of total' format=percent10.;
    run;
    

    【讨论】:

    • 比我建议的更好的解决方案。谢谢。
    【解决方案2】:

    先准备数据

            data have;
            Number = 1;
            Status = 'Suspend';
            DaysToStatus = 10;
            output;
            Number = 2;
            Status = 'Blocked';
            DaysToStatus = 20;
            output;
            Number = 3;
            Status = '';
            DaysToStatus =.;
            output;
            Number = 4;
            Status = 'Suspend';
            DaysToStatus =20;
            output;
            run;
    

    这应该适合你:

                data want;
                set have end = eof;
                if Status='Suspend' then do;
                    sum_suspend+DaysToStatus;
                    count_suspend+1;
                end;
                else if status='Blocked' then do;
                    sum_blocked+DaysToStatus;
                    count_blocked+1;
                end;
                total_sum+DaysToStatus;
                total_count = _n_;
                Average_Days_To_Suspend = sum_suspend / count_suspend;
                Average_Days_To_blocked = sum_blocked / count_blocked;
                percent_suspend = count_suspend/total_count;
                percent_blocked = count_blocked/total_count;
                if eof;
                drop n status number DaysToStatus sum_: count_: total_:;
                run;
    

    【讨论】:

      【解决方案3】:

      proc sql;

      选择 sum(当状态 eq 'Suspend' 然后 DaysToStatus else 0 结束时的情况)/sum(当状态 eq 'Suspend' 然后 DaysToStatus else 0 结束时的情况) as
      avg_days_to_suspend,

      sum(当状态 eq 'Blocked' 然后 DaysToStatus else 0 end)/ 总和(当状态 eq 'Blocked' 然后 DaysToStatus else 0 结束时的情况)作为
      avg_days_to_blocked,

      总和(当状态 eq 'Suspend' 然后 DaysToStatus else 0 时的情况
      end)/sum(DaysToStatus) as percent_suspend format = percent7.0,

      总和(当状态 eq 'Blocked' 然后 DaysToStatus else 0 时的情况
      end)/sum(DaysToStatus) as percent_blocked 格式 = percent7.0

      来自 work.in_ds; /* 用你自己的数据集替换 work.in_ds */

      退出;

      【讨论】:

        猜你喜欢
        • 2016-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多