Home > Mobile >  In SAS, is there a test for whether a function exists?
In SAS, is there a test for whether a function exists?

Time:09-19

I am creating a new function (trims). Before running that code however I would like to check if I've already created the function, and if so delete it. The part that is not working for me is the first line, checking for the existence of a function. I have tried variation, to the memtype (i.e. change "catalog" to "all", or deleted it, and let it default.) I have also tried the cexist function. No luck. Help!

I'm trying to elimanate the WARNINGs the code produces if I don't first delete the function...

        %if %sysfunc(exist(userfuncs.package1,catalog)) %then %do;
            /* delete previous function definition during debugging */
            options cmplib=work.userfuncs;
            proc fcmp outlib=work.userfuncs.package1;
               deletefunc trims;
            run;
        %end;

        /* new function defintion */
        proc fcmp outlib=work.userfuncs.package1;
           function trims(f $, str $, clist $, mod $) $32767;
              from = 1;
              last = length(str);
              if upcase(f) in ('L', 'B') then from = findc(str, clist, 'K'||mod);
              if from=0 then return('');
              if upcase(f) in ('T', 'B') then last = findc(str, clist, 'K'||mod, -last); 
              if last=0 then return('');
              return(substr(str, from, last-from 1));      
           endfunc; 
        run;

I'm running this from SAS-EG 7.13, connecting to a Unix server

Thanks Rodney

CodePudding user response:

Your code is creating a SAS dataset named WORK.USERFUNCS. So if you want to test if a function named TRIMS exist in a package named PACKAGE1 just check if there are any observations in WORK.USERFUNCS where _KEY_="F.PACKAGE1.TRIMS".

CodePudding user response:

Instead of checking the actual function exists, wrap the creation of the function inside macro and skip the compilation if it's already done, otherwise set a global flag the first time it runs...

%MACRO COMPILE_FCMP ;
  %GLOBAL _FCMPCOMPILED_ ;
  %IF &_FCMPCOMPILED_ = 1 %THEN %GOTO _EXIT ;

  /* compile fcmp function */
  proc fcmp ... ;
  run ;
  %LET _FCMPCOMPILED_ = 1 ;

  %_EXIT: ;
%MEND ;
  • Related