2015-04-23 66 views
1

我在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是新的,所以我想會有一些更好的解決方案。

+1

SQL:Unpivot月,按customer_id和month分組,按customer_id分區,按count desc排序等。 – Arvo

回答

2

如果您使用數組來分割數據集爲一個觀察一個客戶歷史,你可以在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; 
+0

非常感謝!它效果很好。 – Vendula

0

解決方案稍差,但它確實爲兩種不同價值觀的工作,在這種情況下5個月。如果主動> = 3號,這是最常見的值:

select customer_id, case when (case when month1 = 'Active' then 1 else 0 end + 
           case when month2 = 'Active' then 1 else 0 end + 
           case when month3 = 'Active' then 1 else 0 end + 
           case when month4 = 'Active' then 1 else 0 end + 
           case when month5 = 'Active' then 1 else 0 end) >= 3 
          then 'Active' else 'Waiting' end 
from tablename 

的另一種方式,UNION ALL

select customer_id, month, count(*) as cnt 
(
    select customer_id, month1 as month from tablename 
    UNION ALL 
    select customer_id, month2 from tablename 
    UNION ALL 
    select customer_id, month3 from tablename 
    UNION ALL 
    select customer_id, month4 from tablename 
    UNION ALL 
    select customer_id, month5 from tablename 
) 
group by customer_id, month 
order by cnt 
fetch first 1 row only 

哪裏FETCH FIRST是ANSI SQL,可能是某些DBMS產品TOPLIMIT

+0

謝謝。我有類似的想法。但問題是,可以有6個不同的價值觀...... – Vendula

+0

也許你可以更新您的樣本數據?如果是平局,你期望什麼結果? – jarlh

+0

對不起,你怎麼看領帶? – Vendula