<?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: find roots of a quadratic equation for each obs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955151#M373034</link>
    <description>&lt;P&gt;The quadratic equation from Wikipedia:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_3-1736092154963.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103483i48B2C0E939857037/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_3-1736092154963.png" alt="Tom_3-1736092154963.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So first convert your equation to the standard form.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_4-1736092209193.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103484i2D555755B3CB6F66/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_4-1736092209193.png" alt="Tom_4-1736092209193.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Then just calculate the various parts. -b , 2a and sqrt(b**2-4ac).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure to check if 4ac &amp;gt; b**2 because those cases have imaginary roots.&lt;/P&gt;
&lt;P&gt;If it has real roots then build that by combining the parts twice, once using addition and once using subtraction.&lt;/P&gt;</description>
    <pubDate>Sun, 05 Jan 2025 15:53:14 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-01-05T15:53:14Z</dc:date>
    <item>
      <title>find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955148#M373033</link>
      <description>&lt;P&gt;I have a data set with y and x.&amp;nbsp; Is there a way to find the 2 roots of a quadratic equation for each obs?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The equation is y - 2*x*R&amp;nbsp; + 3*x*R**2 =0.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample data is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input y x;
datalines;
1 2
2 4
5 6
8 9
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jan 2025 15:40:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955148#M373033</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2025-01-05T15:40:43Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955151#M373034</link>
      <description>&lt;P&gt;The quadratic equation from Wikipedia:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_3-1736092154963.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103483i48B2C0E939857037/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_3-1736092154963.png" alt="Tom_3-1736092154963.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;So first convert your equation to the standard form.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_4-1736092209193.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103484i2D555755B3CB6F66/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_4-1736092209193.png" alt="Tom_4-1736092209193.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Then just calculate the various parts. -b , 2a and sqrt(b**2-4ac).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make sure to check if 4ac &amp;gt; b**2 because those cases have imaginary roots.&lt;/P&gt;
&lt;P&gt;If it has real roots then build that by combining the parts twice, once using addition and once using subtraction.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jan 2025 15:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955151#M373034</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-05T15:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955153#M373036</link>
      <description>&lt;P&gt;Thanks for your response.&amp;nbsp; I was looking for an easier way to find the roots without having to write the equations in data step.&amp;nbsp; For another project I will be having cubic polynomials.&amp;nbsp; In that case how do I save the 3 roots in 3 variables?&amp;nbsp; I saw some examples online who are using&amp;nbsp; PROC MODEL or PROC IML.&amp;nbsp; But they use some sort of starting numbers to find the roots.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jan 2025 17:02:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955153#M373036</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2025-01-05T17:02:11Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955154#M373037</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I was looking for an easier way to find the roots without having to write the equations in data step.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint: there are a gazillion different articles out on the internet regarding how to perform specific mathematical and statistical calculations, many of these articles written by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; . Searching for these are always a good place to start.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See Rick's 2011 article: &lt;A href="https://blogs.sas.com/content/iml/2011/08/03/finding-the-root-of-a-univariate-function.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2011/08/03/finding-the-root-of-a-univariate-function.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hint: in your second message, you said "I was looking for an easier way to find the roots without having to write the equations in data step. For another project I will be having cubic polynomials." Please tell us all of your needs in your first message from now on.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jan 2025 17:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955154#M373037</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-05T17:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955162#M373043</link>
      <description>&lt;UL class="lia-list-style-type-square"&gt;
&lt;LI&gt;Find the root of a function by using the &lt;STRONG&gt;SAS DATA step&lt;/STRONG&gt;&lt;BR /&gt;By Rick Wicklin on The DO Loop August 19, 2020&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2020/08/19/find-root-function-sas-data-step.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2020/08/19/find-root-function-sas-data-step.html&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;A simple way to find the root of a function of one variable &lt;BR /&gt;By Rick Wicklin on The DO Loop February 5, 2014&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2014/02/05/find-the-root-of-a-function.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2014/02/05/find-the-root-of-a-function.html&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Finding the root of a univariate function&lt;BR /&gt;By Rick Wicklin on The DO Loop August 3, 2011&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2011/08/03/finding-the-root-of-a-univariate-function.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2011/08/03/finding-the-root-of-a-univariate-function.html&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;If you use SAS Viya and SAS Studio 5.2 or later AND your site licenses &lt;STRONG&gt;SAS/IML&lt;/STRONG&gt; , then there's a &lt;STRONG&gt;code snippet&lt;/STRONG&gt; called:&lt;BR /&gt;&lt;STRONG&gt;Find Roots of Nonlinear Equation&lt;/STRONG&gt; : Enables you to find the roots of a function of one variable. Finding the root (or zero) of a function enables you to solve nonlinear equations.&lt;/LI&gt;
&lt;LI&gt;Paper 3099-2019 Find a Function Root By &lt;STRONG&gt;Genetic Algorithm&lt;/STRONG&gt; &lt;BR /&gt;Xia Ke Shan, iFRE Inc., Beijing, China &lt;BR /&gt;Kurt Bremser, Allianz Technology GmbH, Austria (Presenter) &lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings19/3099-2019.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings19/3099-2019.pdf&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;root finding, &lt;STRONG&gt;proc optmodel&lt;/STRONG&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/root-finding-proc-optmodel/m-p/298259" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/root-finding-proc-optmodel/m-p/298259&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Halley's method for finding roots&amp;nbsp;&lt;BR /&gt;By Rick Wicklin on The DO Loop August 24, 2016&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2016/08/24/halleys-method-finding-roots.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2016/08/24/halleys-method-finding-roots.html&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Ciao, Koen&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jan 2025 21:10:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955162#M373043</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2025-01-05T21:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955163#M373044</link>
      <description>&lt;P&gt;Thanks for the link. I looked up and got some code.&amp;nbsp; However I am getting only one root.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;**Example 3: Solve for a: P=2*e1 + a + a*a;
