BookmarkSubscribeRSS Feed
Siddhartha
Calcite | Level 5
If I want to delete local macro variable within a macro, what is the process?
Can any one explain me with an example.

Regards,
Sidhu
11 REPLIES 11
deleted_user
Not applicable
I'm not aware that you can delete a LOCAL macro variable. You can delete a global variable with %SYMDEL. Of course, you can always set the local variable to a null value, but you didn't say what your purpose is, so I don't know whether that would help or not.
PatrickG
SAS Employee
Alas, but you can't. %SYMDEL only works on global macro variables. If you need to create then delete a macro variable from within a macro, make it global by using the %GLOBAL statement, then you can delete it when done.
Peter_C
Rhodochrosite | Level 12
why would you need to delete the local macro variable?
Make the macro siimpler and lose the local mvars as it closes.
There are many ways to eliminate such a problem.

good luck
PeterC
Scott_C_Moore
Calcite | Level 5

****************Update on 7/2/2015 I was informed by tech support that this works in SAS version 9.3 as a result of a glitch - it does not work in SAS 9.4****************************************

I know this is an old thread but I found it unanswered and then figured out a solution.

%macro _localvaltest ();

/*setting up a local macro variable and putting it into a dataset*/

%let localvar=1;

data new;

     set sashelp.shoes;

     local=&localvar;

run;

/*creating a set that contains currently defined macro variables*/

data vars;

     set sashelp.vmacro;

run;

/*write local vars to the log*/

%put  _local_;

%put localvar value is &localvar;

/*executing a null step that deletes local variables from the macro table*/

data _null_;

     set vars;

          if scope='_LOCALVALTEST' then call execute ('%symdel '||trim(left(name))||';');

run;

/*write local vars to the log after the delete - does not show any and comes back unresolved for second  put*/
%put  _local_;

%put localvar value is &localvar;

/*creating a new set that shows the local macro variable has been deleted by the previous step*/

data vars2;

     set sashelp.vmacro;

run;

%mend;

%_localvaltest;

Message was edited by: Scott Moore; Updated to correct syntax error and add %put statemets.

Quentin
Super User

Hi Scott,

I don't think that's a solution.  I'm getting all kinds of errors, starting with the first comment which is missing a / at the end.  So as posted everything except the last data step is commented out.

It would be quite surprising if you could trick %SYMDEL into deleting a local variable.

If you play with this more and it still looks like you have deleted a local macro variable, I woud suggest:

1. ) add %put _local_ ;  before and after your deletion, to show the change

2. ) turn on options MPRINT and run the code in batch (so you know you have a clean session)

3. ) post the log from that session (showing both the macro definition and the execution.

Kind Regards,

--Q.


BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Scott_C_Moore
Calcite | Level 5

Couldn't figure out how to cut and paste from my SAS editor and ended up typing the reply, which is never a good idea. Fixed the problem and tested it's working properly.

data_null__
Jade | Level 19

If you have SAS/EG use Copy HTML Source to Clipboard then click HTML in "this" editor and past.

data_null__
Jade | Level 19

Do another test and make a global version of the local macro variable you are trying to delete.  Which one gets deleted?

Scott_C_Moore
Calcite | Level 5

If I create a global version of the same variable, the scope in the sashelp.vmacro is global and it no longer meets the condition of if scope= '_LOCALVALTEST' so it does not get deleted. I'll edit the program one more time to %put the value of the macro variable and it will come back as unresolved for the second %put statement showing that the value has been deleted.

My EG install is broken and getting IT to reinstall is such a hassle that I just use the enhanced editor.

DennisWilliams
Calcite | Level 5
let me know if this work, thnx

*create variable;
%let var = 1;

*display;
%put varibale var has been create and the value is &var;

*further proof;
data view_var_b4;
        set sashelp.vmacro;
run;
*create macro;
%macro del_var (del=);
         %symdel &del;
%mend del_var;

*run macro; 
%del_var (del=var);

*check variable table 
data view_var_after;
          set sashelp.vmacro;
run;

*further proof; 
%put _user_; 
SPR
Quartz | Level 8 SPR
Quartz | Level 8

What about simply saying;

%let mvar=;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 17196 views
  • 0 likes
  • 9 in conversation