Search code examples
sassas-macro

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

  • Few comments:

    • You are missing a semi-colon ; when assigning S.
    • You don't have to use calculated i, use i directly.
    • Power operator in SAS is ** and not ^.
    • You point to t instead of the macro-variable &t. in the do loop statement.
    • You increment n, not t so you need to use n in 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