我有一个医疗数据集,每个月都有一个主题被注册,他们有一行数据,一个变量,指示活动注册的月份。因此,如果有人登记了12个月,他们将在集合中有12行。他们还为服务日期提供了一个变量,给出了他们收到服务的确切日期。
我需要在服务日期之前选择连续6个月的注册时间,在服务日期之后选择连续6个月的注册时间。这个月的具体日子是无关紧要的。重要的是服务和注册月份的月份和年份。
以下是我的数据:
service_dt MemberID enroll_month
11May2010 1 01Nov2009
11May2010 1 01Dec2009
11May2010 1 01Jan2010
11May2010 1 01Feb2010
11May2010 1 01Mar2010
11May2010 1 01Apr2010
11May2010 1 01May2010
11May2010 1 01Jun2010
11May2010 1 01Jul2010
15Jun2010 2 01Jun2010
15Jun2010 2 01Aug2010因此,对于成员1,我们看到服务是在5月,所以我需要选择2009年11月,但2010年11月,如果是连续几个月。对于成员2来说,服务是在6月份,但是注册从6月跳到August...July并不是一个注册月,所以我需要将成员2从我的最后一个队列中删除。
发布于 2014-08-07 13:18:00
我采纳了乔的建议,把我的数据分成两组。在服务日期之前和服务日期之后。然后,我遵循Joe提供的代码,这是我之前问过的一个问题。不过,我只是稍微修改了一下。
/* This code will focus on the months before the service date.*/
data eligibility_before2;
set eligibility_before;
by memberid descending monthid;
if first.memberid then counter = 0;
if dif(monthid) < -1 and mod(monthid, 100) ne 12 then counter = 0;
if mod(monthid, 100) eq 12 and dif(monthid) ne -89 then counter = 0;
counter+1;
if counter = 6 then output;
run;
/*This code will focus on enrollment months after the service date*/
data eligibility_after2;
set eligibility_after;
by memberid monthid;
if first.memberid then counter = 0;
if dif(monthid) > 1 and mod(monthid, 100) ne 1 then counter = 0;
if mod(monthid, 100) eq 1 and dif(monthid) ne 89 then counter = 0;
counter+1;
if counter = 6 then output;
run;在此之后,只需将数据集合并回一起,指定必须在两个数据集中出现成员in才能包含在最后一组中。
发布于 2014-08-06 17:48:18
你想要最早登记月份后的前六个月的记录。您可以通过执行join获得符合此条件的所有成员。由于使用了SQL标记,我假设您希望将其作为SQL语句:
select d.memberid
from data d join
(select min(year(service_dt) * 12 + month(service_dt)) as enroll_ym, d.*
from data d
) dym
on d.memberid = dym.memberid and
year(d.service_dt) * 12 + month(service_dt) between enroll_ym and enroll_ym + 5
group by d.memberid
having count(distinct month(service_dt)) = 6;要获得原始行,您需要重新连接到原始数据。
https://stackoverflow.com/questions/25166677
复制相似问题