<?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: How to minimize function per group of observations with Proc Optmodel? in Mathematical Optimization, Discrete-Event Simulation, and OR</title>
    <link>https://communities.sas.com/t5/Mathematical-Optimization/How-to-minimize-function-per-group-of-observations-with-Proc/m-p/469256#M2273</link>
    <description>&lt;P&gt;The argument to UW_acc should be an element of the set OBS. but you are passing OBS_BY, which is instead a subset of OBS.&amp;nbsp; Same story for EP_incr and Time.&amp;nbsp; What does your pat_opt function expect as input?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, you can combine the READ DATA statements as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;read data work.pat_data into OBS=[_n_] pat_ID=pat_ID UW_acc=UW_pattern_acc EP_incr=EP_pattern_incr Time=Year;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 11 Jun 2018 14:20:20 GMT</pubDate>
    <dc:creator>RobPratt</dc:creator>
    <dc:date>2018-06-11T14:20:20Z</dc:date>
    <item>
      <title>How to minimize function per group of observations with Proc Optmodel?</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/How-to-minimize-function-per-group-of-observations-with-Proc/m-p/469160#M2272</link>
      <description>&lt;P&gt;Hi, I am trying to minimize a function (that I created with proc fcmp) per group of observations that are defined by a category column. I tried using this example (&lt;A href="http://support.sas.com/kb/42/332.html" target="_blank"&gt;http://support.sas.com/kb/42/332.html&lt;/A&gt;) for by group processing with proc optmodel but I'm not sure if the way I'm indexing the observations is correct.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options cmplib=work.funcs;
%let byvar=pat_ID;

proc optmodel cdigits=10 fdigits=10 pdigits=9;

	set OBS;
	str pat_ID {OBS};
	num UW_acc{OBS};
	num EP_incr{OBS};
	num Time{OBS};

	read data work.pat_data into OBS=[_n_] pat_ID=pat_ID;
	read data work.pat_data into OBS=[_n_] UW_acc=UW_pattern_acc;
	read data work.pat_data into OBS=[_n_] EP_incr=EP_pattern_incr;
	read data work.pat_data into OBS=[_n_] Time=Year;

	/* Collect values of BY variable into an index set */
	set BYSET = setof {i in OBS} &amp;amp;byvar.[i];
	put BYSET=;
	str by;

	/* variables */
	set OBS_BY = {i in OBS: &amp;amp;byvar.[i] = by};

	var a &amp;gt;=&amp;amp;LOW_A. &amp;lt;= &amp;amp;UP_A. init &amp;amp;INITA.;
	var b &amp;gt;=&amp;amp;LOW_B. &amp;lt;= &amp;amp;UP_B. init &amp;amp;INITB.;
	
	minimize FuncVal = pat_opt(a, b, UW_acc[OBS_BY], EP_incr[OBS_BY], Time[OBS_PY]);

	/*variables to store result*/
	num a_sol {BYSET};
	num b_sol {BYSET};

	do by = BYSET;
		put by=;
		solve with nlp / multistart=(maxstarts=&amp;amp;MAXSTART.) seed=&amp;amp;SEED. maxiter=&amp;amp;MAXITER. opttol=&amp;amp;OPTTOL. feastol=&amp;amp;FEASTOL.;
		print a b;
		a_sol[by] = a.sol;
		b_sol[by] = b.sol;
	end;

	create data work.solver_results from [by] a=a_sol b=b_sol;

run; 

proc print data=work.solver_results;
run;

quit;

TITLE;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When running the code I get "subscript 1 may not be a set" as an error for this line for each variable uw_acc, ep_incr and time.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;minimize FuncVal = pat_opt(a, b, UW_acc[OBS_BY], EP_incr[OBS_BY], Time[OBS_PY]);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me know, if you need more info!&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I'm using SAS Version 9.4)&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 11:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/How-to-minimize-function-per-group-of-observations-with-Proc/m-p/469160#M2272</guid>
      <dc:creator>xyz3</dc:creator>
      <dc:date>2018-06-11T11:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to minimize function per group of observations with Proc Optmodel?</title>
      <link>https://communities.sas.com/t5/Mathematical-Optimization/How-to-minimize-function-per-group-of-observations-with-Proc/m-p/469256#M2273</link>
      <description>&lt;P&gt;The argument to UW_acc should be an element of the set OBS. but you are passing OBS_BY, which is instead a subset of OBS.&amp;nbsp; Same story for EP_incr and Time.&amp;nbsp; What does your pat_opt function expect as input?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, you can combine the READ DATA statements as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;read data work.pat_data into OBS=[_n_] pat_ID=pat_ID UW_acc=UW_pattern_acc EP_incr=EP_pattern_incr Time=Year;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Jun 2018 14:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Mathematical-Optimization/How-to-minimize-function-per-group-of-observations-with-Proc/m-p/469256#M2273</guid>
      <dc:creator>RobPratt</dc:creator>
      <dc:date>2018-06-11T14:20:20Z</dc:date>
    </item>
  </channel>
</rss>

