<?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 Iterative equation solving in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278392#M56005</link>
    <description>&lt;P&gt;Hello dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your help on following question, concerning the generation of a variable, which solves an equation, containing variables already existing in the data set, where the equation is not solved for the unknown but shall be solved iteratively.&lt;/P&gt;&lt;P&gt;In the following, I provide a simply example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data icctrial;&lt;BR /&gt;input p e1 e2 e3 r;
datalines;
48.159278738	10	20	30	0.1
80.681818182	20	40	60	0.2
456.46644799	30	45	601	0.15
420.4413418	454	5	5	0.11
4986.8628377	5445	12	45	0.17
0.7513148009	0	0	1	0.1
11.605639731	5	6	5	0.2
2.5476889974	1	2	1	0.3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The equation to be solved for each observation is&lt;/P&gt;&lt;P&gt;p=(e1)/(1+a)+e2/((1+a)**2)+e3/((1+a)**3),&lt;/P&gt;&lt;P&gt;with "p", "e1", "e2" and "e3" all given, "a" as the only unknown.&lt;/P&gt;&lt;P&gt;The solution shall be added to the data set as the variable "a" for each observation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data is chosen, such that "r" is the correct solution. "r" is added as a reference (sadly, only).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be glad, if someone would help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours sincerely,&lt;/P&gt;&lt;P&gt;Sinistrum&lt;/P&gt;</description>
    <pubDate>Sat, 18 Jun 2016 15:38:43 GMT</pubDate>
    <dc:creator>Sinistrum</dc:creator>
    <dc:date>2016-06-18T15:38:43Z</dc:date>
    <item>
      <title>Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278392#M56005</link>
      <description>&lt;P&gt;Hello dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your help on following question, concerning the generation of a variable, which solves an equation, containing variables already existing in the data set, where the equation is not solved for the unknown but shall be solved iteratively.&lt;/P&gt;&lt;P&gt;In the following, I provide a simply example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data icctrial;&lt;BR /&gt;input p e1 e2 e3 r;
datalines;
48.159278738	10	20	30	0.1
80.681818182	20	40	60	0.2
456.46644799	30	45	601	0.15
420.4413418	454	5	5	0.11
4986.8628377	5445	12	45	0.17
0.7513148009	0	0	1	0.1
11.605639731	5	6	5	0.2
2.5476889974	1	2	1	0.3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The equation to be solved for each observation is&lt;/P&gt;&lt;P&gt;p=(e1)/(1+a)+e2/((1+a)**2)+e3/((1+a)**3),&lt;/P&gt;&lt;P&gt;with "p", "e1", "e2" and "e3" all given, "a" as the only unknown.&lt;/P&gt;&lt;P&gt;The solution shall be added to the data set as the variable "a" for each observation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data is chosen, such that "r" is the correct solution. "r" is added as a reference (sadly, only).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be glad, if someone would help me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours sincerely,&lt;/P&gt;&lt;P&gt;Sinistrum&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jun 2016 15:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278392#M56005</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2016-06-18T15:38:43Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278404#M56009</link>
      <description>&lt;P&gt;Have you looked at PROC MODEL? Do you have SAS ETS licensed?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/viewer.htm#etsug_model_sect003.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/viewer.htm#etsug_model_sect003.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jun 2016 18:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278404#M56009</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-18T18:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278407#M56011</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and thank you for your reply.&lt;/P&gt;&lt;P&gt;Indeed, during my desperate search, I came across "PROC MODEL".&lt;/P&gt;&lt;P&gt;As I have never worked with SAS before, I was afraid, this was "just" a tool in order to create the standard Least-Squares-Regression output . Furthermore, I do not know, how I could assign the output of such a procedure as a variable to my existing data set.&lt;/P&gt;&lt;P&gt;So, maybe I should dig deeper into that topic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was hoping for something profane in the manner like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data icctrial;
	set icctrial;
	a = 'such that this equation here is fulfilled';
