Date: Wed, 2 Apr 2003 13:03:59 +0800
Reply-To: Zibao Zhang <zibaozhang@HOTMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Zibao Zhang <zibaozhang@HOTMAIL.COM>
Subject: Re: How add a new variable to flag the visit time according to
valuesof another variables?
Content-Type: text/plain; charset="iso-8859-1"
Paul, thank you very much! You code works well.
Kind Regards,
Zibao Zhang, MD
----- Original Message -----
From: Paul Homes <pahomes@ZZZtekeds.com>
To: Zibao Zhang <zibaozhang@HOTMAIL.COM>
Cc: <SAS-L@LISTSERV.UGA.EDU>
Sent: Tuesday, April 01, 2003 3:13 PM
Subject: Re: How add a new variable to flag the visit time according to valuesof another variables?
> Zibao Zhang wrote:
> > Hi SAS-Ler,
> >
> > I have a dataset like this,
> >
> > Subj_No Vist_date A B
> >
> > 1001 01/01/2000 xx xx
> > 1001 02/01/2000 xx xx
> > 1002 01/03/2000 xx xx
> > 1002 02/04/2000 xx xx
> > 1002 03/16/2000 xx xx
> > 1003 01/11/2000 xx xx
> > 1004 01/15/2000 xx xx
> > 1004 03/10/2000 xx xx
> > ....
> >
> > I want to add a new variable(vist_id) to flag the visit time, like:
> >
> > Subj_No Vist_date A B Vist_id
> >
> > 1001 01/01/2000 xx xx 01
> > 1001 02/01/2000 xx xx 02
> > 1002 01/03/2000 xx xx 01
> > 1002 02/04/2000 xx xx 02
> > 1002 03/16/2000 xx xx 03
> > 1003 01/11/2000 xx xx 01
> > 1004 01/15/2000 xx xx 01
> > 1004 03/10/2000 xx xx 02
> > ...
> >
> > It seems that I can use "by subj_no vist_date" in datastep. I can only get the first and last using the automatic variables of "first." and "last.", but subjects were visited by many times, which was not fixed. How to add the flag variable??
> >
> > Thanks in advance.
> >
> > Kind Regards,
> > Zibao Zhang, MD
> >
>
> Hi Zibao,
>
> The technique is to accumulate a visit counter and use a first. flag to
> reset it when a new subject is encountered. Try something like.
>
> data work.test;
> infile cards firstobs=2;
> input Subj_No Vist_date:mmddyy10. a $ b $;
> format Vist_date mmddyy10.;
> cards;
> Subj_No Vist_date A B
> 1001 01/01/2000 xx xx
> 1001 02/01/2000 xx xx
> 1002 01/03/2000 xx xx
> 1002 02/04/2000 xx xx
> 1002 03/16/2000 xx xx
> 1003 01/11/2000 xx xx
> 1004 01/15/2000 xx xx
> 1004 03/10/2000 xx xx
> ;
> run;
>
> proc sort data=work.test;
> by Subj_No Vist_Date;
> run;
>
> data work.test;
> set work.test;
> by Subj_No;
> if first.Subj_No then Vist_id=0;
> Vist_id+1;
> format Vist_id z2.;
> run;
>
> Cheers
> Paul
> --
> Paul Homes ( email: pahomes@ZZZtekeds.com )
> TekEds.com Pty Ltd ( web: http://www.tekeds.com/ )
> --
> *** Remove ZZZ from email address to reply ***
>
>
|