2016-09-10 11 views
1

私はSASの初心者ユーザーです。特に集計行の計算に当てはまります。SAS集計行計算

ここでは、以前あなたが遭遇したことがあると思われる質問があります。

私が保有しているデータは、保険証書に関連しています。左から列は、顧客番号、ポリシー番号、ポリシーステータス、ポリシー開始日、およびポリシーキャンセル日です(ポリシーがアクティブでない場合は、それ以外の場合は欠損値です)。顧客は、その後、それ以外のすべての彼のポリシーがキャンセルされた場合、その後、彼の状態は「アクティブ」になり、彼の状態を少なくとも一つのアクティブなポリシーを保持している場合

data have; 
    informat cust_id 8. pol_num $10. status $10. start_date can_date DDMMYY10.; 
    input cust_id pol_num status start_date can_date; 
    format start_date can_date date9.; 
    datalines; 
    110 P110001 Cancelled 04/12/2004 10/10/2013 
    110 P110002 Active 01/03/2005 . 
    123 P123001 Cancelled 21/07/1998 23/04/2013 
    123 P123003 Cancelled 22/10/1987 01/11/2011 
    133 P133001 Active 19/02/2001 . 
    133 P133001 Active 20/02/2002 . 
    ; 
run; 

は基本的に私は、お客様のレベルにこれらのポリシーレベルの情報をロールします「非アクティブ」になります。また、その顧客のもとで最も早い方のポリシーの開始日を選択する顧客 "開始日"が必要です。顧客が「非アクティブ」の場合、顧客の終了日として最新のポリシーキャンセル日が必要です。

以下

は私が必要なものである:どのような形態で

data want; 
    informat cust_id 8. status $10. start_date exit_date DDMMYY10.; 
    input cust_id status start_date exit_date; 
    format start_date exit_date date9.; 
    datalines; 
    110 Active 01/03/2005 . 
    123 Inactive 22/10/1987 23/04/2013 
    133 Active 19/02/2001 . 
    ; 
run; 

ソリューションをはるかに高く評価されるだろう! DATAステップまたはPROC SQLのいずれかが問題ありません。

ありがとうございました。

答えて

1

は、あなたはそのような何かを行うことができます。

proc sql; 
create table want as 
select cust_id, 
     case when count(case when status='Active' then 1 end) > 0 
      then 'Active' 
      else 'Inactive' 
      end as status, 
     min(start_date) as start_date, 
     case when count(case when status='Active' then 1 end) = 0 
      then max(can_date) 
      end as exit_date 
from have 
group by cust_id; 
quit; 
+0

この私は、感謝のヒープを必要とするものを正確にです! – Lin

0

あなたは、DATAステップで質問を攻撃することができます。ここでは...あなたのデータはCUST_IDstart_dateのによってソートされていると仮定すると、1つの簡単な方法です

data want; 
set have (keep=cust_id status start_date exit_date); 

where upcase(status) contains 'ACTIVE'; 
by cust_id start_date; 

    if first.start_date then output; 
else delete; 

run; 

/*BEGINNER NOTES*/ 

*1] WHERE tells SAS to compile only records that fit a certain 
    condition - the DS 'want' will never have any observations with 
    'CANCELLED' in the status variable; 

*2] I use UPCASE() to standardize the contents of status, as CONTAINS 
    is a case-sensitive operator; 

*3] FIRST.variable = 1 if the value is the first encountered in 
    the compile phase; 
+0

first.start_dateとOUTPUTの間にあるとします。また、これはOPが非アクティブな顧客に対して望んでいるものを取得しません。 – Quentin