BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Dingdang
Fluorite | Level 6

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
imvash
SAS Employee
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.

View solution in original post

3 REPLIES 3
imvash
SAS Employee

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

Dingdang
Fluorite | Level 6

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

imvash
SAS Employee
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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 847 views
  • 1 like
  • 2 in conversation