Deleting global macro variables

From sasCommunity
Jump to: navigation, search

Q: How do I delete global macro variables?

A: use the %symdel statement

syntax:

  •  %symdel Mvar1;
  •  %symdel Mvar1 Mvar2 Mvar3;

This program removes macro variables with a specified prefix.

* name       : SymDel;
* description: delete global macro variables with named prefix;
* purpose    : provide house-cleaning tool for routines;

%Let SymDelList=;
PROC SQL noprint;
         create table Work.Mvars as
         select * from Dictionary.Macros
         where  upcase(Name) like "%upcase(&SymDelPrefix.)%"
           and  scope eq 'GLOBAL';
         select cat('%Put Note3: ',trim(Name),':&',Name)
         into  :SymDelPut separated by ';'
         from   Work.Mvars;
         select upcase(Name)
         into  :SymDelList separated by ' '
         from   Work.Mvars;
         quit;
Proc Delete data = Work.Mvars;
%Put Note2: SymDel of &SymDelList.;
&SymDelPut.;
%symdel SqlObs SqLoops SqlXobs SqlRc;
%symdel &SymDelList. SymDelList SymDelPrefix SymDelPut;
run;
%Let     DotSAS = ;
%Include Project(test-parms&DotSAS.);

%Let Test1 = x;
%Let Test2 = y;
%Let Xyz = 0;
%Let SymDelPrefix = test;
%Include  Project(SymDel&DotSAS.);

%Let SymDelPrefix = Sev;
%Include  Project(SymDel&DotSAS.);
%Put _global_;
45         %Let Test1 = x;
46         %Let Test2 = y;
47         %Let Xyz = 0;
48         %Let SymDelPrefix = test;
49         %Include  Project(SymDel&DotSAS.);
NOTE: %INCLUDE (level 1) file PROJECT(SymDel) is file C:\SASprojects\SmryEachVar\sas\symdel.sas.
50        +* name       : SymDel;
51        +* description: delete global macro variables with named prefix;
52        +* purpose    : provide house-cleaning tool for routines;
53        +
54        +%Let SymDelList=;
55        +PROC SQL noprint;
56        +         create table Work.Mvars as
57        +         select * from Dictionary.Macros
58        +         where  upcase(Name) like "%upcase(&SymDelPrefix.)%"
59        +           and  scope eq 'GLOBAL';
NOTE: Table WORK.MVARS created, with 2 rows and 4 columns.

60        +         select cat('%Put Note3: ',trim(Name),':&',Name)
61        +         into  :SymDelPut separated by ';'
62        +         from   Work.Mvars;
63        +         select upcase(Name)
64        +         into  :SymDelList separated by ' '
65        +         from   Work.Mvars;
66        +         quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      user cpu time       0.00 seconds
      system cpu time     0.01 seconds
      Memory                            141k    

67        +Proc Delete data = Work.Mvars;
68        +%Put Note2: SymDel of &SymDelList.;
Note2: SymDel of TEST1 TEST2
69        +&SymDelPut.;
Note3: TEST1:x
Note3: TEST2:y
70        +%symdel SqlObs SqLoops SqlXobs SqlRc;
71        +%symdel &SymDelList. SymDelList SymDelPrefix SymDelPut;
72        +run;

NOTE: Deleting WORK.MVARS (memtype=DATA).
NOTE: PROCEDURE DELETE used (Total process time):
      real time           0.01 seconds
      user cpu time       0.01 seconds
      system cpu time     0.00 seconds
      Memory                            13k
99         %Put _global_;
GLOBAL XYZ 0
GLOBAL DOTSAS 

Notes: used in the SmryEachVar A Data Review Suite.



An Alternative Approach

Data MVars ( Keep = Name ) ;
  Set SasHelp.VMacro ;
  Where Scope = 'GLOBAL' ;
Run ;

Data _Null_ ;
  Set MVars ;
  Call Symdel( Name ) ;
Run ;