【问题标题】:cumulative frequency in SAS DatasetSAS 数据集中的累积频率
【发布时间】:2014-01-17 04:16:00
【问题描述】:

我的数据集如下所示:

 Customer Sales
        1    15
        2    14
        3    13
        4    11
        5    12
        6    18
        7    21

我需要按销售额百分比对客户进行排序,然后将它们分配到“高”、“中”、“低”桶中......

 Customer Sales  %Sales
        7    21     20%
        6    18     17%
        1    15     14%
        2    14     13%
        3    13     13%
        5    12     12%
        4    11     11%

并且桶需要基于累积频率:

 Customer Sales %Sales CumFreq Bucket
        7    21    20%     20%   High
        6    18    17%     38% Medium
        1    15    14%     52% Medium
        2    14    13%     65% Medium
        3    13    13%     78%    Low
        5    12    12%     89%    Low
        4    11    11%    100%    Low

因此,正如您所见,前 33% 的销售额中的任何人都将是高位,中间 33% 将是中位,而后 33% 将是低位

【问题讨论】:

  • 我知道我们可以使用来自 proc freq 的输出数据集的 Cumul Freq,但不确定它是否完全符合我的目的

标签: sas cumulative-frequency


【解决方案1】:

所以你需要对列求和,对数据集进行排序,然后计算累积百分比。使用自定义格式创建您的 Bucket 列。

data sales;
input  Customer Sales;
datalines;
        1    15
        2    14
        3    13
        4    11
        5    12
        6    18
        7    21
;
run;

proc sort data=sales;
by descending sales ;
run;

proc sql noprint;
select sum(sales) format=best32. into :s from sales;
quit;

proc format;
value pctSales
    0-.33='High'
    .33-.67='Medium'
    .67-1='Low';
run;

data sales;
set sales;
retain total 0;
format pctSales percent8.2;
total = total + sales;
pctSales = total/&s;
bucket = put(pctSales,pctSales.);
drop total;
run;

【讨论】:

    【解决方案2】:
    /*untested: don't have access to SAS right now*/
    
    PROC SQL noprint;
    /*1st get the total no. of sales and stick it into a macro variable*/
    select sum(sales) into: TotalSales
    from someCustomertable;
    
    create table topCustomers as 
    select 
    a.Customer
    , a.sales
    , a.sales/&TotalSales as salesPerc format=percent11.2
    , case
        when calculated salesPerc <=1/3 then "High"
        when calculated salesPerc <=2/3 then "Medium"
        else "Low"
      end as Bucket
    from someCustomertable as a
    order by 3
    QUIT;
    

    【讨论】:

      猜你喜欢
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多