Date: Fri, 16 Jul 2010 21:56:40 -0500
Reply-To: Yu Zhang <zhangyu05@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Yu Zhang <zhangyu05@GMAIL.COM>
Subject: Re: Counter
In-Reply-To: <201007170147.o6GJJZ64006389@willow.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
data have;
input subject $ date :$12. flag;
datalines;
Subject1 5/1/2010 0
Subject1 6/1/2010 0
Subject1 7/1/2010 1
Subject1 8/1/2010 0
Subject1 9/1/2010 0
Subject1 10/1/2010 0
Subject2 1/1/2010 0
Subject2 2/1/2010 1
Subject2 3/1/2010 0
Subject2 4/1/2010 0
;
run;
proc sort data=have;
by subject date;
run;
data want;
do _n_=1 by 1 until(last.subject);
set have;
by subject;
if flag then ind=_n_;
end;
do _n_=1 to _n_;
set have;
flag1=_n_-ind;
output;
end;
drop ind;
run;
On Fri, Jul 16, 2010 at 8:47 PM, SUBSCRIBE SAS-L Anonymous <
hari.sas.j@gmail.com> wrote:
> Hi,
> I have a dataset with three variables, subject date flag. Basically I need
> to index in such a away that as soon as the flag = 1 then index is set 0
> and
> any observation which has a lower value date is indexed -1, -2 and so on
> ..
> and any observation that has higher date value than the (flag = 1) is
> indexed +1, +2 and so on within the group subject.Below is the program that
> creates raw data and also the desired output is shown below as well
>
> data alldata;
>
> input subject $ date $12. flag;
> datalines;
> Subject1 5/1/2010 0
> Subject1 6/1/2010 0
> Subject1 7/1/2010 1
> Subject1 8/1/2010 0
> Subject1 9/1/2010 0
> Subject1 10/1/2010 0
> Subject2 1/1/2010 0
> Subject2 2/1/2010 1
> Subject2 3/1/2010 0
> Subject2 4/1/2010 0
> ;
> run;
>
> data alldata1;
> set alldata (rename = (date = datem));
>
> date = input(datem,mmddyy10.);
>
> format date date9.;
> run;
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> Desired output:
>
> subject date flag index
> Subject1 5/1/2010 0 -2
> Subject1 6/1/2010 0 -1
> Subject1 7/1/2010 1 0
> Subject1 8/1/2010 0 1
> Subject1 9/1/2010 0 2
> Subject1 10/1/2010 0 3
> Subject2 1/1/2010 0 -1
> Subject2 2/1/2010 1 0
> Subject2 3/1/2010 0 1
> Subject2 4/1/2010 0 2
>
> Advanced thanks for help
>
> Regards,
>
> Hari
>
|