<?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 Appending to a Dataset in Optmodel in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/Appending-to-a-Dataset-in-Optmodel/m-p/470078#M2277</link>
    <description>&lt;P&gt;Hi.&amp;nbsp; I am working on a shortest-distance problem where I am finding the shortest distance between every room on a certain floor in a certain building. My goal is to output the dataset in the following manner:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;room1 room2 dist&lt;/P&gt;&lt;P&gt;T4-1&amp;nbsp; &amp;nbsp; T4-2&amp;nbsp; &amp;nbsp; 5&lt;/P&gt;&lt;P&gt;T4-1&amp;nbsp; &amp;nbsp; T4-3&amp;nbsp; &amp;nbsp; 6&lt;/P&gt;&lt;P&gt;etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
	set&amp;lt;string, string&amp;gt; arcs;
	num edge {arcs};
	set &amp;lt;string&amp;gt; rooms;
	set nodes = union {&amp;lt;i, j&amp;gt; in arcs} {i, j}; /* used to refer to each node singly, and not as an &amp;lt;i, j&amp;gt; tuple */
	string source init &amp;amp;source;
	string sink init &amp;amp;sink;

	read data distinct_rooms into rooms=[node_1];

	var flow {arcs} &amp;gt;=0;

	min totalCost = sum {&amp;lt;i, j&amp;gt; in arcs} edge[i, j] * flow[i, j];

	con bal {i in nodes}:
		sum {&amp;lt;(i), j&amp;gt; in arcs} flow[i, j] - sum{&amp;lt;j, (i)&amp;gt; in arcs} flow[j, i] =  (if i = source then 1 
																				else if i = sink then -1 
																				else 0);

	set t4_1f_rooms = {s in rooms: substr(s, 1, 5) = 'T4_R1'};
	/*set t4_1f_rooms = {'T4_R103', 'T4_R129', 'T4_R151', 'T4_R176'}; */

	read data t4_1f_arcs into arcs=[node_1 node_2] edge; 

/*	create data t4_1f_room_dist from i j dist;*/

	impvar r2rdist = sum{&amp;lt;i, j&amp;gt; in arcs: flow[i,j].sol &amp;gt; 1e-6} edge[i, j];

	cofor{so in t4_1f_rooms, si in t4_1f_rooms} do;
		if so = si then continue;
			source = so;
			sink = si;

		solve;

/*		create data (source || '_' || sink) from [i j]={&amp;lt;i,j&amp;gt; in arcs: flow[i, j].sol &amp;gt; 1e-6} flow edge;*/

		create data ('sum_' || source || '_' || sink) from source sink r2rdist;
