SAS 中特殊类型的摘要
Special type of summary in SAS
我有以下数据:
我想知道每年每个月有多少唯一客户拥有某种产品。 From 和 To 列是当产品为 "valid" 时,如果客户更改尺寸、颜色或产品,或者一年过去了。我不关心颜色或尺寸,只关心产品的类型。我知道有很多方法可以做到这一点,但它们非常乏味。
例如,对于 2014 年的第 1 个月,我想知道有多少唯一客户拥有产品 1、产品 2 或产品 3。
Year Month no product 1 no product 2 no product 3
2014 1 1 0 0
(当时只有杰拉德"valid",他只有产品1)
我想要一个这样的列表,涵盖所有 "valid" 年和月。
编辑:
数据:
name from to colour size product
Jenny 15JAN2015 15JAN2016' red small 1
Jenny 15JAN2016' 15JAN2017' green big 1
Jenny 15JAN2017' 15JAN2018' blue big 3
Bob 05APR2014 05APR2015 blue small 2
Bob 05APR2015 05APR2016 green small 2
Gerald 23MAY2013 23DEC2013 red small 2
Gerald 23DEC2013 23MAY2014 yellow big 1
Gerald 23MAY2014 04SEP2014 green big 1
Gerald 04SEP2014 25DEC2014 red small 2
Hope 23MAY2014 04SEP2014 red small 1
Hope 04SEP2014 25DEC2014 red small 1
Siri 15JAN2016' 15JAN2017' red small 1
如果您扩展原始数据,以便客户持有产品的每个月都有一行,那么进行频率计数并转置结果以获得所需格式是一件简单的事情。我的回答唯一不同的是,我将年份和月份显示为 1 列,因为它使循环更容易。
/* source data */
data have;
input name $ from_dt :date9. to_dt :date9. colour $ size $ product;
format from_dt to_dt date9.;
datalines;
Jenny 15JAN2015 15JAN2016 red small 1
Jenny 15JAN2016' 15JAN2017' green big 1
Jenny 15JAN2017' 15JAN2018' blue big 3
Bob 05APR2014 05APR2015 blue small 2
Bob 05APR2015 05APR2016 green small 2
Gerald 23MAY2013 23DEC2013 red small 2
Gerald 23DEC2013 23MAY2014 yellow big 1
Gerald 23MAY2014 04SEP2014 green big 1
Gerald 04SEP2014 25DEC2014 red small 2
Hope 23MAY2014 04SEP2014 red small 1
Hope 04SEP2014 25DEC2014 red small 1
Siri 15JAN2016' 15JAN2017' red small 1
;
run;
/* expand data to have a row for every month */
data temp1;
format mthyr yymm8.;
set have;
do i = 0 to intck('month',intnx('month',from_dt,0),intnx('month',to_dt,0));
mthyr = intnx('month',from_dt,i);
output;
end;
run;
/* count frequencies of products per month */
proc freq data=temp1 noprint;
table mthyr*product / sparse out=temp2;
run;
/* transpose data */
proc transpose data=temp2 out=want (drop=_:) prefix=product;
by mthyr;
id product;
var count;
run;
我有以下数据:
我想知道每年每个月有多少唯一客户拥有某种产品。 From 和 To 列是当产品为 "valid" 时,如果客户更改尺寸、颜色或产品,或者一年过去了。我不关心颜色或尺寸,只关心产品的类型。我知道有很多方法可以做到这一点,但它们非常乏味。
例如,对于 2014 年的第 1 个月,我想知道有多少唯一客户拥有产品 1、产品 2 或产品 3。
Year Month no product 1 no product 2 no product 3
2014 1 1 0 0
(当时只有杰拉德"valid",他只有产品1)
我想要一个这样的列表,涵盖所有 "valid" 年和月。
编辑: 数据:
name from to colour size product
Jenny 15JAN2015 15JAN2016' red small 1
Jenny 15JAN2016' 15JAN2017' green big 1
Jenny 15JAN2017' 15JAN2018' blue big 3
Bob 05APR2014 05APR2015 blue small 2
Bob 05APR2015 05APR2016 green small 2
Gerald 23MAY2013 23DEC2013 red small 2
Gerald 23DEC2013 23MAY2014 yellow big 1
Gerald 23MAY2014 04SEP2014 green big 1
Gerald 04SEP2014 25DEC2014 red small 2
Hope 23MAY2014 04SEP2014 red small 1
Hope 04SEP2014 25DEC2014 red small 1
Siri 15JAN2016' 15JAN2017' red small 1
如果您扩展原始数据,以便客户持有产品的每个月都有一行,那么进行频率计数并转置结果以获得所需格式是一件简单的事情。我的回答唯一不同的是,我将年份和月份显示为 1 列,因为它使循环更容易。
/* source data */
data have;
input name $ from_dt :date9. to_dt :date9. colour $ size $ product;
format from_dt to_dt date9.;
datalines;
Jenny 15JAN2015 15JAN2016 red small 1
Jenny 15JAN2016' 15JAN2017' green big 1
Jenny 15JAN2017' 15JAN2018' blue big 3
Bob 05APR2014 05APR2015 blue small 2
Bob 05APR2015 05APR2016 green small 2
Gerald 23MAY2013 23DEC2013 red small 2
Gerald 23DEC2013 23MAY2014 yellow big 1
Gerald 23MAY2014 04SEP2014 green big 1
Gerald 04SEP2014 25DEC2014 red small 2
Hope 23MAY2014 04SEP2014 red small 1
Hope 04SEP2014 25DEC2014 red small 1
Siri 15JAN2016' 15JAN2017' red small 1
;
run;
/* expand data to have a row for every month */
data temp1;
format mthyr yymm8.;
set have;
do i = 0 to intck('month',intnx('month',from_dt,0),intnx('month',to_dt,0));
mthyr = intnx('month',from_dt,i);
output;
end;
run;
/* count frequencies of products per month */
proc freq data=temp1 noprint;
table mthyr*product / sparse out=temp2;
run;
/* transpose data */
proc transpose data=temp2 out=want (drop=_:) prefix=product;
by mthyr;
id product;
var count;
run;