Friday, February 17, 2012

Macro Functions/Commands

List of frequently used Macro Functions


_ALL_          describes all currently defined macro variables, regardless of scope.
This output includes user-defined global and local variables as well as automatic macro variables. Scopes are listed in the order of innermost to outermost.

_AUTOMATIC_          describes all automatic macro variables.
The scope is listed asAUTOMATIC. All automatic macro variables are global except SYSPBUFF.

_GLOBAL_ describes all global macro variables that were not created by the
macro processor. The scope is listed as GLOBAL. Automatic macro variables are not listed.

_LOCAL_ describes user-defined local macro variables defined within the
currently executing macro. The scope is listed as the name of the macro in which the macro variable is defined.

_USER_ describes all user-defined macro variables, regardless of scope.
The scope is either GLOBAL, for global macro variables, or the name of the macro in which the macro variable is defined.

Create Global macros by using the command %Global


Two rules control where CALL SYMPUT creates its variables:


  • CALL SYMPUT creates the macro variable in the current symbol table available while the DATA step is executing, provided that symbol table is not empty. If it is empty (contains no local macro variables), usually CALL SYMPUT creates the variable in the closest nonempty symbol table.
  • However, there are three cases where CALL SYMPUT creates the variable in the local symbol table, even if that symbol table is empty:
    • Beginning with SAS Version 8, if CALL SYMPUT is used after a PROC SQL, the variable will be created in a local symbol table.
    • If the macro variable SYSPBUFF is created at macro invocation time, the variable will be created in the local symbol table.
    • If the executing macro contains a computed %GOTO statement, the variable will be created in the local symbol table. A computed %GOTO statement is one that uses a label that contains an & or a % in it. That is, a computed %GOTO statement contains a macro variable reference or a macro call that produces a text expression. Here is an example of a computed %GOTO statement:
                                  %goto &home;



Table 6.1 Macro Language Elements that Evaluate Arithmetic and Logical
Expressions
%DOmacro-variable=expression %TO expression<%BY expression>;
%DO %UNTIL(expression);
%DO %WHILE(expression);
%EVAL (expression);
%IF expression %THEN statement;
%QSCAN(argument,expression<,delimiters>)
%QSUBSTR(argument,expression<,expression>)
%SCAN(argument,expression,)
%SUBSTR(argument,expression<,expression>)
%SYSEVALF(expression,conversion-type)


%SYSEVALF function to evaluate logical expressions containing floating-point or missing values.


The following macro quoting functions are most commonly used:

  1. %STR and %NRSTR
  2. %BQUOTE and %NRBQUOTE
  3. %SUPERQ





For the paired macro quoting functions, the function beginning with NR affects the same category of special characters that are masked by the plain macro quoting function as well as ampersands and percent signs. In effect, the NR functions prevent macro and macro variable resolution. To help you remember which does which, try associating the NR in the macro quoting function names with the words “not resolved”
— that is, macros and macro variables are not resolved when you use these functions. The macro quoting functions with B in their names are useful for macro quoting unmatched quotation marks and parentheses. To help you remember the B, try associating B with “by itself”.



Here is an example that uses %STR to mask a string that contains an unmatched single quotation mark. Note the use of the % sign before the quotation mark:

%let innocent=%str(I didn%’t do it!);


Examples Using %NRSTR
Suppose you want the name (not the value) of a macro variable to be printed by the %PUT statement. To do so, you must use the %NRSTR function to mask the & and prevent the resolution of the macro variable, as in the following example:


%macro example;
               %local myvar;
               %let myvar=abc;
               %put %nrstr(The string &myvar appears in log output,);
              %put instead of the variable value.;
%mend example;

%example
This code writes the following text to the SAS log:

The string &myvar appears in log output,
instead of the variable value.

If you did not use the %NRSTR function or if you used %STR, the following undesired output would appear in the SAS log:
The string abc appears in log output,
instead of the variable value.





No comments:

Post a Comment