13 users online | 13 Guests and 0 Registered

Is there an easy way to delete macro programme definitions and user-defined macro variables?

There is always a potential for cross-contamination when running code from different programmes in the same session resulting in unexpected ERRORs / WARNINGs, or outcomes resulting from previously-defined macro programmes, or macro variables.

It is beneficial to 'flag' macro programmes and variables into identifiable groups in order that they can then be selected for deletion. e.g.:

%macro date_pc1 ;
  data _null_ ;
    call symputx('today_pc1' ,put(today(),date9.   ),'g') ;
	call symputx('dmy_pc1'   ,put(today(),ddmmyyn8.),'g') ;
	call symputx('dow_pc1'   ,put(today(),downame. ),'g') ;
	call symputx('daynum_pc1',put(today(),weekday. ),'g') ;
  run ;
  %put &=today_pc1  ;
  %put &=dmy_pc1    ;
  %put &=dow_pc1    ;
  %put &=daynum_pc1 ;
%mend date_pc1 ;

Where there ar multiple such definitions, they can be cleared down using %sysmacdelete and %symdel statements.  The syntax for each:

%sysmacdelete macro_name ; /*** No % trigger on the macro name ***/
%symdel macro_vbl <mac_vbl2 ... mac_vbln> ;

When there are multiple programmes, and macro variables, all of the 'flagged' items can easily be identified and deleted:

proc catalog catalog = work.sasmacr  ;
  contents out = compmacs (keep  = name 
                           where = (name ? 'PC1')
                          ) ;
quit ;

data _null_ ;
  set compmacs ;
  call execute('%sysmacdelete ' !! name !! ' / nowarn ;') ;
run ;

*** Delete macro variables *** ;
proc sql noprint ;
  select distinct name into :macvardel separated by ' '
  from dictionary.macros
  where scope = 'GLOBAL'
    and name  ? 'PC1'
quit ;

%symdel &macvardel / nowarn ;
%symdel macvardel  / nowarn ;

The call execute instruction builds a %sysmacdelete statement to be executed at the step boundary (run;).  On each iteration the name of the macro is inserted from the table built from the macro catalog.

Although macro programme calls and macro variable references are case-insensitive, the system stores the values in upper-case, so the 'flag' (e.g. 'PC1') should be matched thus.

The code attached includes a utility macro which will clear-down the working environemnt, along with any datasets in the WORK library.

Attached files: clear-down.txt

Alan D Rudland
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category