<?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: FCMP - solving equation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768553#M243784</link>
    <description>&lt;P&gt;Good to know that only solutions in the interval (0, 1) are of interest. Normally, the algorithm used by the SOLVE function should converge to a particular solution if the initial value is close enough to that solution. Hence, you could use initial values like, for example, 0.01, 0.02, ..., 0.99 in a loop until a solution is found. Or enforce a positive solution by using a substitution similar to what I did in the &lt;A href="https://communities.sas.com/t5/SAS-Programming/Solving-Equation/m-p/766245/highlight/true#M242829" target="_blank" rel="noopener"&gt;solution of the earlier thread&lt;/A&gt;. Another option would be to switch from SOLVE to the bisection method mentioned in that other post.&amp;nbsp;Can you share an example where you get a negative solution in &lt;FONT face="courier new,courier"&gt;r2&lt;/FONT&gt;, i.e., with the initial value 1? Then I can have a look which of these options is most suitable.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Mon, 20 Sep 2021 13:28:31 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-09-20T13:28:31Z</dc:date>
    <item>
      <title>FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767843#M243475</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;To solve equation below to calculate the unknown r:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MAC1430_0-1631687463560.png" style="width: 393px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63606i2544CFE4A105D53F/image-dimensions/393x174?v=v2" width="393" height="174" role="button" title="MAC1430_0-1631687463560.png" alt="MAC1430_0-1631687463560.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Data looks like:&lt;/P&gt;
