Monday, February 18, 2013

Using DOSUBL to write Macros as functions

In my last post, DOSUB and DOSUBL - Data Driven Development, I mentioned one of the many uses of these very powerful new functions. I'd like to discuss another Use Case: using them to facilitate creating your own macros that work like functions.

Macros that need to return a single value, as opposed to generating code are often written as functions so they can be used, for example, as follows:
  • %let total = %getMax(data= . . ., var =  . . . );
or
  • retain  denom %getMax(data= . . ., var = . . . );
However if the macro needs to run some SAS code, it is not possible to call the macro this way since the generated code would be returned by the macro instead of the desired value. There are a number of work-arounds that many SAS programmers have used in this case, such as:
  • store the value into a macro variable that the calling program must know about;
  • save the value into a data set that the calling program must use to get the value;
  • use the %sysfunc macro along with the data access functions.
The DOSUB and DOSUBL functions provide another way to do this. They can be used via the %sysfunc macro to run the code. Consider the following real-life Use Case, which I use the Parameter File Maintenance (PFM) subsystem discussed in my upcoming e-book, SAS® Server Pages: Generating Dynamic ContentPFM is a web-based data entry facility that I've implemented on a number of projects to support managing the parameter files that drive the application. The getSurrogateKey macro is used as part of the update process to generate an analog to an auto-number surrogate key (something that many databases support with different names/terminology, but that SAS data files do not).

%macro getSurrogateKey
      (data = /* name of the dataset being updated */
      ,key =  /* name of surrogate key variable */
      );
 %local newkey;
 %if &&&key = . or &&&key = %then
 %do; /* generate a new surrogate key value */
   %let rc = %sysfunc(dosubl(
         'proc sql noprint;
           select cats(sum(max(&key),1))
           into:newkey from &data;quit;'
             ));
   %put NOTE: &key value of &newkey generated.;
   &newkey
 %end; /* generate a new surrogate key value */
 %else &&&key;
%mend getSurrogateKey;

The macro is called unconditionally by the update process and if the macro variable, from the Data Entry page (a SAS Server Page), for the key is missing, a new row is being added and this requires a new key value. The DOSUBL function is used, via %SYSFUNC) to run the PROC SQL code to get the next auto-number key value. Since DOSUBL is used, the macro execution does not return the SQL code to the current SAS session's input stack. Executing the macro returns:
  • the new key value, &newkey, for a new row.
  • the current key value, &&&key, for an existing row.
One final note about this example: it takes advantage of functionality introduced in the M1 realease of 9.3. In the M0 release, macro variables created in the code called by DOSUB/DOSUBL are not available. Returning macro variables and their values was added with the M1 release.

3 comments:

  1. Thanks Don,

    This is wild stuff. Mike Rhoads had an great SGF paper last year on "Macro Function Sandwich" which used FCMP and Run_Macro to write function-style macros which execute data/proc steps.

    http://support.sas.com/resources/papers/proceedings12/004-2012.pdf

    Seems like DOSUBL lets you do similar, with less work (?). (I don't have 9.3M1 yet, so can't play)

    When I explored run_macro, I was disappointed to see that it wasn't thoroughly documented. The "inner" data step/proc step executes in it's own environment (sub-session?), and what gets inherited from/passed back to the main environment seemed to vary quite a bit.

    But this is certainly exciting stuff. Makes me wish I was going to SGF this year.

    --Q.

    ReplyDelete
  2. Quentin, I know you won't be at SGF. But I spoke to Rich L who is the developer for DOSUB/DOSUBL (as well as PROC STREAM) and he is presenting a paper on this at SGF:

    Submitting SAS® Code on the Side

    Date: Tue, Apr 30, 2013
    Time: 1:30 PM-1:50 PM
    Location: Room 2016

    So you might want to check this out later. He also said that WRT Mike Rhoads paper about this, he has been in touch with Mike and the above paper includes one of examples modified to use DOSUBL.

    ReplyDelete
  3. Excellent, that's exciting news. I'll look forward to reading the paper. Really useful stuff, which can greatly expand the utility of the macro language.
    Thanks again,
    -Q.

    ReplyDelete