run_macro inside proc fcmp

Reply
Occasional Contributor
Posts: 19

run_macro inside proc fcmp

Hi,

I am solving a optimization problem where I am using proc fcmp to define objective function. I need to call a macro from proc fcmp and I am using run_macro for that.

So overall inside proc optmodel, I am calling proc fcmp and proc fcmp in turn is calling a macro using "run_macro".

But I am getting an error show below after compiling the code:

ERROR: Function RUN_MACRO not found.

ERROR: Failed to compile functions

options mprint ;

options mlogic ;

options symbolgen;

libname ilib "C:\Users\idnshg\Desktop\temp\date\16.1\OR";

%macro abc();

data _null_;

z=&x+&y;

call symput('z',z);

run;

%mend abc;

proc fcmp outlib=work.functions.wrapper;

function test_sum(x,y);

rc=run_macro('abc',x,y,z); 

return(z);

endsub;

run;

options cmplib=work.functions;

/*test prog for sas or*/

proc optmodel;

var x, y;

min f =x**2-x-2*y-x*y+y**2+(test_sum(x,y)); 

solve;

print x y;

quit;

Super User
Posts: 5,257

Re: run_macro inside proc fcmp

I'm not an experienced FCMP programmer, but I can see the difficulties in having a PROC call a user written function, which in turn call a macro which need to execute a data step. Can't see how this could work...

Why do you need a macro, can't you wrap that functionality within another function? I'm guessing you simplified your example, but what is your business case?

Even if you could use a macro, it would have been more logically to use an in-line macro instead, not going around and using a global macro variable.

Data never sleeps
Occasional Contributor
Posts: 19

Re: run_macro inside proc fcmp

I want to manipulate a dataset using data step inside the macro. Apart from the data step a big algorithm needs to be executed. So macro is essential. I can actually call this macro from function defined from proc fcmp using

run_macro. But this steps works when I call proc fcmp from data step but not in the case when i call function from proc optmodel. 

Super User
Posts: 5,257

Re: run_macro inside proc fcmp

ok, from SAS/OR doc:

"Not all PROC FCMP functionality is compatible with PROC OPTMODEL; in particular, the following FCMP functions are not supported and should not be called within your FCMP function definitions: READ_ARRAY, WRITE_ARRAY, RUN_MACRO, and RUN_SASFILE. In many cases, OPTMODEL capabilities can replace these functions."

Since PROC OPTMODEL offers some programming facilities, you can perhaps avoid having to call a macro...?

Data never sleeps
Occasional Contributor
Posts: 19

Re: run_macro inside proc fcmp

Unable to find any programming facility in proc optmodel which could solve my problem.

Super User
Posts: 5,257

Re: run_macro inside proc fcmp

Sorry to hear that.

If you could describe your requirement in bit more detail, perhaps some OPTMODEL/FCMP pro might be able to help...

Data never sleeps
Respected Advisor
Posts: 3,895

Re: run_macro inside proc fcmp

Would it be a possible to not use a macro but code everything directly within Proc FCMP?

proc fcmp outlib=work.functions.wrapper;

  function test_sum(x,y);

    z=x+y;

    return(z);

  endsub;

run;

Occasional Contributor
Posts: 19

Re: run_macro inside proc fcmp

Thanks for your suggestion Patrick.

It is not possible as I also need to include data step in my analysis, which I think is probably not possible in proc FCMP.

Super Contributor
Posts: 644

Re: run_macro inside proc fcmp

I believe the whole problem in your approach is that you are trying to smuggle in a datastep in the middle of a procedure, not normally possible in SAS.  Using a macro does not help because all the macro does is resolve code, it doesn't run the datastep.  Your example must be oversimplified and I assume it contains a set or merge statement to access a SAS table with additional SAS code used to evaluate the function test(). 

If that is the case you may be able to do what you want by using the more primitive SCL file functions to open, read and close a SAS dataset without using a SAS datastep.  Investigate Dopen(), Fopen(), Fread(), Fclose() and Dclose().  I do not know whether you can use them in Proc FCMP but it is worth a try.  Then you could do your calculations on the dataset variables directly in FCMP using the same code you would use in the datastep.  You probably would not even need to use macro variables.

Richard

Ask a Question
Discussion stats
  • 8 replies
  • 610 views
  • 6 likes
  • 4 in conversation