The RUN_MACRO and RUN_SASFILE functions are part of the FCMP procedure in SAS® 9.2. With the introduction of these functions, it is possible to call full PROC steps and DATA steps from within a PROC FCMP step. Because the FCMP procedure also provides the ability to define functions that can be called from the DATA step and other SAS procedures, you can combine these two features to call full PROCS step and DATA step code from within another DATA step or SAS procedure. For a list of SAS procedures that can call functions that are defined via the FCMP procedure, see The FCMP Procedure.
This sample uses the RUN_MACRO function to demonstrate the capability that is described above by making a call to the QUAD subroutine from within a DATA step. The QUAD subroutine, which is used to perform numerical integration, is built into the IML procedure. The QUAD subroutine is not available within DATA step, but this sample provides a way to call SAS/IML® software's QUAD subroutine from within DATA step.
The RUN_MACRO function executes a predefined macro. The following SAS code defines the macro that is to be executed:
The macro, named MACRO_QUAD, contains a full PROC IML step. This PROC IML step is the code that is to be executed from within the DATA step. In the MACRO_QUAD macro, the PROC IML step defines a module, defines a limits vector, and calls the QUAD and SYMPUTX subroutines.
The ARGS and F macro variables are passed from the DATA step to the MACRO_QUAD macro as quoted strings. Because PROC IML needs the values of ARGS and F without quotes, the DEQUOTE function is used to remove them.
The START and FINISH statements are used to define the module. In this case, the module is a function that is named f. The parameters for the function f are the value of the ARGS macro variable. Within the START and FINISH statements, the function f is defined by the contents of the F macro variable.
Following the definition of the function f, the limits of integration are specified by using the values of the A and B macro variables. The function f and limit vector a are passed to the QUAD subroutine to calculate the integral z. The integral z is then assigned to the macro variable Z via the SYMPUTX subroutine.
The next set of SAS code defines a SAS function in PROC FCMP that uses the RUN_MACRO function to call the MACRO_QUAD macro that is defined in the previous SAS code:
The fcmp_quad function has two purposes:
Use the CMPLIB= SAS system option to make a SAS function that is defined via the FCMP procedure available to DATA step and other SAS procedures that support functions and subroutines:
The fcmp_quad SAS function can now be called from within a DATA step. Suppose that you want to compute the following integration:
To compute the integration, use the following SAS code:
In this code:
Each of those variables are passed to the fcmp_quad function, and the result is assigned to out and printed to the SAS log.
When the above DATA step is executed, the resulting value is 0.4999999996. It is the numerical integration result that is computed by defining an integration and by performing a SAS/IML QUAD subroutine call from within a DATA step.
For the complete set of SAS code that is described in this sample, click the Full Code tab. The Results tab contains the details that are printed to the SAS log.
In this particular case, from an efficiency standpoint, the preference is to use SAS/IML® Studio or SAS/IML 9.22 or later. Nevertheless, for both clarity and demonstration purposes, the author took the approach explained in this sample.
SAS Institute Inc. 2010. Adding Statistical Functionality to the DATA Step with PROC FCMP. SAS Global Forum 2010 Cary, NC: SAS Institute Inc. Available at support.sas.com/resources/papers/proceedings10/326-2010.pdf.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Turn on detailed macro logging options. */
options mprint mlogic symbolgen;
/* Define a macro function that contains a call to the quad
subroutine in SAS/IML® software. */
%macro macro_quad();
%let args=%sysfunc(dequote(&args));
%let f=%sysfunc(dequote(&f));
proc iml;
start f(&args);
&f
finish;
a={&a &b};
call quad(z,"f",a);
call symputx('z',z);
run;
quit;
%mend macro_quad;
/* Define a SAS function that calls the macro function, which in turn
calls the quad subroutine */
proc fcmp outlib=work.ds.functions;
function fcmp_quad(a,b,f $,args $);
rc=run_macro("macro_quad",a,b,f,args,z);
if rc eq 0 then return(z);
else return(.);
endsub;
run;
quit;
/* Store the SAS function library. */
option cmplib=(work.ds);
/* Define a function to numerically integrate using the quad subroutine. */
data _null_;
a=0; /* left limit of integration */
b=1; /* right limit */
f='y=x; return(y);'; /* integrand and dependent variable */
args='x'; /* independent variable for the function */
out=fcmp_quad(a,b,f,args); /* Call the fcmp-defined function. */
put out=; /* Print the integral to the log. */
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
1 /* turn on detailed macro logging options */ 2 options mprint mlogic symbolgen; 3 4 5 /* define a macro function that contains a call to the quad 6 subroutine in sas/iml */ 7 %macro macro_quad(); 8 %let args = %sysfunc(dequote(&args)); 9 %let f = %sysfunc(dequote(&f)); 10 proc iml; 11 start f(&args); 12 &f 13 finish; 14 a={&a &b}; 15 call quad(z,"f",a); 16 call symputx('z',z); 17 run; 18 quit; 19 %mend macro_quad; 20 21 22 /* define a SAS function that calls the macro function which in turn 23 calls the quad subroutine */ 24 proc fcmp outlib=work.ds.functions; 25 function fcmp_quad(a,b,f $,args $); 26 rc=run_macro("macro_quad",a,b,f,args,z); 27 if rc eq 0 then return(z); 28 else return(.); 29 endsub; 30 run; NOTE: Function fcmp_quad saved to work.ds.functions. NOTE: PROCEDURE FCMP used (Total process time): real time 0.03 seconds cpu time 0.03 seconds 31 quit; 32 33 34 /* store the SAS function library */ 35 option cmplib=(work.ds); 36 37 38 /* define a function to numerically integrate using the quad subroutine */ 39 data _null_; 40 a=0; /* left limit of integration */ 41 b=1; /* right limit */ 42 f='y=x; return(y);'; /* integrand and dependent variable */ 43 args='x'; /* independent variable for the function */ 44 out=fcmp_quad(a,b,f,args); /* call the fcmp-defined function */ 45 put out=; /* print integral to the log */ 46 run; MLOGIC(MACRO_QUAD): Beginning execution. MLOGIC(MACRO_QUAD): %LET (variable name is ARGS) SYMBOLGEN: Macro variable ARGS resolves to 'x' MLOGIC(MACRO_QUAD): %LET (variable name is F) SYMBOLGEN: Macro variable F resolves to 'y=x; return(y);' MPRINT(MACRO_QUAD): proc iml; SYMBOLGEN: Macro variable ARGS resolves to x MPRINT(MACRO_QUAD): start f(x); SYMBOLGEN: Macro variable F resolves to y=x; return(y); MPRINT(MACRO_QUAD): y=x; MPRINT(MACRO_QUAD): return(y); MPRINT(MACRO_QUAD): finish; SYMBOLGEN: Macro variable A resolves to 0 SYMBOLGEN: Macro variable B resolves to 1 MPRINT(MACRO_QUAD): a={0 1}; MPRINT(MACRO_QUAD): call quad(z,"f",a); MPRINT(MACRO_QUAD): call symputx('z',z); MPRINT(MACRO_QUAD): run; MPRINT(MACRO_QUAD): quit; MLOGIC(MACRO_QUAD): Ending execution. out=0.4999999996 NOTE: DATA statement used (Total process time): real time 0.89 seconds cpu time 0.11 seconds
Type: | Sample |
Topic: | Common Programming Tasks ==> Reusing Code SAS Reference ==> DATA Step SAS Reference ==> Procedures ==> FCMP SAS Reference ==> Procedures ==> IML SAS Reference ==> Macro |
Date Modified: | 2010-11-09 15:13:15 |
Date Created: | 2010-08-13 15:27:02 |
Product Family | Product | Host | Product Release | SAS Release | ||
Starting | Ending | Starting | Ending | |||
SAS System | Base SAS | z/OS | 9.21 | 9.2 TS2M0 | ||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows XP 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft® Windows® for x64 | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Datacenter Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Enterprise Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Standard Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 for x64 | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2008 for x64 | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows XP Professional | 9.21 | 9.2 TS2M0 | ||||
Windows Vista | 9.21 | 9.2 TS2M0 | ||||
Windows Vista for x64 | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled AIX | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled HP-UX | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled Solaris | 9.21 | 9.2 TS2M0 | ||||
HP-UX IPF | 9.21 | 9.2 TS2M0 | ||||
Linux | 9.21 | 9.2 TS2M0 | ||||
Linux for x64 | 9.21 | 9.2 TS2M0 | ||||
OpenVMS on HP Integrity | 9.21 | 9.2 TS2M0 | ||||
Solaris for x64 | 9.21 | 9.2 TS2M0 |