0
votes

I am trying to build a custom transformation in SAS DI. This transformation will "act" on columns in an input data set, producing the desired output. For simplicity let's assume the transformation will use input_col1 to compute output_col1, input_col2 to compute output_col2, and so on up to some specified number of columns to act on (let's say 2).

In the Code Options section of the custom transformation users are able to specify (via prompts) the names of the columns to be acted on; for example, a user could specify that input_col1 should refer to the column named "order_datetime" in the input dataset, and either make a similar specification for input_col2 or else leave that prompt blank.

Here is the code I am using to generate the output for the custom transformation:

data cust_trans;
    set &_INPUT0;

    i=1;
    do while(i<3);
        call symputx('index',i);
        result = myfunc("&&input_col&index");
        output_col&index = result; /*what is proper syntax here?*/
        i = i+1;
    end;
run;

Here myfunc refers to a custom function I made using proc fcmp which works fine.

The custom transformation works fine if I do not try to take into account the variable number of input columns to act on (i.e. if I use "&&input_col&i" instead of "&&input_col&index" and just use the column result on the output table).

However, I'm having two issues with trying to make the approach more dynamic:

  • I get the following warning on the line containing result = myfunc("&&input_col&index"):

    WARNING: Apparent symbolic reference INDEX not resolved.

  • I do not know how to have the assignment to the desired output column happen dynamically; i.e., depending on the iteration of the do loop I'd like to assign the output value to the corresponding output column.

I feel confident that the solution to this must be well known amongst experts, but I cannot find anything explaining how to do this.

Any help is greatly appreciated!

1

1 Answers

1
votes

You can't use macro variables that depend on data variables, in this manner. Macro variables are resolved at compile time, not at run time.

So you either have to

%do i = 1 %to .. ;

which is fine if you're in a macro (it won't work outside of an actual macro), or you need to use an array.

data cust_trans;
    set &_INPUT0;
    array in[2] &input_col1 &input_col2;  *or however you determine the input columns;
    array output_col[2]; *automatically names the results;
    do i = 1 to dim(in);
        result = myfunc(in[i]); *You quote the input - I cannot see what your function is doing, but it is probably wrong to do so;
        output_col[i] = result; /*what is proper syntax here?*/
    end;
run;

That's the way you'd normally do that. I don't know what myfunc does, and I also don't know why you quote "&&input_col&index." when you pass it to it, but that would be a strange way to operate unless you want the name of the input column as text (and don't want to know what data is in that variable). If you do, then pass vname(in[i]) which passes the name of the variable as a character.