Date: Thu, 18 Sep 2008 13:07:04 -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: Help on IML / ARRAY
In-Reply-To: <200809181542.m8IAkr0l004403@malibu.cc.uga.edu>
Content-Type: text/plain; charset=ISO-8859-1
On Thu, Sep 18, 2008 at 11:42 AM, Mike Smith <qt31416@gmail.com> wrote:
> Hi SAS experts,
>
> I would like to ask for your smart thoughts regarding the following SAS
> problem I have. I am dealing with two data sets, say data1 and data2. there
> are a lot of cases for each data set and the 2 primary varaibles for each
> data set are X and Y coordinates. And I want to calculate the distances of
> each case of data2 from data1.
>
> Say(for simplicity):
>
> data data1;
> input idno1 x1 y1;
> datalines;
> 1 0 0
> 2 1 1
> 3 2 0
> ;
>
> data data2;
> input idno2 x2 y2;
> 12 1 1
> 15 3 4
> ;
>
> the output data be:
>
> idno1 idno2 distance;
> 1 12 1.414 (=sqrt2)
> 1 15 5
> 2 12 0
> 2 15 sqrt(13)
> 3 12 sqrt(2)
> 3 15 sqrt(17)
>
> Please help. Thanks,
>
> Mike
> MSU
>
Here is a Hash way.
data data1;
input idno1 x1 y1;
datalines;
1 0 0
2 1 1
3 2 0
;
run;
data data2;
input idno2 x2 y2;
cards;
12 1 1
15 3 4
;
run;
data need;
length idno1 idno2 8 ;
declare hash h();
h.definekey('counter');
h.definedata('idno2','x2','y2');
h.definedone();
do counter = 1 by 1 until(eof);
set data2 end = eof;
h.add();
end;
do until(eof1);
set data1 end = eof1;
do counter = 1 to h.num_items;
h.find();
dis = sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2);
output;
end;
end;
stop;
keep idn: dis;
run;
proc print data = need;
run;
Muthia Kachirayan
|