&lt;PRE&gt;data test;
input stock$ year P	B	B1	B2	B3	B4	F	F1	F2	F3	F4	F5	g;
datalines;
A	1	80.16	12.1492	14.7642	17.7592	21.1642	24.6292	3.8886	5.23	5.99	6.81	6.93	6.63	0.3038
A	2	63.54	15.3785	18.4785	21.9535	25.8685	29.9185	4.8659	6.2	6.95	7.83	8.1	8.51	0.3769
A	3	41.19	11.5507	15.1507	19.1257	23.5857	28.3907	4.5454	7.2	7.95	8.92	9.61	10.47	0.205
A	4	35.28	15.1777	18.7327	22.7277	27.1727	32.2727	4.3858	7.11	7.99	8.89	10.2	11.14	0.1478
A	5	52	14.9545	18.3545	22.1945	26.4395	31.1195	5.7079	6.8	7.68	8.49	9.36	11.07	0.0644
A	6	50.78	15.8837	19.6137	23.8137	28.4137	33.5537	6.1861	7.46	8.4	9.2	10.28	11.43	-0.0636
A	7	67.64	19.3065	23.2765	27.6065	32.3565	37.6015	6.7226	7.94	8.66	9.5	10.49	12.24	-0.0915
A	8	72.05	21.6415	26.5265	31.9415	37.8015	44.3815	7.7686	9.77	10.83	11.72	13.16	14.59	-0.0553
A	9	74.51	23.6561	30.0461	36.9961	44.5511	52.4911	8.804	12.78	13.9	15.11	15.88	11.32	0.0005
A	10	43.05	27.2373	34.5373	42.7023	51.6873	61.0373	9.0739	14.6	16.33	17.97	18.7	16.621	0.123
B	1	37.8	6.3668	7.6503	9.0298	10.5533	12.2488	2.7893	2.567	2.759	3.047	3.391	3.807	0.3038
B	2	35.4	7.6967	8.9367	10.2797	11.7422	13.3282	3.238	2.48	2.686	2.925	3.172	3.46	0.3769
B	3	29.3	8.6812	10.0817	11.5857	13.2137	14.9497	3.2122	2.801	3.008	3.256	3.472	3.778	0.205
B	4	30.9	8.999	10.3945	11.8675	13.4585	15.18	3.2352	2.791	2.946	3.182	3.443	3.753	0.1478
B	5	29.75	9.5064	10.9409	12.4669	14.1014	15.9644	3.5381	2.869	3.052	3.269	3.726	3.942	0.0644
B	6	39.3	10.1293	11.6418	13.2513	14.9848	16.8748	3.4308	3.025	3.219	3.467	3.78	3.954	-0.0636
B	7	43.3	12.2344	13.8109	15.4949	17.3094	19.3754	3.7561	3.153	3.368	3.629	4.132	4.534	-0.0915
B	8	52	13.2885	14.78	16.4125	18.216	20.2585	4.2523	2.983	3.265	3.607	NA	NA	-0.0553
B	9	41.6	13.9864	15.6949	17.5539	19.5994	21.7379	4.7464	3.417	3.718	4.091	4.277	4.836	0.0005
B	10	50.2	14.043	16.067	18.2475	20.607	23.178	4.2598	4.048	4.361	4.719	5.142	5.474	0.123
C	1	24.19	1.4639	3.5839	4.8589	6.6539	9.1189	0.3939	4.24	2.55	3.59	4.93	5.63	0.3038
C	2	19.52	2.3384	3.9534	5.7434	7.9884	10.8534	1.3403	3.23	3.58	4.49	5.73	6.51	0.3769
C	3	7.96	2.626	3.711	5.381	7.581	10.846	-1.0251	2.17	3.34	4.4	6.53	5.9	0.205
C	4	15.72	2.7283	4.0083	5.8033	8.0983	11.6433	0.3156	2.56	3.59	4.59	7.09	7.28	0.1478
C	5	11.81	2.3651	3.9451	5.7001	7.9701	10.8651	0.049	3.16	3.51	4.54	5.79	6.39	0.0644
C	6	16.9	2.8771	4.4821	6.5371	9.0671	11.6921	0.964	3.21	4.11	5.06	5.25	7.21	-0.0636
C	7	18.84	3.532	5.792	8.587	12.027	16.417	1.1485	4.52	5.59	6.88	NA	NA	-0.0915
C	8	21.66	4.5199	7.5199	11.0849	15.4049	20.4099	2.0169	6	7.13	8.64	10.01	11.18	-0.0553
C	9	12.75	4.379	7.449	11.774	16.944	23.244	2.0582	6.14	8.65	10.34	12.6	13.89	0.0005
C	10	24	4.6028	8.4778	13.4228	19.5128	26.4078	0.9412	7.75	9.89	12.18	13.79	16.74	0.123
;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;This is a variant of a previous post with a different data layout (&lt;/SPAN&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Solving-Equation/m-p/766280#M242843" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Solving-Equation/m-p/766280#M242843&lt;/A&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 04:29:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767843#M243475</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-10-10T04:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767845#M243476</link>
      <description>&lt;P&gt;&amp;nbsp;Did you try to modify&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;s solution?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 06:55:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767845#M243476</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-09-15T06:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767847#M243478</link>
      <description>yes, I tried and still trying, got error "ERROR 71-185: The xrate function call does not have enough arguments".&lt;BR /&gt;</description>
      <pubDate>Wed, 15 Sep 2021 07:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767847#M243478</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-09-15T07:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767857#M243484</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;yes, I tried and still trying, got error "ERROR 71-185: The xrate function call does not have enough arguments".&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please show your code (in a code box opened with the "running man" button) including both the PROC FCMP step defining the new XRATE function and the DATA step calling it. Then we'll have a chance to diagnose what went wrong. In particular, we'll see how you defined the function and what arguments you may be missing in the function call.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Sep 2021 09:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/767857#M243484</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-15T09:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768021#M243561</link>
      <description>&lt;P&gt;Thank you very much Reinhard, below is my current try (could be silly) by modifying your code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test;
input stock$ y P	B	B1	B2	B3	B4	F	F1	F2	F3	F4	F5	g;
datalines;
A	1	80.16	12.1492	14.7642	17.7592	21.1642	24.6292	3.8886	5.23	5.99	6.81	6.93	6.63	0.3038
A	2	63.54	15.3785	18.4785	21.9535	25.8685	29.9185	4.8659	6.2	6.95	7.83	8.1	8.51	0.3769
A	3	41.19	11.5507	15.1507	19.1257	23.5857	28.3907	4.5454	7.2	7.95	8.92	9.61	10.47	0.205
A	4	35.28	15.1777	18.7327	22.7277	27.1727	32.2727	4.3858	7.11	7.99	8.89	10.2	11.14	0.1478
A	5	52	14.9545	18.3545	22.1945	26.4395	31.1195	5.7079	6.8	7.68	8.49	9.36	11.07	0.0644
A	6	50.78	15.8837	19.6137	23.8137	28.4137	33.5537	6.1861	7.46	8.4	9.2	10.28	11.43	-0.0636
A	7	67.64	19.3065	23.2765	27.6065	32.3565	37.6015	6.7226	7.94	8.66	9.5	10.49	12.24	-0.0915
A	8	72.05	21.6415	26.5265	31.9415	37.8015	44.3815	7.7686	9.77	10.83	11.72	13.16	14.59	-0.0553
A	9	74.51	23.6561	30.0461	36.9961	44.5511	52.4911	8.804	12.78	13.9	15.11	15.88	11.32	0.0005
A	10	43.05	27.2373	34.5373	42.7023	51.6873	61.0373	9.0739	14.6	16.33	17.97	18.7	16.621	0.123
B	1	37.8	6.3668	7.6503	9.0298	10.5533	12.2488	2.7893	2.567	2.759	3.047	3.391	3.807	0.3038
B	2	35.4	7.6967	8.9367	10.2797	11.7422	13.3282	3.238	2.48	2.686	2.925	3.172	3.46	0.3769
B	3	29.3	8.6812	10.0817	11.5857	13.2137	14.9497	3.2122	2.801	3.008	3.256	3.472	3.778	0.205
B	4	30.9	8.999	10.3945	11.8675	13.4585	15.18	3.2352	2.791	2.946	3.182	3.443	3.753	0.1478
B	5	29.75	9.5064	10.9409	12.4669	14.1014	15.9644	3.5381	2.869	3.052	3.269	3.726	3.942	0.0644
B	6	39.3	10.1293	11.6418	13.2513	14.9848	16.8748	3.4308	3.025	3.219	3.467	3.78	3.954	-0.0636
B	7	43.3	12.2344	13.8109	15.4949	17.3094	19.3754	3.7561	3.153	3.368	3.629	4.132	4.534	-0.0915
B	8	52	13.2885	14.78	16.4125	18.216	20.2585	4.2523	2.983	3.265	3.607	NA	NA	-0.0553
B	9	41.6	13.9864	15.6949	17.5539	19.5994	21.7379	4.7464	3.417	3.718	4.091	4.277	4.836	0.0005
B	10	50.2	14.043	16.067	18.2475	20.607	23.178	4.2598	4.048	4.361	4.719	5.142	5.474	0.123
C	1	24.19	1.4639	3.5839	4.8589	6.6539	9.1189	0.3939	4.24	2.55	3.59	4.93	5.63	0.3038
C	2	19.52	2.3384	3.9534	5.7434	7.9884	10.8534	1.3403	3.23	3.58	4.49	5.73	6.51	0.3769
C	3	7.96	2.626	3.711	5.381	7.581	10.846	-1.0251	2.17	3.34	4.4	6.53	5.9	0.205
C	4	15.72	2.7283	4.0083	5.8033	8.0983	11.6433	0.3156	2.56	3.59	4.59	7.09	7.28	0.1478
C	5	11.81	2.3651	3.9451	5.7001	7.9701	10.8651	0.049	3.16	3.51	4.54	5.79	6.39	0.0644
C	6	16.9	2.8771	4.4821	6.5371	9.0671	11.6921	0.964	3.21	4.11	5.06	5.25	7.21	-0.0636
C	7	18.84	3.532	5.792	8.587	12.027	16.417	1.1485	4.52	5.59	6.88	NA	NA	-0.0915
C	8	21.66	4.5199	7.5199	11.0849	15.4049	20.4099	2.0169	6	7.13	8.64	10.01	11.18	-0.0553
C	9	12.75	4.379	7.449	11.774	16.944	23.244	2.0582	6.14	8.65	10.34	12.6	13.89	0.0005
C	10	24	4.6028	8.4778	13.4228	19.5128	26.4078	0.9412	7.75	9.89	12.18	13.79	16.74	0.123
;
/* Define function XRATE */
proc fcmp outlib=work.funcs.test;
function rs(w, y, B, B1, B2, B3, B4, F1, F2, F3, F4, F5, g, x);
  select(w);
    when(1) z=-1-exp(-x*1e4); /* for a solution &amp;lt; -1 */
    when(2) z=-1+exp(-x*1e4); /* for a solution &amp;gt; -1 */
    when(3) z=g+sign((F5-g*B4)*(1+g))*exp(-x*1e4); /* for a solution near g */
  end;
     p = (F1/(g+z)+F2/(g+z)**2+F3/(g+z)**3+F4/(g+z)**4+F5/(g+z)**5)-(z+g)*(B/(1+g+z)+B1/(1+g+z)**2+B2/(1+g+z)**3+B3/(1+g+z)**4+B4/(1+g+z)**5)+(F5-(g+z)*B4)*(1+g)/(z*(1+g+z)**5);
    return(p);
endsub;
function xrate(w, y, p, B, B1, B2, B3, B4, F1, F2, F3, F4, F5, g);
  array solvopts[5] initial abconv relconv maxiter status (0.001 0.001 1.0e-6 100);
  t=exp(-solve("RS", solvopts, p, w, y, B, B1, B2, B3, B4, F1, F2, F3, F4, F5, g, .)*1e4);
  select(w);
    when(1) r=-1-t; /* solution &amp;lt; -1 */
    when(2) r=-1+t; /* solution &amp;gt; -1 */
    when(3) r=g+sign((F5-g*B4)*(1+g))*t; /* solution near g */
  end;
  return(r);
endfunc;
run;

/* Make function available */

options cmplib=work.funcs;

/* Apply function XRATE to dataset TEST */

data want;
array _B[99] _temporary_;
array _B1[99] _temporary_;
array _B2[99] _temporary_;
array _B3[99] _temporary_;
array _B4[99] _temporary_;
array _P[99] _temporary_;
array _F1[99] _temporary_;
array _F2[99] _temporary_;
array _F3[99] _temporary_;
array _F4[99] _temporary_;
array _F5[99] _temporary_;
do until(last.stock);
  set test;
  by stock;
  r1=xrate(1,y,_P,_B,_B1,_B2,_B3,_B4,_F1,_F2,_F3,_F4,_F5,g);
  r2=xrate(2,y,_P,_B,_B1,_B2,_B3,_B4,_F1,_F2,_F3,_F4,_F5,g);
  r3=xrate(3,y,_P,_B,_B1,_B2,_B3,_B4,_F1,_F2,_F3,_F4,_F5,g);
  output;
end;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Sep 2021 04:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768021#M243561</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-09-16T04:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768042#M243567</link>
      <description>&lt;P&gt;Thanks for providing your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The new, wide data structure has the array elements B1, B2, ... and F1, F2, ... in one observation, which simplifies the code: We don't need the &lt;FONT face="courier new,courier"&gt;do until(last.stock)&lt;/FONT&gt; loop any longer. Each observation populates the parameters of the nonlinear equation. There is no need to define individual arrays _B1, _B2, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had a new idea: It might help to multiply the equation by &lt;FONT face="courier new,courier"&gt;(r-g)(1+r)**5&lt;/FONT&gt;. Thus we'd get rid of the poles and we'd have to deal only with a &lt;EM&gt;polynomial&lt;/EM&gt; equation in &lt;EM&gt;r&lt;/EM&gt; or perhaps in &lt;EM&gt;u:=1+r&lt;/EM&gt; (of degree 6 in general, with 12 parameters*). This should be numerically more robust. Of course, &lt;FONT face="courier new,courier"&gt;r=g&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;r=-1&lt;/FONT&gt; are still inadmissible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* Your equation&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MAC1430_0-1631687463560.png" style="width: 642px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63606i2544CFE4A105D53F/image-dimensions/642x284?v=v2" width="642" height="284" role="button" title="MAC1430_0-1631687463560.png" alt="MAC1430_0-1631687463560.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;has parameters &lt;EM&gt;B, B1, ..., B4, F1, ..., F5&lt;/EM&gt;, &lt;EM&gt;p&lt;/EM&gt; and &lt;EM&gt;g&lt;/EM&gt;. Your sample dataset TEST contains a 13th parameter, &lt;EM&gt;F&lt;/EM&gt;&amp;nbsp;(with values that we saw for &lt;EM&gt;F1, F2,&lt;/EM&gt; ... in the equation of the previous thread). Where does this come into play, or can we ignore it?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Sep 2021 08:05:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768042#M243567</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-16T08:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768207#M243643</link>
      <description>Thank you very much Reinhard, F was used to calculate F1 to F5, yes, we can ignore F for this equation.</description>
      <pubDate>Fri, 17 Sep 2021 01:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768207#M243643</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-09-17T01:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768302#M243681</link>
      <description>&lt;P&gt;Thanks for the clarification.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used the idea mentioned in my previous post and turned the task into finding real zeros of a polynomial in &lt;EM&gt;r&lt;/EM&gt; (of grade 6)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;k(r) = c6*r**6 + c5*r**5 + c4*r**4 + c3*r**3 + c2*r**2 + c1*r + c0 = 0&lt;/PRE&gt;
&lt;P&gt;Most of the work went into determining the coefficients &lt;FONT face="courier new,courier"&gt;c0&lt;/FONT&gt;, ..., &lt;FONT face="courier new,courier"&gt;c6&lt;/FONT&gt;&amp;nbsp;as functions of the original 12 parameters. (This is not necessary for computing values of &lt;FONT face="courier new,courier"&gt;k(r)&lt;/FONT&gt;, but it helps to understand the behavior of &lt;FONT face="courier new,courier"&gt;k(r)&lt;/FONT&gt;&amp;nbsp;-- see below -- and can be useful for various techniques of numerical mathematics that could be applied to it.) To this end I subtracted the right-hand side of your equation from &lt;EM&gt;p&lt;/EM&gt;, multiplied the resulting equation &lt;SPAN&gt;by&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;(r-g)*(1+r)**5&lt;/FONT&gt;&amp;nbsp;and then collected the terms for &lt;FONT face="courier new,courier"&gt;r**6&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;r**5&lt;/FONT&gt; and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The graphs of several cases (taken from your sample dataset TEST) showed a common pattern: one solution in the interval (-1, 0) and one in the interval&amp;nbsp;(0, 1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More generally, the constant term of the polynomial,&lt;/P&gt;
&lt;PRE&gt;c0 = g*(B+F1+F2+F3+F4-p)-F5&lt;/PRE&gt;
&lt;P&gt;is negative for each of the 28 observations in TEST with complete sets of parameters (two have "NA" for &lt;FONT face="courier new,courier"&gt;F4&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;F5&lt;/FONT&gt;) and the leading coefficient&lt;/P&gt;
&lt;PRE&gt;c6 = p&lt;/PRE&gt;
&lt;P&gt;is positive throughout your sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That is, &lt;FONT face="courier new,courier"&gt;k(0)&amp;lt;0&lt;/FONT&gt; while &lt;FONT face="courier new,courier"&gt;k(r)&lt;/FONT&gt; goes to infinity for r&amp;nbsp;→ ∞ and&amp;nbsp;r&amp;nbsp;→ −∞. This implies the existence of at least one positive and at least one negative solution. These are also solutions of the original problem, unless they happen to equal&amp;nbsp;−1 or &lt;EM&gt;g&lt;/EM&gt; (the poles), which should be very unlikely. The definition of function XRATE2 below checks this anyway.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used −1 (which is a valid argument for the polynomial &lt;EM&gt;k&lt;/EM&gt;, although it's a pole of the original function) and 1 as initial values to find the anticipated solutions around zero.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Define function XRATE2 */

proc fcmp outlib=work.funcs.test;
function k(r, p, B[*], F[*], g);
  c6= p;
  c5= p*(5-g)-F[1]/2;
  c4= p*(10-5*g)-B[1]+B[2]+(g-5)*F[1]/2-F[2]/2;
  c3= 10*p*(1-g)+(g-3)*B[1]+(2-g)*B[2]+B[3]+((5*g-9)*F[1]+(g-4)*F[2]-F[3])/2;
  c2= p*(5-10*g)+3*(g-1)*B[1]+(1-2*g)*B[2]+(1-g)*B[3]+B[4]+((9*g-7)*F[1]+(4*g-5)*F[2]+(g-3)*F[3]-F[4])/2;
  c1= p*(1-5*g)+(3*g-1)*B[1]-g*(B[2]+B[3]+B[4])-(1+g)*B[5]+((7*g-2)*F[1]+(5*g-2)*F[2]+(3*g-2)*F[3]+(g-2)*F[4])/2-F[5];
  c0= -p*g+g*B[1]+g*(F[1]+F[2]+F[3]+F[4])-F[5];
  z = c6*r**6 + c5*r**5 + c4*r**4 + c3*r**3 + c2*r**2 + c1*r + c0;
  return(z);
endfunc;

function xrate2(i, p, B[*], F[*], g); /* first argument is the initial value */
  array solvopts[1] i;
  r=solve("k", solvopts, 0, ., p, B, F, g);
  if min(abs(1+r),abs(r-g))&amp;lt;1e-4 then r=.; /* avoid poles of the original function */
  return(r);
endfunc;
run;

/* Make function available */

options cmplib=work.funcs;

/* Apply function XRATE2 to dataset TEST */

data want(rename=(B0=B F0=F));
set test(rename=(B=B0 F=F0));
array B[*] B0-B4;
array F[*] F1-F5;
r1=xrate2(-1, p, B, F, g); /* solution found with initial value -1 */
r2=xrate2( 1, p, B, F, g); /* solution found with initial value  1 */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In each of the 28 complete cases one solution &lt;FONT face="courier new,courier"&gt;r1&lt;/FONT&gt; in the interval (−1, 0) and one solution &lt;FONT face="courier new,courier"&gt;r2&lt;/FONT&gt; in the interval&amp;nbsp;(0, 1) was found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I checked all of them using the original rational function definition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.funcs.test;
function h(r, p, B[*], F[*], g);
  u=1+r;
  s1=p-B[1];
  do t=1 to 5;
    s1 = s1 - F[t]/u**t;
  end;
  s2=B[1]/u;
  do t=1 to 4;
    s2 = s2 + (B[t]+F[t]/2)/u**(t+1);
  end;
  z = s1 + r*s2 - (F[5]+r*B[5])*(1+g)/((r-g)*u**5);
  return(z);
endfunc;
run;

data check;
set want(rename=(B=B0 F=F0));
array B[*] B0-B4;
array F[*] F1-F5;
diff1=h(r1, p, B, F, g);
diff2=h(r2, p, B, F, g);
run;

proc means data=check;
var diff:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The absolute values of the differences &lt;FONT face="courier new,courier"&gt;diff1&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;diff2&lt;/FONT&gt; (expected to be close to zero) were &lt;FONT face="courier new,courier"&gt;&amp;lt;1.5E-8&lt;/FONT&gt;&amp;nbsp;for your sample data. I'd recommend such a check for all solutions you will find with your real data.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 14:13:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768302#M243681</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-17T14:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768451#M243752</link>
      <description>&lt;P&gt;Thank you very much Reinhard,&lt;/P&gt;
&lt;P&gt;I will apply the code on other equations, so trying to understand every bit of the procedure here. I got two questions.&lt;/P&gt;
&lt;P&gt;First, I understand how c0 (the intercept) is derived. But, having trouble to get C1 to C6. Could you kindly explain it?&lt;/P&gt;
&lt;P&gt;Second, in the &lt;U&gt;check&lt;/U&gt; procedure, s2 is defined as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  s2 = s2 + (B[t]+F[t]/2)/u**(t+1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, the equation has terms of &lt;U&gt;(B[t]+F[t+1]/2)/u**(t+2)&lt;/U&gt; rather than (B[t]+F[t]/2)/u**(t+1). Such as,&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MAC1430_0-1632048061847.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63701i7F90F572E83B5A31/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MAC1430_0-1632048061847.png" alt="MAC1430_0-1632048061847.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So, I made an attempt to modify by introducing s3, and separate the above terms into two parts (B[t]/u**(t+&lt;STRONG&gt;2&lt;/STRONG&gt;), and (F[t]/&lt;STRONG&gt;2&lt;/STRONG&gt;)/u**(t+&lt;STRONG&gt;1&lt;/STRONG&gt;)) the code becomes:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.funcs.test;
function h(r, p, B[*], F[*], g);
  u=1+r;
  s1=p-B[1];
  do t=1 to 5;
    s1 = s1 - F[t]/u**t;
  end;
  s2=B[1]/u;
  do t=1 to 3;
    s2 = s2 + B[t]/u**(t+2);
  end;
  s3 = B[1]/u**2;
  do t=1 to 4;
    s3 = s3 + (F[t]/2)/u**(t+1);
  end;
  z = s1 + r*s2 + r*s3 - (F[5]+r*B[4])*(1+g)/((r-g)*u**5);
  return(z);
endfunc;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is my understanding correct? The diff1 and diff2 values become much bigger after this modification.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Sep 2021 10:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768451#M243752</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-09-19T10:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768460#M243755</link>
      <description>&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;First, I understand how c0 (the intercept) is derived. But, having trouble to get C1 to C6. Could you kindly explain it?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;After the multiplication by&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;(r-g)*(1+r)**5&lt;/SPAN&gt;&lt;/FONT&gt;&amp;nbsp;all denominators containing &lt;FONT face="courier new,courier"&gt;r&lt;/FONT&gt;&amp;nbsp;cancel out, but we obtain several terms containing powers of &lt;FONT face="courier new,courier"&gt;(1+r)&amp;nbsp;&lt;/FONT&gt;and the factor&amp;nbsp;&lt;FONT face="courier new,courier"&gt;(r-g)&lt;/FONT&gt;, now in the "numerator." Multiplying all these out (e.g., &lt;FONT face="courier new,courier"&gt;(1+r)**4 = 1+4*r+6*r**2+4*r**3+r**4&lt;/FONT&gt;) and aggregating the coefficients of &lt;FONT face="courier new,courier"&gt;r**j&lt;/FONT&gt; (&lt;FONT face="courier new,courier"&gt;j&lt;/FONT&gt;=0, ..., 6) we end up with a polynomial in &lt;FONT face="courier new,courier"&gt;r&lt;/FONT&gt; of grade 6. I did this calculation by hand, but computer algebra software would have helped to obtain the expressions for &lt;FONT face="courier new,courier"&gt;c0&lt;/FONT&gt;, ..., &lt;FONT face="courier new,courier"&gt;c6&lt;/FONT&gt; more easily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, the term &lt;FONT face="courier new,courier"&gt;F2/(1+r)**2&lt;/FONT&gt; in the original equation turns into &lt;FONT face="courier new,courier"&gt;-F2*(r-g)*(1+r)**3&lt;/FONT&gt; after bringing it to the left side of the equals sign and the multiplication by&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;(r-g)*(1+r)**5&lt;/SPAN&gt;&lt;/FONT&gt;. What does this term contribute to, say, &lt;FONT face="courier new,courier"&gt;c1&lt;/FONT&gt;, i.e., the coefficient of &lt;FONT face="courier new,courier"&gt;r**1=r&lt;/FONT&gt;? Answer (considering &lt;FONT face="courier new,courier"&gt;(1+r)**3=&lt;STRONG&gt;&lt;FONT color="#00CCFF"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;+&lt;STRONG&gt;&lt;FONT color="#FF00FF"&gt;3&lt;/FONT&gt;&lt;/STRONG&gt;*r+3*r**2+r**3&lt;/FONT&gt;)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt; &lt;FONT face="courier new,courier"&gt;-F2*(-g)*&lt;STRONG&gt;&lt;FONT color="#FF00FF"&gt;3&lt;/FONT&gt;&lt;/STRONG&gt; - F2*&lt;STRONG&gt;&lt;FONT color="#00CCFF"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; = (3*g - 1)*F2&lt;/FONT&gt;. Similarly, the term &lt;FONT face="courier new,courier"&gt;-r * (B1+0.5*F2)/(1+r)**3&lt;/FONT&gt; turns into &lt;FONT face="courier new,courier"&gt;r*(r-g)*(B1+0.5*F2)*(1+r)**2&lt;/FONT&gt;, which contributes &lt;FONT face="courier new,courier"&gt;-g*(B1+0.5*F2)*1&lt;/FONT&gt;&amp;nbsp;to &lt;FONT face="courier new,courier"&gt;c1&lt;/FONT&gt;. Together (focusing on the part containing &lt;FONT face="courier new,courier"&gt;F2&lt;/FONT&gt;), &lt;FONT face="courier new,courier"&gt;(3*g - 1)*F2 - g*0.5*F2 = &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;(5*g-2)*F2/2&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;. This is how I obtained the highlighted part of the term for &lt;FONT face="courier new,courier"&gt;c1&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;c1= p*(1-5*g)+(3*g-1)*B[1]-g*(B[2]+B[3]+B[4])-(1+g)*B[5]+((7*g-2)*F[1]&lt;STRONG&gt;&lt;FONT size="4" color="#3366FF"&gt;+(5*g-2)*F[2]&lt;/FONT&gt;&lt;/STRONG&gt;+(3*g-2)*F[3]+(g-2)*F[4])&lt;FONT size="4" color="#3366FF"&gt;&lt;STRONG&gt;/2&lt;/STRONG&gt;&lt;/FONT&gt;-F[5];&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Second, in the &lt;U&gt;check&lt;/U&gt; procedure, s2 is defined as:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  s2 = s2 + (B[t]+F[t]/2)/u**(t+1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, the equation has terms of &lt;U&gt;(B[t]+F[t+1]/2)/u**(t+2)&lt;/U&gt; rather than (B[t]+F[t]/2)/u**(t+1). Such as,&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MAC1430_0-1632048061847.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63701i7F90F572E83B5A31/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MAC1430_0-1632048061847.png" alt="MAC1430_0-1632048061847.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that array &lt;FONT face="courier new,courier"&gt;B&lt;/FONT&gt;&amp;nbsp;(in the DATA step) is defined as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array B[*] B0-B4;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;after renaming variable &lt;FONT face="courier new,courier"&gt;B&lt;/FONT&gt; from your dataset TEST to &lt;FONT face="courier new,courier"&gt;B0&lt;/FONT&gt;&amp;nbsp;in order to avoid a name conflict with the array name. Therefore, &lt;FONT face="courier new,courier"&gt;B[1]=B&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;B[2]=B&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;, ..., &lt;FONT face="courier new,courier"&gt;B[5]=B&lt;STRONG&gt;4&lt;/STRONG&gt;&lt;/FONT&gt;, whereas the indices of array &lt;FONT face="courier new,courier"&gt;F&lt;/FONT&gt; are the same as in your formula (&lt;FONT face="courier new,courier"&gt;F[1]=F&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt; and so on). I know that this is a bit confusing and maybe I should have named array &lt;FONT face="courier new,courier"&gt;B&lt;/FONT&gt; differently. Unlike arrays in a DATA step, arrays in PROC FCMP are always 1-based (see the remark about "lower-bound specifications" in the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p048tu5gixqaxin1ej7b7yuyhn86.htm#n18rmdt275t45rn13qn0jdskhq7i" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt;), i.e., a definition like &lt;FONT face="courier new,courier"&gt;array B[0:4]&lt;/FONT&gt;&amp;nbsp;(to harmonize array indices and original indices) is not allowed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;So, I made an attempt to modify by introducing s3 ...&lt;/P&gt;
&lt;P&gt;(...)&lt;/P&gt;
&lt;P&gt;Is my understanding correct? The diff1 and diff2 values become much bigger after this modification.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, it isn't, as explained above, and the substantially increased &lt;FONT face="courier new,courier"&gt;diff1&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;diff2&lt;/FONT&gt; values demonstrate this. But that's what the check is for: detecting errors.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Sep 2021 13:53:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768460#M243755</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-19T13:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768532#M243774</link>
      <description>&lt;P&gt;Thank you very much for the explanation Reinhard.&lt;/P&gt;
&lt;P&gt;I applied the procedure on my real dataset, got a mix of positive and negative results in both r1 and r2, some missing values as well. I guess it has something to do with the selection of initial value? Any suggestions how to select an initial value that returns the more robust results? (The unknown r I’m looking for is a rate of return, which should be positive numbers and not exceed 1 or 100%.)&lt;/P&gt;
&lt;P&gt;Thank you again for your help, it's really appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Sep 2021 12:00:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768532#M243774</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-09-20T12:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768553#M243784</link>
      <description>&lt;P&gt;Good to know that only solutions in the interval (0, 1) are of interest. Normally, the algorithm used by the SOLVE function should converge to a particular solution if the initial value is close enough to that solution. Hence, you could use initial values like, for example, 0.01, 0.02, ..., 0.99 in a loop until a solution is found. Or enforce a positive solution by using a substitution similar to what I did in the &lt;A href="https://communities.sas.com/t5/SAS-Programming/Solving-Equation/m-p/766245/highlight/true#M242829" target="_blank" rel="noopener"&gt;solution of the earlier thread&lt;/A&gt;. Another option would be to switch from SOLVE to the bisection method mentioned in that other post.&amp;nbsp;Can you share an example where you get a negative solution in &lt;FONT face="courier new,courier"&gt;r2&lt;/FONT&gt;, i.e., with the initial value 1? Then I can have a look which of these options is most suitable.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 20 Sep 2021 13:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768553#M243784</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-20T13:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768703#M243848</link>
      <description>&lt;P&gt;Thank you very much Reinhard, I selected some sotcks in the attached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Oct 2021 02:23:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768703#M243848</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-10-05T02:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768915#M243911</link>
      <description>&lt;P&gt;Thanks a lot for the sample data. I checked several cases "manually" and found different scenarios:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;no real (as opposed to complex) solution at all (e.g.,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;stock='BE0003826436'&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;year=2012&lt;/FONT&gt;, where the polynomial function is everywhere positive while the original function skips the x-axis at two poles!)&lt;/LI&gt;
&lt;LI&gt;only two negative solutions (not always found by SOLVE with initial values −1 or 1)&lt;/LI&gt;
&lt;LI&gt;one positive solution &amp;gt;1 and one negative solution that SOLVE found with &lt;EM&gt;both&lt;/EM&gt; initial values&lt;/LI&gt;
&lt;LI&gt;one negative solution and one in (0, 1) that SOLVE found with &lt;EM&gt;both&lt;/EM&gt; initial values&lt;/LI&gt;
&lt;LI&gt;one negative solution and one in (0, 1), both found by SOLVE&lt;/LI&gt;
&lt;LI&gt;two positive solutions: one in (0, 1) and one &amp;gt;1, but sometimes only the former was found by SOLVE (with both initial values, −1 and 1)&lt;/LI&gt;
&lt;LI&gt;two solutions in (0, 1), both found by SOLVE&lt;/LI&gt;
&lt;LI&gt;missing solutions, of course, where one or more parameters used in the formula are missing ("NA") -- this applies to 67 of the 483 observations in the sample data.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I've implemented a search for initial values checking the sign of the polynomial function &lt;EM&gt;k&lt;/EM&gt; at &lt;EM&gt;r =&amp;nbsp;&lt;/EM&gt;0, 0.001, 0.002, ..., 0.999, 1. The midpoint of an interval with a sign change between its boundaries is selected as an initial value (taking into account even the unlikely case that an interval boundary happens to be a root of &lt;EM&gt;k&lt;/EM&gt;). It turned out that, at least for the 483−67=416 usable sample data observations, the initial values found by this algorithm are close enough to the roots of &lt;EM&gt;k&lt;/EM&gt; in (0, 1), if any, so that the SOLVE function finds the roots accurately. The same roots were found with different subdivisions of [0, 1], e.g., into 10000 subintervals (instead of 1000) or only 3 (!). The interval boundaries enclosing a sign change could also be used as starting values for the bisection method. This would be an alternative approach which could replace the SOLVE function &lt;EM&gt;if&lt;/EM&gt; it didn't work in "extreme" cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I've checked all 171 solutions found for the 416 complete observations (163 with a single solution in (0, 1), 4 with two distinct solutions in (0, 1), 249 with no solutions in (0, 1)) with the code used in the earlier post (i.e., using the rational function &lt;FONT face="courier new,courier"&gt;h&lt;/FONT&gt;&amp;nbsp;rather than the polynomial), except that no renaming of variables was necessary. These checks resulted in a maximum absolute value of &lt;FONT face="courier new,courier"&gt;h(r)&lt;/FONT&gt; of &lt;FONT face="courier new,courier"&gt;6.08E-6&lt;/FONT&gt;. I took a closer look at the case where this relatively "large" maximum occurred (&lt;FONT face="courier new,courier"&gt;stock='CH0008742519' &amp;amp; year=1998&lt;/FONT&gt;) and found that&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The root determined by SOLVE differs from the "best possible" approximation (of the exact solution) by only &lt;FONT face="courier new,courier"&gt;&amp;lt;5E-10&lt;/FONT&gt;, which is well within expectations.&lt;/LI&gt;
&lt;LI&gt;The value of &lt;FONT face="courier new,courier"&gt;h(r)&lt;/FONT&gt; could be improved to &lt;FONT face="courier new,courier"&gt;&amp;lt;8E-13&lt;/FONT&gt; by changing &lt;FONT face="courier new,courier"&gt;r&lt;/FONT&gt; by the above tiny amount.&lt;/LI&gt;
&lt;LI&gt;The first derivative of &lt;FONT face="courier new,courier"&gt;h&lt;/FONT&gt; at the solution &lt;FONT face="courier new,courier"&gt;r=0.8968...&lt;/FONT&gt; (which is the only solution in (0, 1) here) is &amp;gt;14,000 (!). This explains the observed "sensitivity" to minor changes of &lt;FONT face="courier new,courier"&gt;r&lt;/FONT&gt;.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The solutions found with the above approach also confirmed those that I had investigated manually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Define function XRATE2 */

proc fcmp outlib=work.funcs.test;
function k(r, p, B[*], F[*], g);
  c6= p;
  c5= p*(5-g)-F[1]/2;
  c4= p*(10-5*g)-B[1]+B[2]+(g-5)*F[1]/2-F[2]/2;
  c3= 10*p*(1-g)+(g-3)*B[1]+(2-g)*B[2]+B[3]+((5*g-9)*F[1]+(g-4)*F[2]-F[3])/2;
  c2= p*(5-10*g)+3*(g-1)*B[1]+(1-2*g)*B[2]+(1-g)*B[3]+B[4]+((9*g-7)*F[1]+(4*g-5)*F[2]+(g-3)*F[3]-F[4])/2;
  c1= p*(1-5*g)+(3*g-1)*B[1]-g*(B[2]+B[3]+B[4])-(1+g)*B[5]+((7*g-2)*F[1]+(5*g-2)*F[2]+(3*g-2)*F[3]+(g-2)*F[4])/2-F[5];
  c0= -p*g+g*B[1]+g*(F[1]+F[2]+F[3]+F[4])-F[5];
  z = c6*r**6 + c5*r**5 + c4*r**4 + c3*r**3 + c2*r**2 + c1*r + c0;
  return(z);
endfunc;

function xrate2(i, p, B[*], F[*], g); /* first argument is the initial value */
  array solvopts[1] i;
  r=solve("k", solvopts, 0, ., p, B, F, g);
  if min(abs(1+r),abs(r-g))&amp;lt;1e-4 then r=.; /* avoid poles of the original function */
  return(r);
endfunc;
run;

/* Make function available */

options cmplib=work.funcs;

/* Find initial values for solutions in (0, 1) */

%let nsubintv=1000; /* number of subintervals of [0, 1] to search */

data init(drop=_:);
set have;
if nmiss(P, g, of F1-F5, of B0-B4) then output; /* Solutions will be missing for incomplete data. */
else do;
  array init[6]; /* Up to six roots are possible theoretically. */
  array B[*] B0-B4;
  array F[*] F1-F5;
  do _i=0 to &amp;amp;nsubintv;
    _x=_i/&amp;amp;nsubintv;
    _z=k(_x, p, B, F, g);
    _s=sign(_z);
    _prev_s=lag(_s);
    if _s~=0 then do;
      if _i &amp;amp; _s~=_prev_s~=0 then do;
        _j=sum(_j,1);
        init[_j]=(_i-0.5)/&amp;amp;nsubintv; /* select midpoint of subinterval with sign change between boundaries */
      end;
    end;
    else do; /* This case (solution on subinterval boundary) is very unlikely. */
      _j=sum(_j,1);
      init[_j]=_x;
    end;
  end;
  output;
end;
run;

/* Apply function XRATE2 to dataset INIT */

data want(drop=_j);
set init;
array B[*] B0-B4;
array F[*] F1-F5;
array init[6];
array r[6];
do _j=1 to dim(r) while(init[_j]&amp;gt;.);
  r[_j]=xrate2(init[_j], p, B, F, g); /* solution found with initial value init[_j] */
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The PROC FCMP step and the OPTIONS statement are exactly the same as in my earlier post. I repeated them because your attachment &lt;FONT face="courier new,courier"&gt;sample.sas&lt;/FONT&gt; contained something different. For your sample data the dimension of the arrays &lt;FONT face="courier new,courier"&gt;init&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;r&lt;/FONT&gt; in the DATA steps could be reduced from 6 to 2 (remaining variables are always missing), but theoretically a polynomial of grade 6 can have up to 6 real roots, so it's better to allow for that many solutions and initial values. You may want to drop variables &lt;FONT face="courier new,courier"&gt;init1-init6&lt;/FONT&gt;. I've left them in dataset WANT just to check the differences (always within ±0.0005 for &lt;FONT face="courier new,courier"&gt;&amp;amp;nsubintv=1000&lt;/FONT&gt;) between them and the corresponding solutions.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 20:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/768915#M243911</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-21T20:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: FCMP - solving equation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/769033#M243961</link>
      <description>Thank you very much for everything, you are amazing.</description>
      <pubDate>Wed, 22 Sep 2021 08:17:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/FCMP-solving-equation/m-p/769033#M243961</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2021-09-22T08:17:49Z</dc:date>
    </item>
  </channel>
</rss>

