Thank you very much Reinhard, below is my current try (could be silly) by modifying your code.
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 < -1 */
when(2) z=-1+exp(-x*1e4); /* for a solution > -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 < -1 */
when(2) r=-1+t; /* solution > -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;
... View more