data test;
input p e1;
datalines;
12 1
24 2
36 3
;
run;

/* FCMP routine to find function roots */
proc fcmp outlib=work.fcmp.test;
function fn(e1, a);
    return (2*e1 + a + a*a );
endsub;
function findZero(p, e1,init);
    array solvopts[1] initial (0);
    initial = init;
    return (solve("fn", solvopts, p, e1, .)); 
endsub;
run;

options cmplib=work.fcmp.test;
         
data roots;
set test;
format root 5.2;
root = findZero(p, e1, 0.1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jan 2025 22:45:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955163#M373044</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2025-01-05T22:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955164#M373045</link>
      <description>&lt;P&gt;Sorry for still another link ... but I would do it this way :&lt;/P&gt;
&lt;UL&gt;
&lt;LI class="lia-breadcrumb-node crumb"&gt;&lt;A id="link_4" class="lia-link-navigation crumb-community lia-breadcrumb-community lia-breadcrumb-forum" href="https://communities.sas.com/" target="_blank" rel="noopener"&gt;Home&lt;/A&gt;&amp;nbsp;&amp;gt;&amp;nbsp;&lt;A id="link_5" class="lia-link-navigation crumb-category lia-breadcrumb-category lia-breadcrumb-forum" style="font-family: inherit; background-color: #ffffff;" href="https://communities.sas.com/t5/Analytics-and-Statistics/ct-p/analytics" target="_blank" rel="noopener"&gt;Analytics&lt;/A&gt;&amp;nbsp;&amp;gt;&amp;nbsp;&lt;A id="link_6" class="lia-link-navigation crumb-board lia-breadcrumb-board lia-breadcrumb-forum" style="font-family: inherit; background-color: #ffffff;" href="https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/bd-p/sas_iml" target="_blank" rel="noopener"&gt;SAS/IML&lt;/A&gt;&amp;gt;&amp;nbsp;&lt;STRONG&gt;&lt;SPAN class="lia-link-navigation child-thread lia-link-disabled" aria-disabled="true" aria-label="find roots of function"&gt;find roots of function&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/find-roots-of-function/td-p/794659" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/find-roots-of-function/td-p/794659&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A&lt;SPAN&gt;&amp;nbsp;quadratic function (equation) may have&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;one, two, or zero roots&lt;/STRONG&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL class="lia-list-style-type-square"&gt;
&lt;LI&gt;If the discriminant is less than 0, then the quadratic has no real roots. (but&amp;nbsp;&lt;SPAN&gt;the quadratic equation still has two complex-valued roots)&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;If the discriminant is equal to zero then the quadratic has equal roots. (two roots with the same value, called a double root)&lt;/LI&gt;
&lt;LI&gt;If the discriminant is more than zero then it has 2 distinct roots.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;Ciao,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Koen&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jan 2025 23:56:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955164#M373045</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2025-01-05T23:56:35Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955165#M373046</link>
      <description>&lt;P&gt;The link I gave does not have a solution that uses PROC FCMP. So, I don't know what code you are using or what link you got it from.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2025 00:29:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955165#M373046</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-06T00:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955180#M373051</link>
      <description>&lt;P&gt;Yes. Check&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;&amp;nbsp;blogs. Risk has written many blog about this topic.&lt;/P&gt;
&lt;P&gt;For your this special topic, you could use PROC NLIN ,SAS/IML .SAS/OR to get root of function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2022/05/04/bootstrap-nonlinear-regression.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2022/05/04/bootstrap-nonlinear-regression.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2015/06/08/fit-circle.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2015/06/08/fit-circle.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/08/15/optimization-nonlinear-constraints.html" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/iml/2018/08/15/optimization-nonlinear-constraints.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is some example:&lt;/P&gt;
&lt;P&gt;First is from Rick&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input y x;
datalines;
12 1
24 2
36 3
;
run;

 
proc nlin data=have plots(stats=none)=(fitplot); 
   parameters R=1;                
   model y = 2*x + R + R**2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1736131930234.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103488i7671DFEEEE9C6853/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1736131930234.png" alt="Ksharp_0-1736131930234.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second is from Rick&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc iml;
start ObjF(R)  global(x, y);    /* x and y are fixed vectors of data */
   F = ssq(2*x + R + R**2 - y);            /* minimize F = SSQ differences */
   return(F);
finish;
use have;  read all var {x y};  close ;  /* read observed data */
R = {10};                           /* initial guess for (x0, y0) */
opt = {0 1};                           /* options=(min, print level) */
call nlptr(rc,result,"ObjF", r, opt);  /* find optimum (x0, y0) */
print result;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_1-1736132003054.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103489i548B4D00D2A45C3F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_1-1736132003054.png" alt="Ksharp_1-1736132003054.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third is from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/1636"&gt;@RobPratt&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc optmodel;
   set OBS;
   num x {OBS};
   num y {OBS};
   read data have into OBS=[_N_] x y;
   var R &amp;gt;= 0;
   min F = sum {i in OBS} (2*x[i]+R+R^2  - y[i])^2;
   solve;
   print R;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_2-1736132067248.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103490iC5545180185D0BC0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_2-1736132067248.png" alt="Ksharp_2-1736132067248.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2025 03:01:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955180#M373051</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-06T03:01:46Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955182#M373052</link>
      <description>&lt;P&gt;If R was a vector:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input y x;
datalines;
12 1
24 2
36 3
;
run;



proc optmodel;
   set OBS;
   num x {OBS};
   num y {OBS};
   read data have into OBS=[_N_] x y;
   var R{OBS} &amp;gt;= 0;
   min F = sum {i in OBS} (2*x[i]+R[i]+R[i]^2  - y[i])^2;
   solve ;

   print F R;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1736133079047.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103491iE6DE20E01CFF02A0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1736133079047.png" alt="Ksharp_0-1736133079047.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2025 03:11:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955182#M373052</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-06T03:11:29Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955191#M373054</link>
      <description>Your object funtion&lt;BR /&gt;P=2*e1 + a + a*a;&lt;BR /&gt;is monotonic , so you can't expect to get two roots.</description>
      <pubDate>Mon, 06 Jan 2025 06:56:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955191#M373054</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-06T06:56:16Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955201#M373059</link>
      <description>&lt;P&gt;Initially you said you wanted to solve&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;(3*x)*R**2 - (2*x)*R + y =0, which has polynomial coefficients {3*x, -2*x, y}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But later you use&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1*R**2 + 1*R + 2*e1 = 0&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Which is it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In any case, to get the roots of a low-degree polynomial, &lt;A href="https://blogs.sas.com/content/iml/2011/08/03/finding-the-root-of-a-univariate-function.html" target="_self"&gt;use the POLYROOT function in SAS IML&lt;/A&gt;. Create a SAS data set that has the polynomial coefficients ordered from highest degree to lowest (the constant term).&amp;nbsp; Read the coefficients into IML and loop over the coefficients.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A polynomial of degree d always has d complex roots. If you are interested only in the real roots, then find the roots for which the imaginary part is 0 (or very tiny).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code assumes that you want to solve the equation in your original post. It then creates a SAS data set that has ONLY the polynomial coefficients. The IML program extracts the REAL roots.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input y x;
datalines;
-1 2
-2 3
-5 6
-8 9
;

/* root of
   (3*x)*R**2 - 2*x*R  + y = 0 
   Let a = 3*x, b= -2*x, and c=y and use the quadratic equation
*/
data Coef;
keep c2 c1 c0;
set have;
c2 = 3*x;
c1 = -2*x;
c0 = y;
run;

proc iml;
delta = 1E-14;   /* small number to use as a custoff value */
use Coef;
read all var _num_ into coeff;  /* coefficients of R^2, R, constant */
close;

numPolys = nrow(coeff);
deg = ncol(coeff)-1;
roots = j(numPolys, deg, .);  /* poly of degree d has d complex roots */
do i = 1 to numPolys;
   r = polyroot( coeff[i,] );   /* returns all complex roots */
   print i r;
   /* store only the real roots for which r[,2]=0 */
   realIdx = loc( abs(r[,2]) &amp;lt; delta );
   numRealRts = ncol(realIdx);
   if numRealRts &amp;gt; 0 then 
      roots[i, 1:numRealRts] = rowvec( r[realIdx, 1] );  /* get the real parts */
end;
PolyNumber = T(1:numPolys);
RootNumber = 1:deg;
print PolyNumber roots[c=RootNumber];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use the same IML program to find roots of higher-degree polynomials. For example, if you want to find cubic roots, use the following DATA step to create the coefficients and use the same IML code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Coef;
input c3 c2 c1 c0;
datalines;
1 -1 2 1
1 -1 -4 4
1 1 -3 -3
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2025 11:52:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955201#M373059</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-01-06T11:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: find roots of a quadratic equation for each obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955229#M373071</link>
      <description>&lt;P&gt;Thank you so much Rick!!!&amp;nbsp; This works perfectly.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2025 17:46:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-roots-of-a-quadratic-equation-for-each-obs/m-p/955229#M373071</guid>
      <dc:creator>PamG</dc:creator>
      <dc:date>2025-01-06T17:46:52Z</dc:date>
    </item>
  </channel>
</rss>

