Menu

Object Orientation in Macros

Anonymous
2014-01-06
2014-01-07
  • Anonymous

    Anonymous - 2014-01-06

    To any of the developers that have worked on SASUnit,

    I am fascinated by the way SASUnit appears to get object-orientated behaviour from macros but am having trouble understanding how the mechanism works.

    As an example, the macro "%_sasunit_getPgmDesc" defined in "_sasunit_getpgmdesc.sas" appears to return the description string to its parent function in a local macro variable named "r_desc".

    The following code found in "runsasunit.sas" uses the macro:
    /-- save description and start date and time of scenario --------------/
    %_sasunit_getPgmDesc (i_pgmfile=&&l_scnfile&i, r_desc=l_scndesc)

    The remainder of the code in the script seems to imply that the code above populates the macro variable "l_scndesc" with the latest description.

    a) Is my observation correct? Have you managed to return local macro variables from a macro?
    b) If so, please could you explain how the mechanism works?

    Regards,
    Ross

     
  • Anonymous

    Anonymous - 2014-01-07

    Hi Ross,

    it is possible with SAS to pass from macros back to the caller, as we did it here.
    The fact is that you create a local variable in your calling macro (here: l_scndesc). Now you pass the Name of the macro variable to the called macro.
    In the called macro you can then bind the value to te resolved macro variabel name like this:
    %LET &r_desc. = <New Value>;

    Strictly speaking this is no object orientation. It is simply call be reference.

    Regards
    Klaus

     

    Last edit: Andreas Mangold 2014-01-07
  • Andreas Mangold

    Andreas Mangold - 2014-01-07

    Here is a simple example. It is important that the macro variable being assigned exists in the scope of the calling program. So you have to define it there by assigning it a value (as I do in the example) or with a %local or %global statement. It does not need to be global, as even local variables will also be visible in nested macro calls.

    %macro square(input_value, output_macvar_name);
    %* Use the value of output_macvar_name as the name of the macro variable;
    %let &output_macvar_name=%eval(&input_value*&input_value);
    %mend square;
    
    %* Macro variable must be defined in a scope outside of the macro program;
    %let squareofeleven=;
    %* Assign the value of the macro variable;
    %square(input_value=11,output_macvar_name=squareofeleven)
    %* Write it to the log;
    %put squareofeleven=&squareofeleven;
    
     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.