Fluorite | Level 6

## PROC iml froot to solve univariate equation

Hi,

I am new to proc iml. I read that froot function could replicate excel sovler. I try to solve an equation towards the variable x, which is supposed to a number between -1 and 1,  but I got the error: invalid Operation. I am guessing I put in the wrong bound. Could someone please help me with this Problem?

Many thanks!

Best

data have;

input D0 D1 D2 D3;

ZERO = 0;

ID = _N_;

datalines;

0.1 0.45 100 80

0.05 0.5 200 150

0.3 0.45 300 180

0.5 0.6 400 200

0.15 0.6 500 200

;

run;

proc iml;

start Func (x) global (D0, D1, D2, D3);

return ((CDF('NORMAL',(Quantile('NORMAL',D1) + sqrt(x)*Quantile('NORMAL', .999))/sqrt(1-x), 0, 1)-D0)*D1*D2 - D3);

finish;

use have;

read all var {D0 D1 D2 D3} into D;

close;

x=j(nrow(D),1);

do i=1 to nrow(D);

D0= D[i,1]; D1= D[i, 2]; D2 = D[i, 3]; D4 =D[i,4];

x[i]=froot("Func", {-1,1});

end;

print x;

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Employee

## Re: PROC iml froot to solve univariate equation

``````data have;
input D0 D1 D2 D3;
datalines;
0.1 0.45 100 80
0.05 0.5 200 150
0.3 0.45 300 180
0.5 0.6 400 200
0.15 0.6 500 200
;;
run;

proc iml;
start Func (x) global (D0, D1, D2, D3);
return ((CDF('NORMAL',(Quantile('NORMAL',D1) + sqrt(x)*Quantile('NORMAL', .999))/sqrt(1-x), 0, 1)-D0)*D1*D2 - D3);
finish;

use have;
read all var {D0 D1 D2 D3} into D;
close;

x=j(nrow(D),1);
do i=1 to nrow(D);
D0 = D[i, 1];
D1 = D[i, 2];
D2 = D[i, 3];
D3 = D[i, 4];
x[i] = froot("Func", {0 0.999});
end;
print x;
quit;``````

This script does not return any error messages. You had introduced D4 in your loop; I changed it to D3. Your input x boundaries should be well-defined (greater than zero and less than 1, as you had sqrt(x) in your function and division by sqrt(1-x)). Apparently, you do not have any feasible solution in the first 4 iterations.

3 REPLIES 3
SAS Employee

## Re: PROC iml froot to solve univariate equation

you clearly have an unbalanced parentheses in your return statement in Func(x). Try fixing it first.

Fluorite | Level 6

## Re: PROC iml froot to solve univariate equation

Thanks Imvash for your hint. I corrected the place. But still, it does not function.

SAS Employee

## Re: PROC iml froot to solve univariate equation

``````data have;
input D0 D1 D2 D3;
datalines;
0.1 0.45 100 80
0.05 0.5 200 150
0.3 0.45 300 180
0.5 0.6 400 200
0.15 0.6 500 200
;;
run;

proc iml;
start Func (x) global (D0, D1, D2, D3);
return ((CDF('NORMAL',(Quantile('NORMAL',D1) + sqrt(x)*Quantile('NORMAL', .999))/sqrt(1-x), 0, 1)-D0)*D1*D2 - D3);
finish;

use have;
read all var {D0 D1 D2 D3} into D;
close;

x=j(nrow(D),1);
do i=1 to nrow(D);
D0 = D[i, 1];
D1 = D[i, 2];
D2 = D[i, 3];
D3 = D[i, 4];
x[i] = froot("Func", {0 0.999});
end;
print x;
quit;``````

This script does not return any error messages. You had introduced D4 in your loop; I changed it to D3. Your input x boundaries should be well-defined (greater than zero and less than 1, as you had sqrt(x) in your function and division by sqrt(1-x)). Apparently, you do not have any feasible solution in the first 4 iterations.

From The DO Loop