Date: Thu, 2 Oct 2008 14:34:05 -0400
Reply-To: Muthia Kachirayan <muthia.kachirayan@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Muthia Kachirayan <muthia.kachirayan@GMAIL.COM>
Subject: Re: Having sql min(date)
In-Reply-To: <7fae10f00810020832k1f606145i87f44e8def66ec82@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Oct 2, 2008 at 11:32 AM, yingtao <yingtaoliu@gmail.com> wrote:
> I am curious of working it out with data step instead of SQL.
> I have the following lousy code, anyone has better idea to condense
> into one step?
> data need;
> do i=1 by 1 until(last.id1);
> set sample;
> by id1;
> array d1(*) d1_1-d1_3;
> array d2(*) d2_1-d2_3;
> d1(i)=date1;
> d2(i)=date2;
> end;
> do until(last.id1);
> set sample;
> by id1;
> f=sum(f,1);
> output;
> end;
> run;
>
> data need1;
> format mind1 mind2 date9.;
> do i=1 by 1 until(last.id1);
> set need;
> by id1;
> array d1(*) d1_1-d1_3;
> array d2(*) d2_1-d2_3;
> d1(f)=99999;/*Make it unrealistic big*/
> d2(f)=99999; /*Make it unrealistic big*/
> mind1=min(of d1_1-d1_3);
> mind2=min(of d2_1-d2_3);
> output;
> end;
> run;
>
> Tao
>
Tao,
If you are looking for a one data step solution to find the min of daye1 and
date2 within ID1, here is 3 ways where DoW- loop is used.
The first uses FIRST DOT and LAST DOT.
The second uses FIRST DOT.
The third does not use either.
*** First;
data need;
format mind1 mind2 date9.;
do until(last.id1);
set sample;
by id1;
if first.id1 then do;
mind1 = date1;
mind2 = date2;
end;
if date1 < mind1 then mind1 = date1;
if date2 < mind2 then mind2 = date2;
if last.id1 then output;
end;
run;
*** Second ;
data need;
format mind1 mind2 date9.;
do until(last.id1);
set sample;
by id1;
if first.id1 then do;
mind1 = date1;
mind2 = date2;
end;
if date1 < mind1 then mind1 = date1;
if date2 < mind2 then mind2 = date2;
end;
run;
*** Third;
data need;
format mind1 mind2 date9.;
mind1 = constant('BIG');
mind2 = constant('BIG');
do until(last.id1);
set sample;
by id1;
if date1 < mind1 then mind1 = date1;
if date2 < mind2 then mind2 = date2;
end;
run;
regards,
Muthia Kachirayan