Previous Page | Next Page

The FCMP Procedure

Functions for Calling SAS Code from Within Functions


The RUN_MACRO Function


Syntax of the RUN_MACRO Function

The RUN_MACRO function executes a predefined SAS macro. Its behavior is similar to executing %macro_name; in SAS.

The following two forms of the RUN_MACRO function are available:

rc = RUN_MACRO ('macro_name')
rc = RUN_MACRO ('macro_name', variable_1, variable_2, ...)

where

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.
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.


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

p=4.6

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:

   /* 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

The RUN_SASFILE Function


The Syntax of the RUN_SASFILE Function

The RUN_SASFILE function executes the SAS code in the fileref that you specify.

The following two forms of the RUN_SASFILE function are available:

rc = RUN_SASFILE ('fileref_name');
rc = RUN_SASFILE('fileref_name', variable-1, variable-2, ...)

where

rc

is 0 if the function is able to submit a request to execute the code that processes the SAS file. The return code indicates only that the call was attempted.

fileref_name

specifies the name of the SAS fileref that points to the SAS code.

Requirement: fileref_name must be a string enclosed in quotation marks or a character variable that contains the name of the SAS fileref.
variable

specifies optional PROC FCMP variables which will be set by macro variables of the same name. These arguments must be PROC FCMP double or character variables.

Before SAS executes the code that references the SAS file, the SAS macro variables are defined with the same name and value as the PROC FCMP variables. After execution, these macro variable values are copied back to the corresponding PROC FCMP variables.


Example

The following example is similar to the first example for RUN_MACRO except that RUN_SASFILE uses a SAS file instead of a predefined macro. This example assumes that test.sas(a, b, c) is located in the current directory.

      /* test.sas(a,b,c) */
data _null_;
   call symput('p', &a * &b);
run;

   /* Set a SAS fileref to point to the data set. */
filename myfileref "test.sas"; 

   /* Set up a function in PROC FCMP and call it from the DATA step. */
proc fcmp outlib = sasuser.ds.functions;
   function subtract_sasfile(a,b);
   rc = run_sasfile('myfileref', a, b, p);
   if rc = 0 then return(p);
      else return(.);
   endsub;
run;

options cmplib = (sasuser.ds);
data _null_;
   a = 5.3;
   b = 0.7;
   p = .;
   p = subtract_sasfile(a, b);
   put p=;
run;

Previous Page | Next Page | Top of Page