Search code examples
multidimensional-arraysas-macro

SAS Two dimensional array with macro variables as dimensions


Can you help for this question please,

How to define two dimensional arrays with flexible dimensions depending on macro variables in SAS?

%LET Dim = &a.*&b.;

ARRAY Spline{&a.,&b.} B1-B&Dim.;

I tried with the above code but it gave these errors:

http://s17.postimg.org/v5nkxw8hb/Sans_titre.png

Thanks for the help!


Solution

  • During a macro variable assingment you can not have a mathematical expression.

    So if a = 4 and b = 5

    %LET Dim = &a.*&b.;

    resolves to DIM = "4*5" as string, but you want 20 instead.

    to do this, you have to do the mathematical operation in a datastep:

    data _null_;
        temp= &a * &b;
        call symputx('DIM', temp);
    
        ARRAY Spline{&a.,&b.} B1-B&DIM;
    run;
    

    Also i wonder about your screenshot, it seems that B = 5-1 (as string), so i guess you already did a matehematical operation when assinging b, which failed... %let b = 5-1; resolves to a string "5-1", not to 4...

    so in your example everything resolves to:

     ARRAY Spline{4,5-1} B1-B4*5-1;
    

    i guess you wanted

        ARRAY Spline{4,4} B1-B16;
    

    so seperate all mathematical operations in a datastep...