【问题标题】:Calculating proportion and cumulative data in SAS在SAS中计算比例和累积数据
【发布时间】:2018-05-22 10:43:16
【问题描述】:

我有一个名为 stores 的数据集。我想提取 total_sales(retail_price), 各店铺销售额占比累计销售额占比 SAS。

样本数据集:- 商店

Date       Store_Postcode   Retail_Price    month   Distance
08/31/2013  CR7 8LE              470           8    7057.8
10/26/2013  CR7 8LE              640           10   7057.8
08/19/2013  CR7 8LE              500           8    7057.8
08/17/2013  E2 0RY               365           8    1702.2
09/22/2013  W4 3PH               395.5         12   2522
06/19/2013  W4 3PH               360.5         6    1280.9
11/15/2013  W10 6HQ              475           12   3213.5
06/20/2013  W10 6HQ              500           1    3213.5
09/18/2013  E7 8NW               315           9    2154.8
10/23/2013  E7 8NW               570           10   5777.9
11/18/2013  W10 6HQ              455           11   3213.5
08/21/2013  W10 6HQ              530           8    3213.5

我试过的代码:-

Proc sql;                                                                                                                               

Create table work.Top_sellers as                                                                                                         

Select Store_postcode as Stores,SUM(Retail_price) as Total_Sales,Round((Retail_price/Sum(Retail_price)),0.01) as               
Proportion_of_sales                                                                                                                     

From work.stores                                                                                                           

Group by Store_postcode                                                                                                                 
Order by total_sales;                                                                                                                   

Quit;  

我不知道如何在 proc sql 中计算累积变量... 请帮助我改进我的代码!

【问题讨论】:

  • 你是如何定义累计销售比例的?是每家店还是整体?这很重要,因为否则商店的顺序会影响结果,如果顺序会影响结果,那么在大多数情况下它并不是一个真正可用的指标。​​
  • 我要的是店铺@Reeza的累计销售额比例

标签: sas proc-sql


【解决方案1】:

在 SQL 中计算累积结果需要数据具有明确的唯一有序键,并且查询涉及具有“三角”条件的自反连接。

data have;
  do id = 100 to 120;
    sales = ceil (10 + 25 * ranuni(123));
    output;
  end;
run;

proc sql;
  create table want as
  select 
    have1.id
  , have1.sales
  , sum(have2.sales) as sales_cusum
  from
    have as have1
  join
    have as have2
  on 
    have1.id >= have2.id  /* 'triangle' criteria */
  group by
    have1.id, have1.sales
  order by
    have1.id
  ;
quit;

第二种方法是逐行重新计算 cusum

proc sql;
  create table want as
  select have.id, have.sales, 
   ( select sum(inner.sales) 
     from (select * from have) as inner
     where inner.id <= have.id
   )
   as cusum
  from
   have;

【讨论】:

    【解决方案2】:

    我改变主意了,CDF 是一种不同的计算方式。 以下是如何通过数据步骤执行此操作。首先计算累计总数(我在这里使用了一个数据步骤,但如果你有 SAS/ETS,我可以使用 PROC EXPAND)。

    *sort demo data;
    proc sort data=sashelp.shoes out=shoes;
    by region sales;
    run;
    
    data cTotal last (keep = region cTotal);
    set shoes;
    by region;
    
    *calculate running total;
    if first.region then cTotal=0;
    cTotal = cTotal + sales;
    
    *output records, everything to cTotal but only the last record which is total to Last dataset;
    if last.region then output last;
    output cTotal;
    
    retain cTotal;
    run;
    
    *merge in results and calculate percentages;
    data calcs;
    merge cTotal Last (rename=cTotal=Total);
    by region;
    
    percent = cTotal/Total;
    run;
    

    如果您需要更有效的解决方案,我会尝试 DoW 解决方案。

    【讨论】:

    • 感谢 Reeza 的帮助...这将完成我的工作!
    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多