0
votes
data example1;
    input var1 var2 var3;
    datalines;
    10 11 14
    3 5 8
    0 1 2
;

data example2;
    input var;
    datalines;
    1
    2
    8
;

Let's say that the number of var variables depending on data input. I want to put that number to macro variable and use in another data step, for example:

%macro m(input);

    data &input.;
        set &input.;

        array var_array[*] var:;

        %let array_dim = dim(var_array);

        do i = 1 to &array_dim;

            var_array[i] = var_array[i] + 1;

        end;

        drop i;

    run;

    data example2;
        set example2;

        var2 = var * &array_dim; /* doesn't work */

    run;

%mend;

%m(example1);

%let array_dim = dim(var_array); doesn't work in second data step, because dim(var_array) isn't evaluated, but %eval or %sysevalf in declaring the macro variable does't work here. How to do that correctly?

1

1 Answers

1
votes

You are mixing up macro code and data step code in a way that is not supported in SAS. If you want to assign a macro variable a value that you're generating as part of a data step, you need to use call symput.

Also, if you create a macro variable during a data step, you cannot resolve it during the same data step in the way that you are attempting to do (unless you use the resolve function...). It's easier just to use a data set variable for this.

So here's a fixed version of your code that I think probably does what you want:

%macro m(input);

    data &input.;
        set &input.;

        array var_array[*] var:;

        array_dim = dim(var_array);

        /*Only export the macro variable once, for the first row*/
        if _n_ = 1 then call symput('array_dim_mvar', array_dim);

        do i = 1 to array_dim;

            var_array[i] = var_array[i] + 1;

        end;

        drop i;

    run;

    data example2;
        set example2;

        var2 = var * &array_dim_mvar;

    run;

%mend;

%m(example1);