Date: Mon, 15 Nov 2004 23:13:22 -0500
Reply-To: "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Subject: Re: Help with keeping observations, not variables
Xiaobing Fang wrote:
> I need to keep the individuals with the first period of their
> continous months. Here is the sample data:
>
> ID month
> 1 200401
> 1 200402
> 1 200403
> 1 200406
> 2 200401
> 2 200402
> 2 200407
>
> I want to change this data as:
>
> ID Month
> 1 200401
> 1 200402
> 1 200403
> 2 200401
> 2 200402
>
> Where I can keep the individuals with their first continous period.
> For example, ID 1 with 200401, 200402 and 200403. I have learned that
> 'keep' or 'drop' works for variables, but how about keeping or
> droping the observations? Thanks.
>
> Xiaobing
This should do the trick. The first month in each group is set as an anchor
point. As the step reads each row in each group the number of months from
the anchor is computed. If the number is the same as the row number in the
group, then the row is part of a nongap month sequence and is output.
data foo;
input ID month : yymmn6. ;
format month yymmd.;
datalines;
1 200401
1 200402
1 200403
1 200406
2 200401
2 200402
2 200407
run;
data runs;
do _n_ = 1 by 1 until (last.id);
set foo;
by id;
if first.id then anchor = intnx ('month', month, -1);
if intck ('month', anchor, month) = _n_ then output;
end;
drop anchor;
run;
--
Richard A. DeVenezia
http://www.devenezia.com/