<?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: Create new table using a old table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21273#M3406</link>
    <description>If you have sas/stat licensed, then the calculation takes only a few lines. Reshaping from the matrix to an edgelist is left as homework.&lt;BR /&gt;
[pre]&lt;BR /&gt;
   data one;&lt;BR /&gt;
     input id $ x y @@;&lt;BR /&gt;
   cards;&lt;BR /&gt;
   S1 1 2 S2 1 3 S3 2 3 S4 5 6&lt;BR /&gt;
   ;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   proc distance data=one out=dist shape=square method=euclid;&lt;BR /&gt;
     var ratio(x y);&lt;BR /&gt;
     id id;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   /* check */&lt;BR /&gt;
   proc print data=dist noobs;&lt;BR /&gt;
     format s: f6.4;&lt;BR /&gt;
   run;&lt;BR /&gt;
   /* on lst&lt;BR /&gt;
   id        S1        S2        S3        S4&lt;BR /&gt;
&lt;BR /&gt;
   S1    0.0000    1.0000    1.4142    5.6569&lt;BR /&gt;
   S2    1.0000    0.0000    1.0000    5.0000&lt;BR /&gt;
   S3    1.4142    1.0000    0.0000    4.2426&lt;BR /&gt;
   S4    5.6569    5.0000    4.2426    0.0000&lt;BR /&gt;
   */&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you don't have the luxury of having sas/stat licensed, then sql takes only a few lines more:&lt;BR /&gt;
[pre]&lt;BR /&gt;
   data one;&lt;BR /&gt;
     input id $ x y @@;&lt;BR /&gt;
   cards;&lt;BR /&gt;
   S1 1 2 S2 1 3 S3 2 3 S4 5 6&lt;BR /&gt;
   ;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   proc sql;&lt;BR /&gt;
     create table dist as&lt;BR /&gt;
     select d1.id as id1, d2.id as id2&lt;BR /&gt;
          , sqrt((d1.x-d2.x)**2 + (d1.y-d2.y)**2) as dist&lt;BR /&gt;
     from   one as d1, one as d2&lt;BR /&gt;
     where  d1.id not = d2.id&lt;BR /&gt;
     order by d1.id, d2.id;&lt;BR /&gt;
&lt;BR /&gt;
     /* check */&lt;BR /&gt;
     select id1, id2, putn(dist,"6.4") as dist from dist;&lt;BR /&gt;
   quit;&lt;BR /&gt;
   /* on lst&lt;BR /&gt;
   id1       id2       dist&lt;BR /&gt;
   --------------------------&lt;BR /&gt;
   S1        S2        1.0000&lt;BR /&gt;
   S1        S3        1.4142&lt;BR /&gt;
   S1        S4        5.6569&lt;BR /&gt;
   S2        S1        1.0000&lt;BR /&gt;
   S2        S3        1.0000&lt;BR /&gt;
   S2        S4        5.0000&lt;BR /&gt;
   S3        S1        1.4142&lt;BR /&gt;
   S3        S2        1.0000&lt;BR /&gt;
   S3        S4        4.2426&lt;BR /&gt;
   S4        S1        5.6569&lt;BR /&gt;
   S4        S2        5.0000&lt;BR /&gt;
   S4        S3        4.2426&lt;BR /&gt;
   */&lt;BR /&gt;
[/pre]</description>
    <pubDate>Mon, 25 Oct 2010 18:52:34 GMT</pubDate>
    <dc:creator>chang_y_chung_hotmail_com</dc:creator>
    <dc:date>2010-10-25T18:52:34Z</dc:date>
    <item>
      <title>Create new table using a old table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21268#M3401</link>
      <description>Hi Guys,&lt;BR /&gt;
I am new to SAS programming, I am stuck with the following problem for some time, I will really appricate if you can give me some pointers for solving this.&lt;BR /&gt;
&lt;BR /&gt;
I have a table named coordinates&lt;BR /&gt;
ID X  Y&lt;BR /&gt;
--  --  --&lt;BR /&gt;
S1 1 2&lt;BR /&gt;
S2 1 3&lt;BR /&gt;
S3 2 3&lt;BR /&gt;
S4 5 6&lt;BR /&gt;
&lt;BR /&gt;
I need to make one another table out of this table such that it should contain the following&lt;BR /&gt;
ID_S ID_D DIST&lt;BR /&gt;
------  ------  ------&lt;BR /&gt;
S1    S2    sqrt((1-1)^2 + (3-2)^2)&lt;BR /&gt;
S1    S3    ...&lt;BR /&gt;
S1    S4    ...&lt;BR /&gt;
S2    S1    ...&lt;BR /&gt;
S2    S3    ...&lt;BR /&gt;
S2    S4    ...&lt;BR /&gt;
S3    S1    ...&lt;BR /&gt;
S3    S2    ...&lt;BR /&gt;
S3    S4    ...&lt;BR /&gt;
S4    S1    ...&lt;BR /&gt;
S4    S2    ...&lt;BR /&gt;
S4    S3    ...&lt;BR /&gt;
&lt;BR /&gt;
Regards&lt;BR /&gt;
HunT

