计算连续注册月数

时间:2016-09-21 15:38:48

标签: sas

我有一个数据集,我必须在其中提取已注册的观察结果以计算连续几个月的数量。 请参阅下面的数据示例。

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读取transMonth并计算每个memberID具有Plan_HMO的连续月数?

在上面的例子中,memberID 00001从201604到201606只有3个连续的月份。我只需要计算最近几个月的连续月份。

感谢任何帮助!

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;