BookmarkSubscribeRSS Feed
Siddharth123
Obsidian | Level 7

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


15 REPLIES 15
yaswanthj
Calcite | Level 5

Hi .

Macro parameters are always local to the macro that defines them. So that you already define the macro variable inside the macro..which means local


And you are passing the local macro parameter..So that you have a result of local macro variable value ..


Thanks,

Yash

Scott_Mitchell
Quartz | Level 8

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_;

yaswanthj
Calcite | Level 5

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.

Scott_Mitchell
Quartz | Level 8

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.

yaswanthj
Calcite | Level 5

thanks Scott.. i have updated the word in my previous reply..

kuridisanjeev
Quartz | Level 8

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

Astounding
PROC Star

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.

Tom
Super User Tom
Super User

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 ;

kuridisanjeev
Quartz | Level 8

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

data_null__
Jade | Level 19

CALL SYMPUT does not have a third argument.

Give macro1 a parameter and see what happens.

kuridisanjeev
Quartz | Level 8

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

Tom
Super User Tom
Super User

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;

data_null__
Jade | Level 19

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.

25         %macro2;
NOTE:
Line generated by the invoked macro "MACRO2".
25              Data _null_; Call symput("Mac1","Test","L"); Run;
                                 
______
                                 
253
MPRINT(MACRO2):   Data _null_;
MPRINT(MACRO2):   Call symput("Mac1","Test","L");
MPRINT(MACRO2):   Run;

ERROR 253-185: The SYMPUT subroutine call has too many arguments.

NOTE: The SAS System stopped processing this step because of
errors.
NOTE: DATA statement used (Total process time):
      real time          
0.00 seconds
      cpu time           
0.00 seconds
     

WARNING: Apparent symbolic reference MAC1 not
resolved.
26        
27         %Put &Mac1;
&Mac1
28        
Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 15 replies
  • 3636 views
  • 7 likes
  • 7 in conversation