Hi All,
I am executing:
%macro one (input);
%two;
%put the value is &date;
%mend;
%macro two;
data _null_;
call symput('date','12SEP2008');
run
%mend;
%let date=31DEC2006;
%one(&date);
%put _user_;
I am unsure why in Global Symbol Table, Date is stored as the one that is local ''12SEP2008', I would expect that Date is stored as 31DEC2006 in the Global Symbol Table.
Kind Regards
SK
You can make a macro variable global using the SYPUTX function as below.
%MACRO ONE (INPUT);
%TWO;
%PUT THE VALUE IS &DATE;
%MEND;
%MACRO TWO;
DATA _NULL_;
CALL SYMPUTX('DATE','12SEP2008','G');
RUN
%MEND;
%LET DATE=31DEC2006;
%ONE(&DATE);
%PUT _USER_;
I think you get same result. what ever mentioned in the above..
If you can try like below you get your result..declaring the second date locally..
%macro one (input);
%two;
%put the value is &date;
%mend;
%macro two;
data _null_;
call symput('date','12SEP2008');
run
%mend;
%macro two;
%let date=31DEC2006;
%mend;
%one(&date);
%put _user_;
Thanks,
Yash.
I wasn't addressing the OP's question, I was addressing the fact that you stated you can not make macro variables global, which is incorrect.
thanks Scott.. i have updated the word in my previous reply..
Hello Siddharth,
Default 3rd argument for Call symput is "G".So that is the reason even if you create a macro variable with in the macro by using call symput,that will become Global macro variable.
So you need to write like this .
%macro one (input);
%two;
%put the value is &date;
%mend;
%macro two;
data _null_;
call symput('date','12SEP2008','L');
run
%mend;
%let date=31DEC2006;
%one(&date);
%put _user_;
Hope this make sense.
Best Regards,
Sanjeev.K
CALL SYMPUT does not always create a new macro variable. First, it looks to see if it can find an existing macro variable named DATE. If it can find one (and it does in the GLOBAL symbol table), that's the one that it changes. The same is true for many statements: %LET, %DO for example.
If you define a new macro variable inside of a macro using %LET or CALL SYMPUT then it will be local to the macro and disappear when the macro finishes. But if the macro variable already exists then both %LET and CALL SYMPUT will store the new value into the existing macro variable rather than making a new one.
So you could fix your example by defining the macro variable before making the inner macro call.
%macro one (input);
%let date=;
%two;
%put the value is &date;
%mend;
Alternatively you could create the macro variable in the GLOBAL symbol table by using %GLOBAL or the 'G' modifier on CALL SYMPUTX() function call. But if that macro variable already exists in some other macro scope that is active the %GLOBAL statement will cause an error. The CALL SYMPUTX() would work to modify the global value, but the existence of a similarly named macro variable in a local scope would hide the value.
In general it is better to define the macro variables that you want subroutine macros to be able to modify before calling the macro. If you want the subroutine to create a GLOBAL macro variable then use the %GLOBAL statement. Preferably conditionally on a test for the existence of the macro variable already.
%if not %symexist(date) %then %global date ;
Dear Tom,
Just a Quick Question,
As per your words,
"If you define a new macro variable inside of a macro using %LET or CALL SYMPUT then it will be local to the macro and disappear when the macro finishes. "
But I tried with Bellow Code,
%Macro Macro1;
Data _null_;
Call symput("Mac","Test");
Run;
%mend;
%macro1;
%Put &Mac;
%put _global_;
Still Mac listed in Global variables list and even that macro variable resolving outside of macro.
So can i Support my previous comments "Default 3rd argument for Call symput is "G".So that is the reason even if you create a macro variable with in the macro by using call symput,that will become Global macro variable." ???
Best Regards,
Sanjeev.K
CALL SYMPUT does not have a third argument.
Give macro1 a parameter and see what happens.
Hi,,
I tried with the same code ,but passed third parameter.(As i said default parameter is "G").
%Macro Macro2;
Data _null_;
Call symput("Mac1","Test","L");
Run;
%mend;
%macro2;
%Put &Mac1;
%put _global_;
Now Mac1 not resolving outside of macro and it was not listed in Global macro variable list as well.
Confused !!!!!!!!!!!!!!!!!!!!!.
am I doing any thing wrong ???
Regards,
Sanjeev.K
Try these four different tests. Notice that in the last one there is a macro variable Y defined for the local symbol table before the CALL SYMPUTX() runs.
%symdel xxx;
%macro x;
data _null_;
call symputx('xxx','set by call symputx','l');
run;
%mend x;
%x;
%put &xxx;
%symdel xxx;
%macro x;
data _null_;
call symputx('xxx','set by call symputx','g');
run;
%mend x;
%x;
%put &xxx;
%symdel xxx;
%macro x;
data _null_;
call symputx('xxx','set by call symputx');
run;
%mend x;
%x;
%put &xxx;
%symdel xxx;
%macro x;
%let y=1;
data _null_;
call symputx('xxx','set by call symputx');
run;
%mend x;
%x;
%put &xxx;
kuridisanjeev wrote:
Hi,,
I tried with the same code ,but passed third parameter.(As i said default parameter is "G").
%Macro Macro2;
Data _null_;
Call symput("Mac1","Test","L");
Run;
%mend;
%macro2;
%Put &Mac1;
%put _global_;
Now Mac1 not resolving outside of macro and it was not listed in Global macro variable list as well.
Confused !!!!!!!!!!!!!!!!!!!!!.
am I doing any thing wrong ???
Regards,
Sanjeev.K
I don't think you are looking at the SASLOG.
That is an example a the strange behavior of CALL SYMPUT when used inside of a macro where the local symbol table is empty. In that special case CALL SYMPUT will create the macro variable in the GLOBAL symbol table.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.