DATA Step, Macro, Functions and more

Deleting local macro variable within a macro

Reply
Contributor
Posts: 69

Deleting local macro variable within a macro

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
N/A
Posts: 0

Re: Deleting local macro variable within a macro

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.
SAS Employee
Posts: 58

Re: Deleting local macro variable within a macro

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.
Valued Guide
Posts: 2,174

Re: Deleting local macro variable within a macro

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
Occasional Contributor
Posts: 12

Re: Deleting local macro variable within a macro

****************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.

PROC Star
Posts: 1,227

Re: Deleting local macro variable within a macro

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.


Occasional Contributor
Posts: 12

Re: Deleting local macro variable within a macro

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.

Respected Advisor
Posts: 3,777

Re: Deleting local macro variable within a macro

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

Respected Advisor
Posts: 3,777

Re: Deleting local macro variable within a macro

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

Occasional Contributor
Posts: 12

Re: Deleting local macro variable within a macro

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.

Senior User
Posts: 1

Re: Deleting local macro variable within a macro

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_; 
Ask a Question
Discussion stats
  • 10 replies
  • 5996 views
  • 0 likes
  • 8 in conversation