Message was edited by: HunT_GuY</description>
      <pubDate>Sat, 23 Oct 2010 19:04:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21268#M3401</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-10-23T19:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create new table using a old table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21269#M3402</link>
      <description>Hi:&lt;BR /&gt;
  It looks like you are making the new table out of all possible combinations of the values for the ID variable from the original COORDINATES table??? You could do this with PROC SQL. When you do a join (or a self-join) like this in PROC SQL, you have to know how you are going to name the new variables (such as ID_S and ID_D) that you want to create. Generally, when you are creating all possible combinations of values (such as values for ID), this kind of join is called a Cartesian product. In your case, you do NOT seem to want ID values of S1 combined with S1 or ID values of S2 combined with S2, etc -- so you would have to use a WHERE clause in your PROC SQL to prevent this combination of IDs  from being output to the new table.&lt;BR /&gt;
 &lt;BR /&gt;
  What I do NOT understand is &lt;BR /&gt;
1) X and Y variables are shown in the coordinates file -- but they are not shown as variables in the new table. What happens to X and Y in the creation of the new table?&lt;BR /&gt;
2) You show a new variable called DIST with ... for each observation in the new table after the first observation. Does this mean that DIST is a fixed value and uses the same formula for every observation? Or, do you mean to imply that the X and Y values from ID_S and ID_D are used in the calculation of DIST?&lt;BR /&gt;
 &lt;BR /&gt;
For example, if you had data like this (which shows the possible combinations of S1 and S2 -AND- also shows the X and Y values for ID_S and ID_D) all with different column names, &lt;BR /&gt;
[pre]&lt;BR /&gt;
id_s    s_x    s_y    id_d    d_x    d_y&lt;BR /&gt;
 S1      1      2      S2      1      3&lt;BR /&gt;
 S1      1      2      S3      2      3&lt;BR /&gt;
 S1      1      2      S4      5      6&lt;BR /&gt;
 S2      1      3      S1      1      2&lt;BR /&gt;
 S2      1      3      S3      2      3&lt;BR /&gt;
 S2      1      3      S4      5      6&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                             &lt;BR /&gt;
...would the various X and Y values be used in the SQRT-based formula for every observation???&lt;BR /&gt;
&lt;BR /&gt;
Can you explain a bit more about how the DIST column gets calculated for each observation and what your formula represents???&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Sat, 23 Oct 2010 20:37:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21269#M3402</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-10-23T20:37:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create new table using a old table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21270#M3403</link>
      <description>Hi cynthia,&lt;BR /&gt;
Thanks for the reply, basically I am trying to find the distance between two points, here id_s -&amp;gt; represents the source point, id_d -&amp;gt; represents the destination point and dist -&amp;gt; represents the distance between these two points. Using the formula distance between x1, y1 and x2,y2 = sqrt ((x2-x1)^2 + (y2-y1)^2). Therefore the values of x,y will be used for every observation for calculation of distance. I hope I was able to clarity your concern.&lt;BR /&gt;
Thanks&lt;BR /&gt;
HunT</description>
      <pubDate>Sun, 24 Oct 2010 08:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21270#M3403</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-10-24T08:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: Create new table using a old table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21271#M3404</link>
      <description>Using the DATA step here is a quick program that is very scalable;  But first as an aside this,Euclidean distance, is only one of the simpler measures of distance available in SAS (search for 'proximity measures' and look into PROC DISTANCE).  Some of the cluster analysis procedures can also calculate this as a distance matrix.  Depends on your usage.&lt;BR /&gt;
&lt;BR /&gt;
[pre]data coords;&lt;BR /&gt;
&lt;BR /&gt;
input id $2. x y;&lt;BR /&gt;
&lt;BR /&gt;
datalines;&lt;BR /&gt;
S1 1 2&lt;BR /&gt;
S2 1 3&lt;BR /&gt;
S3 2 3&lt;BR /&gt;
S4 5 6&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* Build distances;&lt;BR /&gt;
data dist(keep= idx idy x1 y1 x2 y2 dist);&lt;BR /&gt;
   set coords nobs=nobs end=eof;&lt;BR /&gt;
&lt;BR /&gt;
   array xx {100} _temporary_;&lt;BR /&gt;
   array yy {100} _temporary_;&lt;BR /&gt;
   array idlist {100} $2 _temporary_;&lt;BR /&gt;
&lt;BR /&gt;
   * Fill the arrays;&lt;BR /&gt;
   xx(_n_) = x;&lt;BR /&gt;
   yy(_n_) = y;&lt;BR /&gt;
   idlist(_n_) = id;&lt;BR /&gt;
