<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: distances between observations within groups in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855795#M42307</link>
    <description>&lt;P&gt;Here's one solution :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=class; by sex name; run;

proc distance data=class out=dist method=euclid SHAPE=SQUARE;
 by sex;
 copy name;
 var interval(age height weight);
run;

data dist_trp(drop=dist1-dist10);
 set dist;
 by sex;
 array dist{10} dist1-dist10;
 do Person_withinSameSex = 1 to dim(dist);
  distance_euclid=dist(Person_withinSameSex);
  output;
 end;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jan 2023 17:33:29 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2023-01-26T17:33:29Z</dc:date>
    <item>
      <title>distances between observations within groups</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855698#M42303</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I would like to calculate distances between all observations within groups.&lt;/P&gt;
&lt;P&gt;The result should be a dataset in which for each observation there are as many records as are the observations within the group, along with the distances.&lt;/P&gt;
&lt;P&gt;My best attempt to realize this is as follows:&lt;/P&gt;
&lt;P&gt;ods select none;&lt;BR /&gt;proc modeclus data=have k=4 Neighbor ; &lt;BR /&gt;by group;&lt;BR /&gt;var x1 x2 x3 x4;&lt;BR /&gt;id id;&lt;BR /&gt;ods output Neighbor=Neighbor;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want(drop=id_old);&lt;BR /&gt;set Neighbor(rename=(id=id_old));&lt;BR /&gt;by group;&lt;BR /&gt;retain id;&lt;BR /&gt;if not missing(id_old) then id=id_old;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the main problem with this solution seems to be that I cannot choose a distance metric. Or can I?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On the other side with proc distance&amp;nbsp; I can choose among many metrics, but the result is a matrix, as follows:&lt;/P&gt;
&lt;P&gt;proc distance data=have out=dist method=euclid SHAPE=SQUARE;&lt;BR /&gt;by group;&lt;BR /&gt;copy id;&lt;BR /&gt;var interval(x1 x2 x3 x4);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;I know I can match the dataset with itself and then calculate any distance but I wonder if there are other solutions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate suggestions on the possibility to choose a distance metric from proc modeclus and/or how the resulting dataset of proc distance has to be manipulated to obtain the shape I need.&lt;/P&gt;
&lt;P&gt;And/or any other solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Than you very much in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 10:12:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855698#M42303</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2023-01-26T10:12:00Z</dc:date>
    </item>
    <item>
      <title>Re: distances between observations within groups</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855795#M42307</link>
      <description>&lt;P&gt;Here's one solution :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=class; by sex name; run;

proc distance data=class out=dist method=euclid SHAPE=SQUARE;
 by sex;
 copy name;
 var interval(age height weight);
run;

data dist_trp(drop=dist1-dist10);
 set dist;
 by sex;
 array dist{10} dist1-dist10;
 do Person_withinSameSex = 1 to dim(dist);
  distance_euclid=dist(Person_withinSameSex);
  output;
 end;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 17:33:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855795#M42307</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-01-26T17:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: distances between observations within groups</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855799#M42308</link>
      <description>&lt;P&gt;On top of the previous reply just above ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="xisDoc-refProc"&gt;
&lt;DIV id="statug_modeclus000020" class="aa-section"&gt;
&lt;P class="xisDoc-paragraph"&gt;The MODECLUS procedure clusters observations in a SAS data set by using any of several algorithms based on nonparametric density estimates. No Euclidean distance metric is possible.&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;Here are the methods MODECLUS can use :&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;SAS/STAT 15.2 User's Guide&lt;BR /&gt;The MODECLUS Procedure&lt;BR /&gt;Clustering Methods&lt;BR /&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/statug/statug_modeclus_details02.htm" target="_blank" rel="noopener"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/statug/statug_modeclus_details02.htm&lt;/A&gt;&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;Koen&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 26 Jan 2023 17:39:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855799#M42308</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-01-26T17:39:22Z</dc:date>
    </item>
    <item>
      <title>Re: distances between observations within groups</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855931#M42315</link>
      <description>&lt;P&gt;thank you very much.&lt;/P&gt;
&lt;P&gt;the only thing missing is that I need to know which&amp;nbsp; is mached to any id, but I can obtain that&lt;/P&gt;
&lt;P&gt;with a little coding like this.&lt;/P&gt;
&lt;P&gt;is it correct?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=sashelp.class out=class; by sex name; run;&lt;/P&gt;
&lt;P&gt;proc distance data=class out=dist method=euclid SHAPE=SQUARE;&lt;BR /&gt;by sex;&lt;BR /&gt;copy name;&lt;BR /&gt;var interval(age height weight);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data dist;&lt;BR /&gt;set dist;&lt;BR /&gt;by sex;&lt;BR /&gt;retain id2;&lt;BR /&gt;if first.sex then id2=1;&lt;BR /&gt;else id2+1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data dist_trp(drop=dist1-dist10);&lt;BR /&gt;*data dist_trp;&lt;BR /&gt;set dist;&lt;BR /&gt;by sex;&lt;BR /&gt;array dist{10} dist1-dist10;&lt;BR /&gt;do Person_withinSameSex = 1 to dim(dist);&lt;BR /&gt;distance_euclid=dist(Person_withinSameSex);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table dist_trp as select a.*,b.name as name_matched&lt;BR /&gt;from dist_trp as a&lt;BR /&gt;left join&lt;BR /&gt;dist as b&lt;BR /&gt;on a.sex=b.sex and a.Person_withinSameSex=b.id2&lt;BR /&gt;order by sex,id2,Person_withinSameSex;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Jan 2023 12:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/855931#M42315</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2023-01-27T12:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: distances between observations within groups</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/856115#M42323</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can PROC COMPARE your output dataset&lt;/P&gt;
&lt;P&gt;with "work.dist_trp_match" to check whether your program is right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=class; by sex name; run;

data class;
 set class;
 retain id2;
 by sex name; 
 if first.sex then do; id2=0; end;
 id2=id2+1;
run;

proc distance data=class out=dist method=euclid SHAPE=SQUARE;
 by sex;
 copy name id2;
 var interval(age height weight);
run;

data dist_trp(drop=dist1-dist10);
 set dist;
 by sex;
 array dist{10} dist1-dist10;
 do Person_withinSameSex = 1 to dim(dist);
  distance_euclid=dist(Person_withinSameSex);
  output;
 end;
run;

PROC SQL noprint;
 create table dist_trp_match as
 select aa.* , bb.name as MatchName
 from   dist_trp(where=(distance_euclid is not missing))    as aa
      , dist(where=(Name is not missing) keep=sex name id2) as bb
 where aa.sex = bb.sex AND aa.Person_withinSameSex=bb.id2 ;
QUIT;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jan 2023 21:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/856115#M42323</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-01-28T21:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: distances between observations within groups</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/856278#M42337</link>
      <description>&lt;P&gt;Thank You Koen,&lt;/P&gt;
&lt;P&gt;I marked my second message as solution by mistake, since I wanted to mark yours as solution. apologies.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know how to undo the marking. I wrote to sas to ask the unmarking.&lt;/P&gt;
&lt;P&gt;Let's see if they respond.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 16:48:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/distances-between-observations-within-groups/m-p/856278#M42337</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2023-01-30T16:48:37Z</dc:date>
    </item>
  </channel>
</rss>

