SAS中特殊类型的摘要
我有以下数据:SAS中特殊类型的摘要,sas,summary,Sas,Summary,我有以下数据: 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
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
我想知道每年每个月有多少独特的客户拥有某种产品。“从”和“到”列表示产品“有效”,如果客户更改了尺寸、颜色或产品,或者一年过去了,则会得到一个新的行。我不在乎颜色或尺寸,只在乎产品的类型。我知道有很多方法可以做到这一点,但它们非常乏味
例如,对于2014年的第1个月,我想知道有多少独特的客户拥有产品1、产品2或产品3
Year Month no product 1 no product 2 no product 3
2014 1 1 0 0
(当时只有杰拉尔德“有效”,他只有产品1)
我想有一份所有“有效”年份和月份的类似清单
编辑:
数据:
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;
你的数据是一张图片。你需要把它变成文本,这样就可以剪切粘贴到程序中。谢谢!现在可以了吗?非常感谢你的回答。这是一个很好的解决方案。对于大数据集,可能会有很多行:)是的,可能是。如果存在存储问题,则可以在展开步骤中创建视图,尽管SAs仍需处理数据。散列解决方案可能是一种可能性,但我不太熟悉这些复杂的问题,所以也许其他人可以建议一种解决方案。