&lt;BR /&gt;
   * write the obs from the array;&lt;BR /&gt;
   if eof then do i = 1 to nobs-1;&lt;BR /&gt;
      do j= i+1 to nobs;&lt;BR /&gt;
         * Assign values to each variable;&lt;BR /&gt;
         idx = idlist(i);&lt;BR /&gt;
         idy = idlist(j);&lt;BR /&gt;
         x1 = xx(i);&lt;BR /&gt;
         y1 = yy(i);&lt;BR /&gt;
         x2 = xx(j);&lt;BR /&gt;
         y2 = yy(j);&lt;BR /&gt;
         dist=sqrt((x1-x2)**2 + (y1-y2)**2);&lt;BR /&gt;
         output dist;&lt;BR /&gt;
      end;&lt;BR /&gt;
   end;&lt;BR /&gt;
run;     &lt;BR /&gt;
&lt;BR /&gt;
proc print data=dist;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 25 Oct 2010 01:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21271#M3404</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-10-25T01:31:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create new table using a old table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21272#M3405</link>
      <description>Thanks ArtC. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Mon, 25 Oct 2010 06:12:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21272#M3405</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-10-25T06:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create new table using a old table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21273#M3406</link>
      <description>If you have sas/stat licensed, then the calculation takes only a few lines. Reshaping from the matrix to an edgelist is left as homework.&lt;BR /&gt;
[pre]&lt;BR /&gt;
   data one;&lt;BR /&gt;
     input id $ x y @@;&lt;BR /&gt;
   cards;&lt;BR /&gt;
   S1 1 2 S2 1 3 S3 2 3 S4 5 6&lt;BR /&gt;
   ;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   proc distance data=one out=dist shape=square method=euclid;&lt;BR /&gt;
     var ratio(x y);&lt;BR /&gt;
     id id;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   /* check */&lt;BR /&gt;
   proc print data=dist noobs;&lt;BR /&gt;
     format s: f6.4;&lt;BR /&gt;
   run;&lt;BR /&gt;
   /* on lst&lt;BR /&gt;
   id        S1        S2        S3        S4&lt;BR /&gt;
&lt;BR /&gt;
   S1    0.0000    1.0000    1.4142    5.6569&lt;BR /&gt;
   S2    1.0000    0.0000    1.0000    5.0000&lt;BR /&gt;
   S3    1.4142    1.0000    0.0000    4.2426&lt;BR /&gt;
   S4    5.6569    5.0000    4.2426    0.0000&lt;BR /&gt;
   */&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you don't have the luxury of having sas/stat licensed, then sql takes only a few lines more:&lt;BR /&gt;
[pre]&lt;BR /&gt;
   data one;&lt;BR /&gt;
     input id $ x y @@;&lt;BR /&gt;
   cards;&lt;BR /&gt;
   S1 1 2 S2 1 3 S3 2 3 S4 5 6&lt;BR /&gt;
   ;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   proc sql;&lt;BR /&gt;
     create table dist as&lt;BR /&gt;
     select d1.id as id1, d2.id as id2&lt;BR /&gt;
          , sqrt((d1.x-d2.x)**2 + (d1.y-d2.y)**2) as dist&lt;BR /&gt;
     from   one as d1, one as d2&lt;BR /&gt;
     where  d1.id not = d2.id&lt;BR /&gt;
     order by d1.id, d2.id;&lt;BR /&gt;
&lt;BR /&gt;
     /* check */&lt;BR /&gt;
     select id1, id2, putn(dist,"6.4") as dist from dist;&lt;BR /&gt;
   quit;&lt;BR /&gt;
   /* on lst&lt;BR /&gt;
   id1       id2       dist&lt;BR /&gt;
   --------------------------&lt;BR /&gt;
   S1        S2        1.0000&lt;BR /&gt;
   S1        S3        1.4142&lt;BR /&gt;
   S1        S4        5.6569&lt;BR /&gt;
   S2        S1        1.0000&lt;BR /&gt;
   S2        S3        1.0000&lt;BR /&gt;
   S2        S4        5.0000&lt;BR /&gt;
   S3        S1        1.4142&lt;BR /&gt;
   S3        S2        1.0000&lt;BR /&gt;
   S3        S4        4.2426&lt;BR /&gt;
   S4        S1        5.6569&lt;BR /&gt;
   S4        S2        5.0000&lt;BR /&gt;
   S4        S3        4.2426&lt;BR /&gt;
   */&lt;BR /&gt;
[/pre]</description>
      <pubDate>Mon, 25 Oct 2010 18:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-table-using-a-old-table/m-p/21273#M3406</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-10-25T18:52:34Z</dc:date>
    </item>
  </channel>
</rss>

