Search code examples
sasfcmp

FCMP with VARARGS not working as expected?


While researching FCMP to help answer another question on here, I was left a bit perplexed by how proc fcmp works when using the VARARGS option in order to be able to call that function with a variable number of arguments. The sas support page for the FUNCTION statement provides the following example and clearly states that "the example implies that the summation function can be called as follows: sum = summation(1, 2, 3, 4, 5);."

options cmplib=sasuser.funcs;

proc fcmp outlib=sasuser.funcs.temp;
function summation (b[*]) varargs;
    total = 0;
    do i = 1 to dim(b);
        total = total + b[i];
    end;
return(total);
endsub;
sum=summation(1,2,3,4,5);
   put sum=;
run;

Running this seems to work well and produces an output report showing sum=15 which seems to indicate that calling the function as summation(1,2,3,4,5) works as expected.

However, if I then try to use that function in the same way in a data step

data _null_;
test=summation(1,2,3,4,5);
run;

I get errors in the log

ERROR 72-185: The summation function call has too many arguments.

ERROR 707-185: Expecting array for argument 1 of the summation subroutine call.

This has me confused. Am I missing something obvious?

The second error message says that the function is expecting an array as argument 1. Forgetting the fact that calling the function that way in the fcmp proc seemed to work and that the SAS support seems to indicate that this is the whole point of this; expecting an array, which can indeed be of varying length, is really not the same as accepting a variable number of arguments, an array being one argument


Solution

  • If you specify VARARGS, then the last argument in the function must be an array.

    And later on:

    Note: When calling this function from a DATA step, you must provide the VARARGS as an array.

    http://documentation.sas.com/?docsetId=proc&docsetTarget=n10vesidziklh1n1l7kidq4dqd0r.htm&docsetVersion=9.4&locale=en

    Please make sure to use the latest version of the documentation, in this case 9.4. Unless you happen to unfortunately be stuck on version 9.2. This works for me - note that it's not how I expected it to work as well...but it does :).

    data demo;
        array test(4) (1, 2, 3, 4);
        check = summation(test);
        put check=;
    run;