BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chaitanya
Calcite | Level 5

%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.?

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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

10 REPLIES 10
UrvishShah
Fluorite | Level 6

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


Tom
Super User Tom
Super User

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? 

UrvishShah
Fluorite | Level 6

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

data_null__
Jade | Level 19

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;  
Ron_MacroMaven
Lapis Lazuli | Level 10

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

chaitanya
Calcite | Level 5

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

Ron_MacroMaven
Lapis Lazuli | Level 10

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

chaitanya
Calcite | Level 5

got your point..thanks for d clarification..

Ron_MacroMaven
Lapis Lazuli | Level 10

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

data_null__
Jade | Level 19

If you say so.

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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