<?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: iterative join to accumulate records from a second table (linear-referencing)? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511545#M137679</link>
    <description>This is a more complex problem, and I think you'll get the best solution if you post a really clear example. A small data set that shows what you have and EXACTLY what output would be expected from that input.</description>
    <pubDate>Thu, 08 Nov 2018 21:37:01 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-11-08T21:37:01Z</dc:date>
    <item>
      <title>iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511535#M137674</link>
      <description>&lt;P&gt;I have two datasets which need to be “combined” in a certain way that goes beyond a simple join/merge.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One dataset represents a roadway network consisting of links connected by nodes. Below is a representation of a roadway that splits at a “Y” intersection. A driver on link 100 can continue onto link 101 or 201. 101 is a dead-end, but 201 connects to 202, which connects to 203. The dataset is tens of thousands of rows.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;LINK_ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;FROM_NODE&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;TO_NODE&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;LINK_TOTAL_LENGTH&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1.1&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;101&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.8&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;201&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;90&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;202&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;90&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;91&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.4&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;203&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;91&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;92&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1.0&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The other dataset represents points on the roadway network. These will serve as starting points for trips. Here is one record that indicates a&amp;nbsp;starting point 0.3 units into the link. This dataset is a few thousand rows.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;START_POINT_ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;LINK_ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;DISTANCE_INTO_LINK&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;10001&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.3&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My &lt;STRONG&gt;target deliverable&lt;/STRONG&gt; is a third table that lists all links&amp;nbsp;within a certain distance of the starting&amp;nbsp;point. In this case, the distance is 1.5 units.&amp;nbsp;I will need to do a bit of math to calculate the last three columns, in between each iteration.&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;START_POINT_ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;LINK_ID&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;START_POSITION_ON_LINK&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;END_POSITION_ON_LINK&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;CUMULATIVE_DISTANCE&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;10001&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;100&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.3&lt;/P&gt;&lt;/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&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Conceptually simple, but I’m not sure of the best way to code this. The logical approach is to just perform datastep merges over and over (using a macro) to keep finding the next LINK(s) based on matching the nodes, until the cumulative distance reaches the specified distance (1.5 units), then start over for the next START_POINT_ID. But that requires a lot of overhead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Is this "iterative conditional lookup"&amp;nbsp;something that could be done within a single datastep?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 08 Nov 2018 21:06:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511535#M137674</guid>
      <dc:creator>desertsp2</dc:creator>
      <dc:date>2018-11-08T21:06:45Z</dc:date>
    </item>
    <item>
      <title>Re: iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511543#M137678</link>
      <description>&lt;P&gt;In case my example is too specific, this is probably similar to what I'm doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://en.wikipedia.org/wiki/Connected_component_(graph_theory)" target="_blank"&gt;https://en.wikipedia.org/wiki/Connected_component_(graph_theory)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;

Edit: &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; to fix link</description>
      <pubDate>Thu, 08 Nov 2018 21:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511543#M137678</guid>
      <dc:creator>desertsp2</dc:creator>
      <dc:date>2018-11-08T21:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511545#M137679</link>
      <description>This is a more complex problem, and I think you'll get the best solution if you post a really clear example. A small data set that shows what you have and EXACTLY what output would be expected from that input.</description>
      <pubDate>Thu, 08 Nov 2018 21:37:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511545#M137679</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-08T21:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511547#M137680</link>
      <description>&lt;P&gt;My initial post shows the exact data and exact results. I could expand to include additional records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would you suggest I create dataline code&amp;nbsp;so responders can easily work with actual SAS datasets?&lt;/P&gt;</description>
      <pubDate>Thu, 08 Nov 2018 21:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511547#M137680</guid>
      <dc:creator>desertsp2</dc:creator>
      <dc:date>2018-11-08T21:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511548#M137681</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/140174"&gt;@desertsp2&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you know PGStats's SubGraphs macro? Please see &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-find-all-connected-components-in-a-graph/ta-p/231539" target="_blank"&gt;How to find all connected components in a graph&lt;/A&gt;&lt;SPAN&gt;. (Just as a starting point.) There are also several threads about similar problems in the community archives. "subgraphs" is a suitable keyword to find some of them.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Nov 2018 21:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511548#M137681</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-11-08T21:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511560#M137686</link>
      <description>&lt;P&gt;Thanks! I'll do some further research and see if this has already been addressed.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Nov 2018 22:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511560#M137686</guid>
      <dc:creator>desertsp2</dc:creator>
      <dc:date>2018-11-08T22:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511618#M137706</link>
      <description>&lt;P&gt;Something like this may work:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create index from_node on links(from_node);
quit;

data want;
  merge points(in=ok) links;
  by link_id;
  if ok;
  start_position_on_link=distance_into_link;
  end_position_on_link=link_total_length;
  cumulative_distance=link_total_length-distance_into_link;
  keep start_point_id link_id start_position_on_link end_position_on_link cumulative_distance from_node to_node;
run;

data want;
  modify want;
  from_node=to_node;
  distance_so_far=cumulative_distance;&lt;BR /&gt;  do from_node=.,to_node;
    do until(0);
      set links key=from_node;
      if _iorc_ then leave;
      start_position_on_link=0;
      end_position_on_link=link_total_length;
      cumulative_distance=distance_so_far+link_total_length;
      output;
      end;&lt;BR /&gt;   end;
  _error_=0;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;- except that I do not understand your calculation of the cumulated distance (nor why the start_position_on_link should be 0.1 on the last obs), so I made that up.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The idea is to make an iterative travel through the tree by adding nodes in the end of the table, which will then be read iteratively. The _error_ variable is set to 0 after the loop ends, because it would create an error message every time there are no more nodes to read.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: I put in the outer loop (do from_node=.,to_node) to make the datastep reread the whole set of nodes, if there happens to be a point lying on the ending link of the last link read from the previous point.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Nov 2018 09:36:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511618#M137706</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-11-09T09:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: iterative join to accumulate records from a second table (linear-referencing)?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511652#M137716</link>
      <description>&lt;P&gt;The following code could get connected component.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards ;
input from $  to $ ;
cards;
1     2
1     3
4     5
5     2
9     4
6     7
8     7
;
run;
data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node;
  output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 20);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full(where=(from is not missing and to is not missing))',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Nov 2018 12:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/iterative-join-to-accumulate-records-from-a-second-table-linear/m-p/511652#M137716</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-11-09T12:40:16Z</dc:date>
    </item>
  </channel>
</rss>