run;&lt;/PRE&gt;&lt;P&gt;and that someone would have an easy solution,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But thanks to you, I now know where to search.&lt;/P&gt;&lt;P&gt;I hope I will come to terms with this issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Indeed, "I" have SAS/ETS licensed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT:&lt;/P&gt;&lt;P&gt;I tried this&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc model data=icctrial out=egal;
exogenous p e1 e2 e3;
endogenous a ;
solve a;
p=(e1)/(1+a)+e2/((1+a)**2)+e3/((1+a)**3);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but it gives pretty inaccurate results.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jun 2016 19:52:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278407#M56011</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2016-06-18T19:52:19Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278410#M56012</link>
      <description>&lt;P&gt;You don't need an iterative procedure to find the roots of third degree polynomials. But for a general function of one variable, you can hijack the line search of &lt;STRONG&gt;proc nlin&lt;/STRONG&gt; in this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input p e1 e2 e3;
datalines;
48.159278738	10	20	30	
80.681818182	20	40	60	
456.46644799	30	45	601	
420.4413418	    454	5	5
4986.8628377	5445	12	45
0.7513148009	0	0	1
11.605639731	5	6	5
2.5476889974	1	2	1
;

/* Add equation number */
data parms;
set test;
eqNo + 1;
run;

/* define starting value for parameter b = 1+a */
data startingValues;
set parms;
parameter = "b";
Estimate = 1.1;
run;

/* Find 3rd degree equation root for every obs */
proc nlin data=parms outest=roots(where=(_TYPE_="FINAL"));
by eqNo p e1 e2 e3;
parameters / pdata=startingValues;
model e3 = -(-p*b**3 + e1*b**2 + e2*b);
run;

title "Polynomial roots";
proc sql;
select eqNo, p, e1, e2, e3, b-1 as a format=6.4,
    e3 + (-p)*b**3 + e1*b**2 + e2*b as rootValue
from roots
order by eqNo;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: Some roots don't match your 'r' values.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jun 2016 20:14:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278410#M56012</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-18T20:14:58Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278429#M56018</link>
      <description>&lt;P&gt;You can also use the&lt;STRONG&gt; solve&lt;/STRONG&gt; function in FCMP:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input p e1 e2 e3;
datalines;
48.159278738	10	20	30	
80.681818182	20	40	60	
456.46644799	30	45	601	
420.4413418	    454	5	5
4986.8628377	5445	12	45
0.7513148009	0	0	1
11.605639731	5	6	5
2.5476889974	1	2	1
;


/* FCMP routine to find function roots */
proc fcmp outlib=sasuser.fcmp.test;
function fn(e1, e2, e3, a);
    return ( e1/(1+a) + e2/(1+a)**2 + e3/(1+a)**3 );
endsub;
function findZero(p, e1, e2, e3, init);
    array solvopts[1] initial (0);
    initial = init;
    return (solve("fn", solvopts, p, e1, e2, e3, .)); 
endsub;
run;

options cmplib=sasuser.fcmp;
         
data roots;
set test;
root = findZero(p, e1, e2, e3, 0.1);
run;

proc print data=roots; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which gives the same roots as proc nlin.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2016 01:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278429#M56018</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-19T01:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278434#M56021</link>
      <description>&lt;PRE&gt;
You are looking for the root of function : F(x)=p*x**3-e1*x**2-e2*x-e3  
It would be easy for SAS/IML . You can run it under SAS University Edition.



data icctrial;
infile cards expandtabs truncover;
input p e1 e2 e3 r;
datalines;
48.159278738	10	20	30	0.1
80.681818182	20	40	60	0.2
456.46644799	30	45	601	0.15
420.4413418	454	5	5	0.11
4986.8628377	5445	12	45	0.17
0.7513148009	0	0	1	0.1
11.605639731	5	6	5	0.2
2.5476889974	1	2	1	0.3
;
run;

