Date: Fri, 8 May 2009 18:50:31 -0400
Reply-To: Arthur Tabachneck <art297@NETSCAPE.NET>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Arthur Tabachneck <art297@NETSCAPE.NET>
Subject: Re: First and Last Observations.
Randy,
Either your example isn't correct, or I misunderstood what you are trying
to accomplish.
I'd think something like the following does what you want:
data have;
set have;
rec_order=_n_;
run;
proc sort data=have;
by id_a id_b;
run;
data want;
set have;
by id_a id_b;
if first.id_b then Flag_FO=1;
else Flag_FO=0;
if last.id_b then Flag_LO=1;
else Flag_LO=0;
run;
proc sort data=want;
by rec_order;
run;
Art
-------
On Fri, 8 May 2009 17:20:29 -0400, Randy <randistan69@HOTMAIL.COM> wrote:
>Dear All:
>
>My data set is as follows (ID_A and ID_B as a unit should be considered as
>one ID):
>
>
>Date Code ID_A ID_B Time
>May1 1 AB1 MN 10:00
>May1 1 AB4 MN 10:00
>May1 1 AB2 MO 10:01
>May1 1 AB1 MN 10:05
>May1 1 AB1 MN 11:05
>May1 1 AB2 MO 11:15
>May1 1 AB4 MN 11:25
>May1 2 AB3 MO 10:03
>May1 2 AB1 MN 10:05
>May1 2 AB2 MA 10:06
>May1 2 AB3 MO 11:19
>May1 2 AB1 MN 12:20
>May1 2 AB2 MA 13:21
>May1 2 AB1 FG 13:25
>May2 1 AB1 MN 9:31
>May2 1 AB2 MO 9:45
>May2 1 AB1 MN 10:31
>May2 2 AB3 MO 11:45
>May2 2 AB1 MN 12:36
>May2 2 AB3 MO 12:45
>
>I want to construct a data set with two flags. The first flag (flag = 1
if
>first observation, 0 otherwise) is the flag of the first observation of
ID_A
>and ID_B on a particular date and for a particular code. The second flag
>(flag = 1 if last observation, 0 otherwise)is the flag of the last
>observation of ID_A and ID_ on a particular date and particular code. If
>there is only one observation then the flag for the first observation and
>the last observation will both equal 1. So the output should look like
this:
>
>Date Code ID_A ID_B Time Flag_FO Flag_LO
>May1 1 AB1 MN 10:00 1 0
>May1 1 AB4 MN 10:00 1 0
>May1 1 AB2 MO 10:01 1 0
>May1 1 AB1 MN 10:05 0 0
>May1 1 AB1 MN 11:05 0 1
>May1 1 AB2 MO 11:15 0 1
>May1 1 AB4 MN 11:25 0 1
>May1 2 AB3 MO 10:03 1 0
>May1 2 AB1 MN 10:05 1 0
>May1 2 AB2 MA 10:06 1 0
>May1 2 AB3 MO 11:19 0 1
>May1 2 AB1 MN 12:20 0 1
>May1 2 AB2 MA 13:21 0 1
>May1 2 AB1 FG 13:25 1 1
>May2 1 AB1 MN 9:31 1 0
>May2 1 AB2 MO 9:45 1 1
>May2 1 AB1 MN 10:31 1 1
>May2 2 AB3 MO 11:45 1 0
>May2 2 AB1 MN 12:36 1 1
>May2 2 AB3 MO 12:45 0 1
>
>Thanx
>Randy