DATA Step, Macro, Functions and more

how to delete a macro variable created inside a macro

Reply
Contributor
Posts: 60

how to delete a macro variable created inside a macro

Hi,

I am trying to delete the value stored inside a macro variable that I have created inside the following macro . the reason why want to delete the value stored inside adjust_session is because when I=1 , adjust_session get the value of 2, but when I=2 'adjust_session' stay with the same value as when I=1.

%macro ADJUST(NOBSI);

%local i;


%do I=1 %TO &NOBSI;
data _null_; /*rechange to data _NULL_*/
set list1 ;
if id=&i then do;
put new_session=;
call symput('adjust_session',new_session);
end;
stop;
run;
.......ETC
%symdel adjust_session y product;
%end;
%mend;
%adjust(&nobsi)

LOG

MLOGIC(ADJUST): %SYMDEL ADJUST_SESSION

WARNING: Attempt to delete macro variable ADJUST_SESSION failed. Variable not found.
Contributor
Posts: 21

Re: how to delete a macro variable created inside a macro

Posted in reply to sasuser1000
%SYMDEL is only to delete global variables.
No need to delete local macro variables, since they are removed automatically when macro ends.
Contributor
Posts: 60

Re: how to delete a macro variable created inside a macro

Posted in reply to ASASProgrammer
Hi,

so why the value of 'adjust_session' will not change when I=2 and takes the value of new_session as whenI=1. I know that I is not the problem since I change in my output from 1 to 2 , but adjust_session will have always the same value as when I=1???

%do I=1 %TO &NOBSI;
data _null_; /*rechange to data _NULL_*/
set list1 ;
if id=&i then do;
put new_session=;
call symput('adjust_session',new_session);
Regular Contributor
Posts: 165

Re: how to delete a macro variable created inside a macro

Posted in reply to sasuser1000
I would try either removing the stop statement or use point in the set statement(and get rid of the if statement). According to the documentation on the STOP statement:

The STOP statement causes SAS to stop processing the current DATA step immediately and resume processing statements after the end of the current DATA step.

It looks like the data step will never get past the first observation.
Contributor
Posts: 60

Re: how to delete a macro variable created inside a macro

RICKM THANK YOU SOOOO MUCH!
IT WORKED!
Super Contributor
Super Contributor
Posts: 365

Re: how to delete a macro variable created inside a macro

Posted in reply to sasuser1000
Hello SASUser1000,

I've made some small changes in your code and it works as expected (at least in my opinion):
[pre]
data list1;
do id=1 to 20;
new_session=id**2;
output;
end;
run;
%macro ADJUST(NOBSI);
%local i adjust_session;
%do I=1 %TO &NOBSI;
data _null_; /*rechange to data _NULL_*/
set list1 ;
if id=&i then do;
put new_session=;
call symputx('adjust_session',new_session);
end;
run;
%end;
%mend;
options mprint mlogic;
%adjust(2)
[/pre]
Sincerely,
SPR
Contributor
Posts: 60

Re: how to delete a macro variable created inside a macro

Thanks SPR, I will check this.
Ask a Question
Discussion stats
  • 6 replies
  • 732 views
  • 0 likes
  • 4 in conversation