Search code examples
sasfcmp

How to assign output from SOLVE function in PROC FCMP (SAS) to global variable/table?


I'm trying to use PROC FCMP with the function SOLVE to solve non-linear equations and insert solutions into the table automatically. For instance

proc fcmp;
      /* define the function */
   function inversesqrt(x);
      return(1/sqrt(x));
   endsub;

   y = 20;
   x = solve("inversesqrt", {.}, y, .);
   put x;
run;

After running above code x will be shown in results but i cannot utilize it in further code. I tried to save x as macro variable or as table but nothing works for me. Can somebody help me, please?


Solution

  • Create a second fcmp function to return the solve.

    options cmplib=work.funcs;
    
    proc fcmp outlib=work.funcs.sandbox;
          /* define the function */
       function InverseSqrt(x);
          return(1/sqrt(x));
       endsub;
    
       function SolveInverseSqrt(arg);
         return (solve('InverseSqrt', {.}, arg, .));
       endsub;
    run;
    
    %let x = %sysfunc(SolveInverseSqrt(20));
    %put &=x;