From: Trish Bous on
Sorry, that code is

DATA TMP;
SET ioc.panc_a;
retain flag;
new_group = group;
if first.key then do;
flag = new_group;
end;
else do;
new_group = flag;
end;
run;
From: Joe Matise on
You need a BY statement:

by key;
under the SET statement. And of course it must be sorted by that.

-Joe

On Fri, Feb 19, 2010 at 4:30 PM, Trish Bous <tboussard(a)gmail.com> wrote:

> Sorry, that code is
>
> DATA TMP;
> SET ioc.panc_a;
> retain flag;
> new_group = group;
> if first.key then do;
> flag = new_group;
> end;
> else do;
> new_group = flag;
> end;
> run;
>
From: Proc Me on
A by group, ..., or (requires v9.2) a hash table:

data have(drop=_date);
length key $4 group 3 _date $10;
input key $ group _date $;
format date date9.;
/*format _date date10.;*/
date = mdy(substr(_date, 1, 2), substr(_date, 4,2), substr(_date, 7,4));
cards;
1C2 2 12/10/1995
1C2 . 02/05/2002
1C2 3 01/01/2005
2Z45 . 02/10/2005
2Z45 1 05/17/2006
3B12 2 02/06/1998
4A53 . 10/01/2006
4A53 2 05/12/2006
run;

/*
* First of all we load keys and lowest dated (optionally non-missing)
* group into a hash table. Then we use this hash table to calculate the
* new group and the date corresponding thereto.
*/
data want(drop=rc _:);

if 0 then set have;
/* Declare and define a key-only lookup hash table */
format first_date date9.;
declare Hash LookUp();
rc = LookUp.DefineKey ('key');
rc = LookUp.DefineData ('new_group', 'first_date');
rc = LookUp.DefineDone ();
/* Load lookup records */
do until (_lookup_loaded) ;
set have(rename=(date=_date group=_group)) end = _lookup_loaded;
rc = Lookup.find();
if rc ne 0 then do;
first_date = _date;
new_group = _group;
rc = Lookup.add();
end;
else do;
if first_date > _date /* or new_group eq . */ then do;
first_date = _date;
new_group = _group;
rc = Lookup.replace();
end;
end;
end;

do until (_search_complete) ;
set have end = _search_complete;
rc = LookUp.find();
if (rc = 0) then output;
end;

stop;

run;

I hope this helps,

Proc Me
From: PJ on
data have;

input key $4. group date mmddyy10.;
format date yymmddn8.;
cards;
1C2 2 12/10/1995
1C2 . 02/05/2002
1C2 3 01/01/2005
2Z45 . 02/10/2005
2Z45 1 05/17/2006
3B12 2 02/06/1998
4A53 . 10/01/2006
4A53 2 05/12/2006
;

run;

proc sort data=have;
by key date;
run;

data want;

set have;
by key date;
retain newgp;
if first.key then newgp = group;
run;