RUN_MACRO Function
Executes a predefined SAS macro.
Category: |
Calling SAS Code from within Functions |
Note: |
The behavior of this function is similar to executing %macro_name; in
SAS.
|
Syntax
rc = RUN_MACRO('macro_name' <, variable_1,
..., variable_n>);
Required Arguments
- rc
-
is 0 if the function
is able to submit the macro. The return code indicates only that the
macro call was attempted. The macro itself should set the value of
a SAS macro variable that corresponds to a PROC FCMP variable to determine
whether the macro executed as expected.
- macro_name
-
specifies the name
of the macro to be run.
Requirement:macro_name must
be a string enclosed in quotation marks or a character variable that
contains the macro to be executed.
Optional Argument
- variable
-
specifies optional
PROC FCMP variables, which are set by macro variables of the same
name. These arguments must be PROC FCMP double or character variables.
Before SAS executes
the macro, SAS macro variables are defined with the same name and
value as the PROC FCMP variables. After SAS executes the macro, the
macro variable values are copied back to the corresponding PROC FCMP
variables.
Examples
Example 1: Executing a Predefined Macro with PROC FCMP
This example creates
a macro called TESTMACRO, and then uses the macro within PROC FCMP
to subtract two numbers.
/* Create a macro called TESTMACRO. */
%macro testmacro;
%let p = %sysevalf(&a - &b);
%mend testmacro;
/* Use TESTMACRO within a function in PROC FCMP to subtract two numbers. */
proc fcmp outlib = sasuser.ds.functions;
function subtract_macro(a, b);
rc = run_macro('testmacro', a, b, p);
if rc eq 0 then return(p);
else return(.);
endsub;
run;
/* Make a call from the DATA step. */
option cmplib = (sasuser.ds);
data _null_;
a = 5.3;
b = 0.7;
p = .;
p = subtract_macro(a, b);
put p=;
run;
Output from Executing a Predefined Macro with PROC FCMP
Example 2: Executing a DATA Step within a DATA Step
This example shows how
to execute a DATA step from within another DATA step. The program
consists of the following sections:
-
The first section of the program
creates a macro called APPEND_DS. This macro can write to a data set
or append a data set to another data set.
-
The second section of the program
calls the macro from function writeDataset in PROC FCMP.
-
The third section of the program
creates the SALARIES data set and divides the data set into four separate
data sets depending on the value of the variable Department.
-
The fourth section of the program
writes the results to the output window.
/* Create a macro called APPEND_DS. */
%macro append_ds;
/* Character values that are passed to RUN_MACRO are put */
/* into their corresponding macro variables inside of quotation */
/* marks. The quotation marks are part of the macro variable value. */
/* The DEQUOTE function is called to remove the quotation marks. */
%let dsname = %sysfunc(dequote(&dsname));
data &dsname
%if %sysfunc(exist(&dsname)) %then %do;
modify &dsname;
%end;
Name = &Name;
WageCategory = &WageCategory;
WageRate = &WageRate;
output;
stop;
run;
%mend append_ds;
/* Call the APPEND_DS macro from function writeDataset in PROC FCMP. */
proc fcmp outlib = sasuser.ds.functions;
function writeDataset (DsName $, Name $, WageCategory $, WageRate);
rc = run_macro('append_ds', dsname, DsName, Name, WageCategory, WageRate);
return(rc);
endsub;
run;
/* Use the DATA step to separate the salaries data set into four separate */
/* departmental data sets (NAD, DDG, PPD, and STD).
*/
data salaries;
input Department $ Name $ WageCategory $ WageRate;
datalines;
BAD Carol Salaried 20000
BAD Beth Salaried 5000
BAD Linda Salaried 7000
BAD Thomas Salaried 9000
BAD Lynne Hourly 230
DDG Jason Hourly 200
DDG Paul Salaried 4000
PPD Kevin Salaried 5500
PPD Amber Hourly 150
PPD Tina Salaried 13000
STD Helen Hourly 200
STD Jim Salaried 8000
;
run;
options cmplib = (sasuser.ds) pageno=1 nodate;
data _null_;
set salaries;
by Department;
length dsName $ 64;
retain dsName;
if first.Department then do;
dsName = 'work.' || trim(left(Department));
end;
rc = writeDataset(dsName, Name, WageCategory, wageRate);
run;
proc print data = work.BAD; run;
proc print data = work.DDG; run;
proc print data = work.PPD; run;
proc print data = work.STD; run;
Output for Calling a DATA Step within a DATA Step
The SAS System
1
Wage Wage
Obs Name Category Rate
1 Carol Salaried 20000
2 Beth Salaried 5000
3 Linda Salaried 7000
4 Thomas Salaried 9000
5 Lynne Hourly 230
The SAS System
2
Wage Wage
Obs Name Category Rate
1 Jason Hourly 200
2 Paul Salaried 4000
The SAS System
3
Wage Wage
Obs Name Category Rate
1 Kevin Salaried 5500
2 Amber Hourly 150
3 Tina Salaried 13000
The SAS System
4
Wage Wage
Obs Name Category Rate
1 Helen Hourly 200
2 Jim Salaried 8000