DATA Step, Macro, Functions and more

How to use marco to change variable?

Reply
New Contributor
Posts: 4

How to use marco to change variable?

This is my SAS macro to calculate american put or call by Cox-Ross-Rubinstein method:

Macro to Calculate the call Intrinsic Option Value at Each NodeThis

%macro cval(si,xi);
(&si-ξ+abs(&si-ξ))/2
%mend cval;
* Macro to Calculate the callOption Price at Each Node;
%macro c(ni,ss);
%if ∋=&n %then
%do;
%cval (&ss,&x)
%end;
%else %do;
max(%cval(&ss,&x),((%c(∋+1,&ss*&u))*&p+(%c(∋+1,&ss*&d))*(1-&p))/&rq)
%end;
%mend c;
* Macro to Calculate the put Intrinsic Option Value at Each Node;
%macro pval(si,xi);
(&xi-&si+abs(&xi-&si))/2
%mend pval;

* Macro to Calculate the put Option Price at Each Node;
%macro p(ni,ss);
%if ∋=&n %then
%do;
%pval (&ss,&x)
%end;
%else %do;
max(%pval(&ss,&x),((%p(∋+1,&ss*&u))*&p+(%p(∋+1,&ss*&d))*(1-&p))/&rq)
%end;
%mend p;

data data1;
input eq_price strike time opt_price intrate;
datalines;
1108.48 995 0.041067762 0.8 0.01107094
1122.22 995 0.032854209 0.35 0.011018561
1123.67 995 0.030116359 0.3 0.01098864
1126.33 995 0.027378508 0.2 0.01092533
1131.92 995 0.024640657 0.2 0.010853256
1121.86 995 0.021902806 0.175 0.0108289
1127.23 995 0.013689254 0.075 0.010809861
1121.22 995 0.010951403 0.1 0.010954211
1122.22 1010 0.128678987 3.5 0.011561104
1123.67 1010 0.125941136 3 0.011490423
1126.33 1010 0.123203285 2.4 0.011465478
1131.92 1010 0.120465435 2.4 0.011459139
1121.86 1010 0.117727584 3.1 0.011313808
1127.23 1010 0.109514031 2.45 0.011324488
1121.22 1010 0.106776181 2.9 0.011172599
;
run;
* Set the Option Variables and Call the Macros;
data b;
set data1;
%let s=eq_price; * underlying security price;
%let x=strike; * strike price;
%let n=7; * nt = n = number of subperiods;
%let t=time; * time to maturity, in years;
%let h=&t/&n;* it=h=T/n= size of the sub-period;
%let r=intrate; * r = continuously compounded risk free rate;
%let q=0.0254; * dividend yield;
%let sig=0.24; *implied Volatility;

%let u =exp(&sig*sqrt(&h));
%let d =exp(-&sig*sqrt(&h));
%let rq=exp((&r-&q)*&h);
%let p =(&rq-&d)/(&u-&d);

p=%p(0,&s);
c=%c(0,&s);
output;
run;

and the out put is

The SAS System 11:42 Saturday, May 21, 2011 5

Obs eq_price strike time opt_price intrate p c

1 1108.48 995 0.04107 0.800 0.011071 0.17516 113.487
2 1122.22 995 0.03285 0.350 0.011019 0.00000 127.220
3 1123.67 995 0.03012 0.300 0.010989 0.00000 128.670
4 1126.33 995 0.02738 0.200 0.010925 0.00000 131.330
5 1131.92 995 0.02464 0.200 0.010853 0.00000 136.920
6 1121.86 995 0.02190 0.175 0.010829 0.00000 126.860
7 1127.23 995 0.01369 0.075 0.010810 0.00000 132.230
8 1121.22 995 0.01095 0.100 0.010954 0.00000 126.220
9 1122.22 1010 0.12868 3.500 0.011561 4.54713 115.75
10 1123.67 1010 0.12594 3.000 0.011490 4.33255 117.00
11 1126.33 1010 0.12320 2.400 0.011465 4.04350 119.39
12 1131.92 1010 0.12047 2.400 0.011459 3.57726 124.54
13 1121.86 1010 0.11773 3.100 0.011314 4.04854 114.96
14 1127.23 1010 0.10951 2.450 0.011324 3.31856 119.67
15 1121.22 1010 0.10678 2.900 0.011173 3.54189 113.90


But my macro use the sig which is only at 0.24,at this level, the answer p is not same to opt_price.
I want to use the macro to change sig variable in my marco util the answer for p is same to opt_price.

Please hlep me and give me a example to solve this problem?
Thx!
SAS Employee
Posts: 58

Re: How to use marco to change variable?

Posted in reply to nolonglove
I guess you need to write some sort of a %DO %UNTIL loop for this?
Valued Guide
Posts: 634

Re: How to use marco to change variable?

Posted in reply to nolonglove
You need to review the difference between the routine CALL SYMPUTX and the use of %LET in a DATA step. It is not possible to assign DATA step variable's value to a macro variable using a %LET - the DATA step routine SYMUTX is used instead. It is very important to understand why.

in your program (snippet shown here).
[pre]
data b;
set data1;
%let s=eq_price; * underlying security price;
[/pre]
the macro variable &S will contain the letters e-q-_-p-r-i-c-e, not the variable and not the value of the variable. This means that the %IF in the macro %P will always be false.
Ask a Question
Discussion stats
  • 2 replies
  • 195 views
  • 0 likes
  • 3 in conversation