Help using Base SAS procedures

global macro

Reply
Contributor
Posts: 50

global macro

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


Contributor
Posts: 70

Re: global macro

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

Super Contributor
Posts: 297

Re: global macro

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

Contributor
Posts: 70

Re: global macro

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.

Super Contributor
Posts: 297

Re: global macro

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.

Contributor
Posts: 70

Re: global macro

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

Super Contributor
Posts: 276

Re: global macro

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

Super User
Posts: 5,081

Re: global macro

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.

Super User
Super User
Posts: 6,499

Re: global macro

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 ;

Super Contributor
Posts: 276

Re: global macro

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

Respected Advisor
Posts: 3,777

Re: global macro

CALL SYMPUT does not have a third argument.

Give macro1 a parameter and see what happens.

Super Contributor
Posts: 276

Re: global macro

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

Super User
Super User
Posts: 6,499

Re: global macro

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;

Respected Advisor
Posts: 3,777

Re: global macro

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        
Super User
Super User
Posts: 6,499

Re: global macro

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.

Ask a Question
Discussion stats
  • 15 replies
  • 929 views
  • 6 likes
  • 7 in conversation