proc iml;
use icctrial;
read all var _num_ into x[c=vnames];
close;

root=j(nrow(x),1,.);
do i=1 to nrow(x);
 parm=x[i,1]||-x[i,2]||-x[i,3]||-x[i,4]; 
 roots=polyroot(parm);
 root[i]=roots[loc(roots[,2]=0),1]-1;
end;
want=x||root;
print want[c=(vnames||'root') l=''];
quit;






OUTPUT:

p	e1	e2	e3	r	root
48.159279	10	20	30	0.1	0.1
80.681818	20	40	60	0.2	0.1898444
456.46645	30	45	601	0.15	0.1489363
420.44134	454	5	5	0.11	0.1004448
4986.8628	5445	12	45	0.17	0.1014909
0.7513148	0	0	1	0.1	0.1
11.60564	5	6	5	0.2	0.1791374
2.547689	1	2	1	0.3	0.2614769


&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Jun 2016 03:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278434#M56021</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-19T03:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278454#M56022</link>
      <description>&lt;P&gt;Thank you for your answers. This is really awesome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, "&lt;STRONG&gt;solve&lt;/STRONG&gt; function in FCMP" and "hijack[ing] the line search of &lt;STRONG&gt;proc nlin&lt;/STRONG&gt;" as you mentioned, PG, are presumably exactly what I am looking for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data set I made up was just a simple example. In fact, I do have to solve a function of the form&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/3676i41B3ACB36A744A65/image-size/original?v=v2&amp;amp;px=-1" alt="Unbenannt.png" title="Unbenannt.png" border="0" /&gt;,&lt;/P&gt;&lt;P&gt;where the variable "b" is a function of "a" (and a constant, which is given, no problem with that), too.&lt;/P&gt;&lt;P&gt;So the itterative procedure is indeed what I am looking for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you, Ksharp, too. I am convinced I can make use of this later, too, for other approaches for different equations as the equation mentioned is not the only to be dealt with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, today I am going to play a little with your solution suggestions.&lt;/P&gt;&lt;P&gt;I just got the message by mail that my problem had been quasi-solved and just wanted to express real quickly my gratitude for and appreciation of your answers. Again, thank you very much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What still makes me worry, though, is this one observation "5", where the true solution is 17% and not 10,15%. But I guess it is just due to the fact I mindlessly made up the data, the real sample I am dealing with does not contain such extreme values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Sinistrum&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2016 06:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278454#M56022</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2016-06-19T06:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278522#M56037</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84594"&gt;@Sinistrum&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What still makes me worry, though, is this one observation "5", where the true solution is 17% and not 10,15%. But I guess it is just due to the fact I mindlessly made up the data, the real sample I am dealing with does not contain such extreme values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Simple check for obs 5:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;p - f(e1, e2, e3; 0.17) = 296.15, &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;p - f(e1, e2, e3; 0.1015) =&lt;/SPAN&gt;&amp;nbsp;0.0002&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jun 2016 20:47:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278522#M56037</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-19T20:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative equation solving</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278616#M56063</link>
      <description>&lt;P&gt;So, obviously, I am even to clusmy to set up test-data-sets properly for I have made a mistake with discounting e1.&lt;/P&gt;&lt;P&gt;The correct data is&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input p e1 e2 e3 r;
datalines;
48.159278738	10	20	30	0.1
79.166666667	20	40	60	0.2
455.28067724	30	45	601	0.15
416.72307808	454	5	5	0.11
4690.7089915	5445	12	45	0.17
0.7513148009	0	0	1	0.1
11.226851852	5	6	5	0.2
2.4078288575	1	2	1	0.3&lt;BR /&gt;;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;With this now corrected data, the estimated "root" matches the "r".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So sorry for this and, once again, thank you very much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Sinistrum&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 09:32:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterative-equation-solving/m-p/278616#M56063</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2016-06-20T09:32:01Z</dc:date>
    </item>
  </channel>
</rss>

