2016-09-21 65 views
0

我有一個數據集,其中必須提取已註冊的觀察值以計算連續的幾個月。 查看下面的數據外觀示例。計算連續註冊月數

data test; 
    input transMonths MemberID Plan $; 
    datalines; 
    201510 00001 HMO 
    201601 00001 HMO 
    201602 00001 HMO 
    201603 00001 PPO 
    201604 00001 HMO 
    201605 00001 HMO 
    201606 00001 HMO 
; 

主要問題:我如何才能SAS閱讀transMonths和計算有多少連續數月與Plan_HMO每MEMBERID了?

在上面的例子中,memberID 00001從201604到2016 06只有3個連續的月份。我只需要計算最近的連續月份。

任何幫助表示讚賞!

回答

1

您可以使用group processingnotsorted標誌。

data result; 
    retain transMonths_first; 
    set test; 
    by plan notsorted; 

    if first.plan then do; 
     months = 0; 
     transMonths_first = transMonths; 
    end; 

    months + 1; 

    if last.plan then do; 
     output; 
    end; 
run; 
0

使用滯後的可能是你需要獲取數據的一個好辦法:

data test; 
    input transMonths MemberID Plan $; 
    datalines; 
    201510 00001 HMO 
    201601 00001 HMO 
    201602 00001 HMO 
    201603 00001 PPO 
    201604 00001 HMO 
    201605 00001 HMO 
    201606 00001 HMO 
; 

*Sort data with ID, months, Plan; 
proc sort data = test; 
by MemberID transMonths Plan; 

* For each member, compare the month and Plan to previous observation and add the number of continuous months if they are the same plan but only one month ahead; 
data want; 
set test; 
by MemberID Plan notsorted; 
retain continuous_months; 
lag_month=lag(transMonths); 
lag_plan=lag(plan); 

if first.MemberID or first.Plan then continuous_months=1; 
else do; 
    if Plan = lag_plan and transMonths = lag_month +1 then continuous_months+1; 
end; 
run; 

*Get the member ID and maximum continuous months for all HMOs only; 
proc sql; 
create table want1 as 
select MemberID,max(continuous_months) as max_continuous_months 
from want 
where Plan ='HMO' 
group by 1; 
quit;