I am creating a custom function using FCMP proceedure I want to define a optional argument. How can I do it?
For example, in the below code mod is optional for me. Is there any way?
proc fcmp outlib=misc.functions.funcs;
function scanw(var$, n, dlm$, mod$) $;
<my code>
endsub;
quit;
if i am not passing a value to mod then SAS gives error
ERROR 71-185: The scanw function call does not have enough arguments.
Thanks in advance.
Rajaram
Hi Rajaram,
Proc fcmp supports the VARARGS argument. This indicates you have a variable list of arguments. The argument must be an array:
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;
It may not be entirely what you were hoping for as this implies some limitations. But that's up to you.
See the doc for details.
Regards,
- Jan
Thanks Jan. I read full documentation optional argments are not available. Even I tried below example as follows and i am getting error. within proc fcmp it was working.
options cmplib=work.funcs;
proc fcmp outlib=work.funcs.temp;
function summation (b[*]) varargs;
total = 0;
do i = 1 to dim(b);
total = total + b[i];
end;
return(total);
endsub;
run;
data _null_;
sum=summation(1,2,3,4,5);
put sum=;
run;
Log 252 data _null_; 253 sum=summation(1,2,3,4,5); 253 sum=summation(1,2,3,4,5); --------- 72 ERROR 72-185: The summation function call has too many arguments. 253 sum=summation(1,2,3,4,5); --------- 707 ERROR 707-185: Expecting array for argument 1 of the summation subroutine call. 254 put sum=; 255 run; NOTE: The SAS System stopped processing this step because of errors. NOTE: DATA statement used (Total process time): real time 0.03 seconds cpu time 0.01 seconds
Hi @Rajaram
The doc states that when using VARARGS "the last argument in the function must be an array".
So this works:
data _null_;
array args{5} _temporary_ (1 2 3 4 5);
sum=summation(args);
put sum=;
run;
As I dsaid before some limitatioons apply. An array knows only elemnts of all the same type. So depending on your use case this may or may not be valuable.
I must admit, why it does work from fcmp itself is beyond me.
Hope this helps,
- Jan.
Here is a slight variation on Jan's approach.
Another annoying feature of PROC FCMP is that it only supports positional arguments. So, I often do something like this:
function scanw( oneArg$ ); firstarg = scan( oneArg, 1, '#' ); secondarg = scan( oneArg, 2, # ); et cetera end;
In other words, you pass in one string of parameters separated by some delimiter. This allows you full control of how to handle the parameter string. Often, I make the parameter string a set of name-value pairs, for example:
'ABC:value#DEF:67#GHI:ddd'
So, three name-value pairs separated by the octothorpe. The name-value pairs themselves are joined by the colon.
HTH - Jack
Thank you@jklaverstijn
Thank you @sh0e, I have designed my code like that but my objective was to design likeSCANW(string, count,charlist <,modifiers>)
We have to accept some limitation.
Thanks all
Hi,
even though it is mentioned in the doc that the last argument must be an array, there must be a solution. I wonder how it is done for sum, mean, and all the other base math functions that allow variable arguments.. Is there some workaround or how was it done for these kind of functions?
I would really appreciate a more elegant solution..
Cheers, *obj*
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.