我在SAS中有一张表格,例如,其中有一个customer_id和5列及其每月状态。客户有6种不同的状态。例如
customer_id month1 month2 month3 month4 month5
12345678 Waiting Inactive Active Active Canceled
我想从最频繁的month1-month5列返回一个值。在这种情况下,其值为有效。所以结果将是
customer_id frequent
12345678 Active
SAS有任何功能吗?我对如何使用sql有所了解,但是在许多情况下,它将非常复杂。我是SAS的新手,所以我想会有更好的解决方案。
如果使用数组将客户历史记录的每个月的数据集拆分为一个观察值,则可以在proc sql中使用摘要函数轻松获得最频繁出现的情况,并使用最近一个月(假设是第5个月)来打破平局。
data want1;
set have;
array m(*) month1 -- month5;
do i = 1 to dim(m);
cid = customer_id;
frequent = m(i);
position = i;
output;
end;
keep cid frequent position;
run;
proc sql;
create table want2 as select
cid as customer_id,
frequent,
max(position) as max_pos,
count(frequent) as count
from want1
group by cid, frequent;
quit;
proc sort data = want2; by customer_id descending count descending max_pos; run;
data want3;
set want2;
by customer_id descending count descending max_pos;
if first.customer_id;
drop max_pos count;
run;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句