作为第一次使用SAS代码的SQL开发人员,我很难理解我所提供的脚本的一个部分。
请任何人解释一下下面的内容,或者如果可能的话,在SQL中的等价性?
* sum up the total 6 months value for customers with positive value and quantity of items;
proc summary data=value_last6_positive nway missing;
var saleprice quantity;
class Cardid ;
output out = value_last6_s (drop=_type_ _freq_)
sum(saleprice)=saleprice
sum(quantity)=quantity;
run;
* rank them;
proc sort data=value_last6_s;
by saleprice;
run;
data count;
set value_last6_s;
count=1;
run;
proc sort data=count;
by count;
run;
data count2;
set count;
by count;
if first.count then rank=1;
else rank+1;
if rank=<544139 then decile=10;
else if rank=<544139*2 then decile=9;
else if rank=<544139*3 then decile=8;
else if rank=<544139*4 then decile=7;
else if rank=<544139*5 then decile=6;
else if rank=<544139*6 then decile=5;
else if rank=<544139*7 then decile=4;
else if rank=<544139*8 then decile=3;
else if rank=<544139*9 then decile=2;
else decile=1;
run;
proc freq data=count2;
table decile;
run;
proc means data=count2;
var saleprice;
class decile;
run;我已经构建了一个与value_last6_s等价的临时表,该表的结构是(CardID, SalePrice, Quantity),使用CardID分组的销售数据聚合。不太确定该怎么做。提前谢谢。
编辑:
我对第一个proc summary块的转换:
-- value_last6_s
SELECT CardID,
SUM(SalePrice) SalePrice,
SUM(Quantity) Quantity
INTO #value_last6_s
FROM #value_last6_positive
GROUP BY CardID
ORDER BY SUM(SalePrice);发布于 2016-05-31 12:59:30
第一步只是创建一个汇总表。虚拟变量计数作为常量添加,您可能不需要这个常量。排序需要按照sum或SALEPRICE的顺序获得值,以便在下一步中创建秩变量。
create table COUNT as
select Cardid
, sum(saleprice) as saleprice
, sum(quantity) as quantity
from value_last6_positive
group by Cardid
;下一个数据步骤是生成一个秩变量。您不能在PROC中这样做,因为它不包括窗口函数。您应该能够使用ROW_NUMBER()函数来做到这一点。您可以用CASE语句替换IF/ELSE/ can链。
create table COUNT2 as
select a.*
, row_number() over (order by saleprice) as RANK
, case
when (rank<=544139) then 10
when (rand<=544139*2) then 9
...
else 1 end as DECILE
from COUNT a
;最后的步骤是使用新的十进制变量创建报告。
一份频率报告。
select decile,count(*) as COUNT,COUNT/(count(*) over()) as PERCENT
from count2
group by 1
order by 1
;以及方法的总结。
select decile
, count(saleprice) as N
, mean(saleprice) as Mean
, min(saleprice) as Min
, max(saleprice) as Max
from count2
group by 1
order by 1
;https://stackoverflow.com/questions/37545179
复制相似问题