'do loop within a macro
The code below is returning the following error message:
Syntax error, expecting one of the following: a name, INPUT, PUT.
%macro run_calculation(amt, t, r);
data customer_value;
i=&r./100.;
do n=0 to t;
S=&amt.*[(1+ calculated i)^t - 1]/calculated i
end;
run;
%mend;
%run_calculation(amt=1000, t=8, r=5);
Expected output is S value at each t in a table.
Solution 1:[1]
Few comments:
- You are missing a semi-colon
;when assigning S. - You don't have to use
calculated i, useidirectly. - Power operator in SAS is
**and not^. - You point to
tinstead of the macro-variable&t.in the do loop statement. - You increment
n, nottso you need to usenin your formula. - Use parenthesis, not brackets.
- You are missing an explicit output statement in order to get S value for each n.
%macro run_calculation(amt, t, r);
data customer_value;
i=&r./100.;
do n=0 to &t.;
S=&amt.*((1+i)**n - 1)/i;
output;
end;
run;
%mend;
%run_calculation(amt=1000, t=8, r=5);
i n S
0.05 0 0
0.05 1 1000
0.05 2 2050
0.05 3 3152.5
0.05 4 4310.125
0.05 5 5525.63125
0.05 6 6801.9128125
0.05 7 8142.0084531
0.05 8 9549.1088758
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
