Wednesday, April 18, 2012

SAS Macros Quick and useful TIP

HI Folks,

I have a quick tips for you on macros. Most of the code should be self explanatory, but do post comments if you need yelp/suggestion !

Scenario : you are testing some code in Windows SAS session using macros ! need help to see what all macro variables you had created and you wana clear them all (ofcourse just close the session and restart like an dork !)

use:
%put _all_; /* To see all kinds of macro variables that exist in session */

%put _global_ ; /* To only global macro variables that exist in session */
%put _local_ ; /* for local variables , if any */

but that was not tricky, but indeeed helpful.

use the macro ! to clear all global macro variables in a session (this will also clear all the automatic global variables but nothing important ):


COde
%let mymac1=Krypton;
%put &mymac1;
%macro delvars;
data vars;
set sashelp.vmacro;
run;
data _null_;
set vars;
if scope='GLOBAL' then
call execute('%symdel '||trim(left(name))||';');
run;
%mend;
%delvars;
%put &mymac1;



Thanks,
Kr !!

3 comments:

  1. You do not need the 1st data step.

    %let mymac1=Krypton;
    %put &mymac1;
    %macro delvars;
    data _null_;
    set sashelp.vmacro (where=(scope='GLOBAL'));
    call execute('%symdel '||trim(left(name))||';');
    run;
    %mend;
    %delvars;
    %put &mymac1;

    ReplyDelete
  2. Rightly pointed out Charles, do you have some blog too !!

    ReplyDelete
  3. Here is a macro program I use to delete any (non-SAS-defined) global macros. Not much commenting, but hopefully fairly self-explanatory.

    Help yourself..

    %macro MySymDelAll(Display=N);
    %if x&SYSPROCNAME ne x %then %put ERROR: Cannot run within a DATA or PROC step boundary;
    %else %do;
    %local List Count;
    proc sql noprint;
    select name into :List separated by " " from dictionary.macros
    where scope = "GLOBAL"
    /* Do not delete some SAS-defined GLOBAL macros */
    and substr(name,1,3) not in ("_EG", "SYS", "_CL", "SQL", "_SA", "SAS");
    %let Count=&SQLOBS;
    quit;
    %if &Display ne N %then %put List(&Count): &List;
    %if Count gt 0 %then %symdel &List;
    %end;
    %mend;

    %let ABC=123;
    %MySymDelAll();
    %put _USER_;

    ReplyDelete