/*		create data soldata from source sink r2rdist;*/
	end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I use an impvar in order to sum the edges of the traversed nodes, but for each solution this results in a dataset with one observation and I want to append the solution for each room to room combo to an existing dataset instead of creating a new one so I don't have hundreds of output data sets.&amp;nbsp; I can't perform the append in a data step as the sheer number of output data sets causes EG to crash.&amp;nbsp; The solver runs fine when data set output is suppressed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought of declaring a new problem each iteration and referencing each problem's solution in another loop, but apparently problem declarations cannot be nested statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to append the output to an existing data set within optmodel?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jun 2018 20:03:38 GMT</pubDate>
    <dc:creator>yus03590</dc:creator>
    <dc:date>2018-06-13T20:03:38Z</dc:date>
    <item>
      <title>Appending to a Dataset in Optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Appending-to-a-Dataset-in-Optmodel/m-p/470078#M2277</link>
      <description>&lt;P&gt;Hi.&amp;nbsp; I am working on a shortest-distance problem where I am finding the shortest distance between every room on a certain floor in a certain building. My goal is to output the dataset in the following manner:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;room1 room2 dist&lt;/P&gt;&lt;P&gt;T4-1&amp;nbsp; &amp;nbsp; T4-2&amp;nbsp; &amp;nbsp; 5&lt;/P&gt;&lt;P&gt;T4-1&amp;nbsp; &amp;nbsp; T4-3&amp;nbsp; &amp;nbsp; 6&lt;/P&gt;&lt;P&gt;etc&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc optmodel;
	set&amp;lt;string, string&amp;gt; arcs;
	num edge {arcs};
	set &amp;lt;string&amp;gt; rooms;
	set nodes = union {&amp;lt;i, j&amp;gt; in arcs} {i, j}; /* used to refer to each node singly, and not as an &amp;lt;i, j&amp;gt; tuple */
	string source init &amp;amp;source;
	string sink init &amp;amp;sink;

	read data distinct_rooms into rooms=[node_1];

	var flow {arcs} &amp;gt;=0;

	min totalCost = sum {&amp;lt;i, j&amp;gt; in arcs} edge[i, j] * flow[i, j];

	con bal {i in nodes}:
		sum {&amp;lt;(i), j&amp;gt; in arcs} flow[i, j] - sum{&amp;lt;j, (i)&amp;gt; in arcs} flow[j, i] =  (if i = source then 1 
																				else if i = sink then -1 
																				else 0);

	set t4_1f_rooms = {s in rooms: substr(s, 1, 5) = 'T4_R1'};
	/*set t4_1f_rooms = {'T4_R103', 'T4_R129', 'T4_R151', 'T4_R176'}; */

	read data t4_1f_arcs into arcs=[node_1 node_2] edge; 

/*	create data t4_1f_room_dist from i j dist;*/

	impvar r2rdist = sum{&amp;lt;i, j&amp;gt; in arcs: flow[i,j].sol &amp;gt; 1e-6} edge[i, j];

	cofor{so in t4_1f_rooms, si in t4_1f_rooms} do;
		if so = si then continue;
			source = so;
			sink = si;

		solve;

/*		create data (source || '_' || sink) from [i j]={&amp;lt;i,j&amp;gt; in arcs: flow[i, j].sol &amp;gt; 1e-6} flow edge;*/

		create data ('sum_' || source || '_' || sink) from source sink r2rdist;
/*		create data soldata from source sink r2rdist;*/
	end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I use an impvar in order to sum the edges of the traversed nodes, but for each solution this results in a dataset with one observation and I want to append the solution for each room to room combo to an existing dataset instead of creating a new one so I don't have hundreds of output data sets.&amp;nbsp; I can't perform the append in a data step as the sheer number of output data sets causes EG to crash.&amp;nbsp; The solver runs fine when data set output is suppressed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought of declaring a new problem each iteration and referencing each problem's solution in another loop, but apparently problem declarations cannot be nested statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to append the output to an existing data set within optmodel?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jun 2018 20:03:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Appending-to-a-Dataset-in-Optmodel/m-p/470078#M2277</guid>
      <dc:creator>yus03590</dc:creator>
      <dc:date>2018-06-13T20:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: Appending to a Dataset in Optmodel</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/Appending-to-a-Dataset-in-Optmodel/m-p/470170#M2279</link>
      <description>&lt;P&gt;You can call PROC APPEND from a SUBMIT block:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;      submit;
         proc append base=ds1 data=ds2;
         run;
      endsubmit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another approach is to save the results in a numeric parameter, indexed by sources and sinks, and then call CREATE DATA only once, outside the COFOR loop.&amp;nbsp; This approach is illustrated in &lt;A href="http://go.documentation.sas.com/?docsetId=ormpex&amp;amp;docsetTarget=ormpex_ex22_toc.htm&amp;amp;docsetVersion=14.3&amp;amp;locale=en" target="_self"&gt;this documentation example&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you can also use the network solver (instead of LP) to solve shortest path problems in PROC OPTMODEL.&amp;nbsp; In fact, the default is all-pairs shortest paths, so you&amp;nbsp;would need to call the network solver only once.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jun 2018 02:28:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/Appending-to-a-Dataset-in-Optmodel/m-p/470170#M2279</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2018-06-14T02:28:14Z</dc:date>
    </item>
  </channel>
</rss>

