2016-09-21 93 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;