DATA Step, Macro, Functions and more

can we use do loop to delete global macro variables?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

can we use do loop to delete global macro variables?

%macro xy;

      %array &trt(60) ;

          %do i = 1 %to 60;

               %symdel trt(i) ;

          %end;

%mend xy;

%xy;macro

WARNING: Apparent invocation of macro ARRAY not resolved.

WARNING: Apparent symbolic reference TRT not resolved

i have created trt1 to trt60  macro variables , i want to delete them all at once . can anybody suggest me the way and what mistake i did  in this program.?


Accepted Solutions
Solution
‎04-05-2013 07:40 AM
Respected Advisor
Posts: 3,777

Re: can we use do loop to delete global macro variables?

This doesn't require a macro.

%global trt1 trt2 trt3;

data todelete;
   set sashelp.vmacro;
   where scope eq 'GLOBAL' and name eq: 'TRT' and offset eq 0;
  
run;
  
run;
data _null_;
  
set todelete;
   call symdel(name);
   run;  

View solution in original post


All Replies
Regular Contributor
Posts: 195

Re: can we use do loop to delete global macro variables?

Hi,

I think it quite simple by using %DO Loop...

%macro delete_var;

      %do i = 1 %to 60;

              %global trt&i.;

               %let trt&i = 1;

               %symdel trt&i.;

      %end;

%mend;

%delete_var


Super User
Super User
Posts: 6,500

Re: can we use do loop to delete global macro variables?

There is no macro equivalent of the data step ARRAY statement.

If you have a numbered series of macro variables that you want to delete then just use a %DO loop.

%do i=1 %to 60;

  %if %symexist(trt&i) %then %symdel trt&i ;

%end;


Why do you feel a need to delete the macro variables? 

Regular Contributor
Posts: 195

Re: can we use do loop to delete global macro variables?

Hi Tom,

I think Chaitanya is trying to make his code more efficient by deleting global macro variables as they are not needed for further processing...

Chaitanya -  if your aim is to make your programme more efficient then, rather than delete macro variables, you can assign the macro variables a NULL value like this %let trt1 = ;

By this way, you can save the space that is needed to store global macro variables in global symbol table...

-Urvish

Solution
‎04-05-2013 07:40 AM
Respected Advisor
Posts: 3,777

Re: can we use do loop to delete global macro variables?

This doesn't require a macro.

%global trt1 trt2 trt3;

data todelete;
   set sashelp.vmacro;
   where scope eq 'GLOBAL' and name eq: 'TRT' and offset eq 0;
  
run;
  
run;
data _null_;
  
set todelete;
   call symdel(name);
   run;  
Regular Contributor
Posts: 198

Re: can we use do loop to delete global macro variables?

I agree with Data_Null_:

a macro is not required, just a subroutine.

Save that solution in a utility file

and whenever you want to do housecleaning

just

%include SiteIncl(symdel_all_global);

here are some points for future reference:

* symdel works on global macro variables

* opinion: it is Very BAD programming practice to write a macro that removes mvars from the global symbol table

* macro array?

I have written three versions:

http://www.sascommunity.org/wiki/Category:Macros_by_Ron_Fehd

which I have deprecated in favor of either of

http://www.sascommunity.org/wiki/Macro_CallMacr

or

http://www.sascommunity.org/wiki/Macro_CallText

or

http://www.sascommunity.org/wiki/Macro_Loops_with_Dates

reference:

http://www.sascommunity.org/wiki/List_Processing_Basics_Creating_and_Using_Lists_of_Macro_Variables

Ron Fehd  deprecated(%array) maven

Occasional Contributor
Posts: 7

Re: can we use do loop to delete global macro variables?

%do loop is also deleting all the macro variables , is there any rationale in using subroutine?

Regular Contributor
Posts: 198

Re: can we use do loop to delete global macro variables?

My reframe of your Q is:

I have to write a macro whose only purpose is to do one thing, once.

and my answer is:

Macros are all about repetition.

So, yes, your macro does have one of my two criteria for writing a macro

1. %do

2. %if

but saving and finding a reusable subroutine

is as much work as saving and finding a non-reusable macro

so my vote is for the reusable subroutine.

And,

why are you creating an array of global macro variables in the first place?

They can be %local within the macro that creates and uses them

and are discarded when the macro finishes.

The context of your Q is

-- in my opinion --

not good programming practice.

Ron Fehd  better==less maven

Occasional Contributor
Posts: 7

Re: can we use do loop to delete global macro variables?

got your point..thanks for d clarification..

Regular Contributor
Posts: 198

Re: can we use do loop to delete global macro variables?

Here is a one.step solution:

http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0605D&L=sas-l&P=R4163

Ron Fehd  SAS-L archives maven

Respected Advisor
Posts: 3,777

Re: can we use do loop to delete global macro variables?

If you say so.

Seems more complex to create more macro variables just so you can say it is one step which its not.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 469 views
  • 8 likes
  • 5 in conversation