macro help: reference %global macro vars

macro help: reference %global macro vars

Post by Fehd, Ronald » Tue, 03 Jul 2001 23:12:17




> How can I reference GLOBAL macro x within macro NEW?
> %global x; %*this is unnecessary in open code;

74   %macro newY(Y=);
75   %PUT LOCAL: ;
76   %PUT _local_;
77   %put GLOBAL X: &x;/* THIS IS TO REFERENCE LOCAL MACRO VARIABLE X*/
78   %LOCAL X;
79   %LET X = &Y.;
80   %PUT _local_;
81
82   %global X;
83   %put global or local x: &X.;
84   %PUT _local_;
85   %mend;
86
87   %newy(Y=2);
LOCAL:
NEWY Y 2
GLOBAL X: 1
NEWY X 2
NEWY Y 2
global or local x: 2
NEWY X 2
NEWY Y 2
88   %put global: &x;/* THIS IS TO REFERENCE LOCAL MACRO VARIABLE X*/
global: 1

You are not going to be able to declare X in a %local symbol table
-- by having it in the parameter list --
before you try to access %global X.

This concept is known as masking:
You can access %global X until you mask it with a %local declaration.
Your declaration was in the parameter list of macro NEW.
My example %put the %global X, then declared X %local and %put the local
value. You will not be able to switch back to accessing the %global X.

As always the important question is: Why do you want to do this?

This is major pain in the program for new macro users:
keeping track of which macro var they are using.

It often shows up as a major Can't-figure-this-out
when using the same loop index -- I -- in a calling and a called macro.

14   %macro CALLING(A);
15   %DO I = 1 %TO 3;%PUT CALLING MSG: &I.;%CALLED(&I);%END;
16   %PUT _LOCAL_;%MEND;
17   %macro CALLED(B);
18   %DO I = 1 %TO 3;%PUT CALLED MSG: &I.;%END;%MEND;
19   %CALLING(1);
CALLING MSG: 1
CALLED MSG: 1
CALLED MSG: 2
CALLED MSG: 3
CALLING I 5
CALLING A 1

mac-var I is %local to CALLING
macro CALLED resets it and increments it thru its loop,
and on exit, back to CALLING I fails the CALLING loop value and exits.
To fix this, add a
%local I;
to macro CALLED.


OpSys: WinNT   Ver: 8.1
---> cheerful provider of TESTED SAS code!*! <---
By using your intelligence you can sometimes make your problems twice as
complicated.
-- Ashleigh Brilliant

 
 
 

macro help: reference %global macro vars

Post by Fehd, Ronald » Wed, 04 Jul 2001 04:22:49



> This is a * version 8 bug.
> You cannot global a local variable.

I agree.
However, you can 'copy' a local mac-var value to a global mac-var.

Quote:> In version 6 an error was reported in any attempt to do so.

yes, my memory of error messages supports that.

Quote:> In running your
> code under version 8 there are no error messages!  Line 82,
> "%global X;", is
> simply ignored.  I would very much prefer to leave it as an
> error since it
> is an error and the programmer should be made aware of his mistake.

I was chagrined to _not_ see any error message on this
%global X;
statement.  The %put supported my assumption
-- listing the %local value of X --
that once mac-var X had been added to the %local symbol table
there would be no way to access the %global variable.
except, perhaps, thru work-arounds, as provided by other posters
accessing the system tables.

Quote:> Who asked for this behavior.  Is it a mistake by accident or design?

ill-informed bxor undereducated, I'm sure.

Little birdies will have to rescue themselves from this one.


> -----Original Message-----

> Sent: Monday, July 02, 2001 10:12 AM

> Subject: Re: macro help: reference %global macro vars


> > How can I reference GLOBAL macro x within macro NEW?

> > %global x; %*this is unnecessary in open code;

> 74   %macro newY(Y=);
> 75   %PUT LOCAL: ;
> 76   %PUT _local_;
> 77   %put GLOBAL X: &x;/* THIS IS TO REFERENCE LOCAL MACRO
> VARIABLE X*/
> 78   %LOCAL X;
> 79   %LET X = &Y.;
> 80   %PUT _local_;
> 81
> 82   %global X;
> 83   %put global or local x: &X.;
> 84   %PUT _local_;
> 85   %mend;
> 86
> 87   %newy(Y=2);
> LOCAL:
> NEWY Y 2
> GLOBAL X: 1
> NEWY X 2
> NEWY Y 2
> global or local x: 2
> NEWY X 2
> NEWY Y 2
> 88   %put global: &x;/* THIS IS TO REFERENCE LOCAL MACRO VARIABLE X*/
> global: 1

> You are not going to be able to declare X in a %local symbol table
> -- by having it in the parameter list --
> before you try to access %global X.

> This concept is known as masking:
> You can access %global X until you mask it with a %local declaration.
> Your declaration was in the parameter list of macro NEW.
> My example %put the %global X, then declared X %local and
> %put the local
> value. You will not be able to switch back to accessing the %global X.

> As always the important question is: Why do you want to do this?

> This is major pain in the program for new macro users:
> keeping track of which macro var they are using.

> It often shows up as a major Can't-figure-this-out
> when using the same loop index -- I -- in a calling and a
> called macro.

> 14   %macro CALLING(A);
> 15   %DO I = 1 %TO 3;%PUT CALLING MSG: &I.;%CALLED(&I);%END;
> 16   %PUT _LOCAL_;%MEND;
> 17   %macro CALLED(B);
> 18   %DO I = 1 %TO 3;%PUT CALLED MSG: &I.;%END;%MEND;
> 19   %CALLING(1);
> CALLING MSG: 1
> CALLED MSG: 1
> CALLED MSG: 2
> CALLED MSG: 3
> CALLING I 5
> CALLING A 1

> mac-var I is %local to CALLING
> macro CALLED resets it and increments it thru its loop,
> and on exit, back to CALLING I fails the CALLING loop value and exits.
> To fix this, add a
> %local I;
> to macro CALLED.


> OpSys: WinNT   Ver: 8.1
> ---> cheerful provider of TESTED SAS code!*! <---
> By using your intelligence you can sometimes make your
> problems twice as
> complicated.
> -- Ashleigh Brilliant


 
 
 

macro help: reference %global macro vars

Post by Ian Whitlo » Wed, 04 Jul 2001 04:14:14


Ron,

This is a * version 8 bug.  You cannot global a local variable.  In
version 6 an error was reported in any attempt to do so.  In running your
code under version 8 there are no error messages!  Line 82, "%global X;", is
simply ignored.  I would very much prefer to leave it as an error since it
is an error and the programmer should be made aware of his mistake.

Who asked for this behavior.  Is it a mistake by accident or design?


-----Original Message-----

Sent: Monday, July 02, 2001 10:12 AM

Subject: Re: macro help: reference %global macro vars


> How can I reference GLOBAL macro x within macro NEW?
> %global x; %*this is unnecessary in open code;

74   %macro newY(Y=);
75   %PUT LOCAL: ;
76   %PUT _local_;
77   %put GLOBAL X: &x;/* THIS IS TO REFERENCE LOCAL MACRO VARIABLE X*/
78   %LOCAL X;
79   %LET X = &Y.;
80   %PUT _local_;
81
82   %global X;
83   %put global or local x: &X.;
84   %PUT _local_;
85   %mend;
86
87   %newy(Y=2);
LOCAL:
NEWY Y 2
GLOBAL X: 1
NEWY X 2
NEWY Y 2
global or local x: 2
NEWY X 2
NEWY Y 2
88   %put global: &x;/* THIS IS TO REFERENCE LOCAL MACRO VARIABLE X*/
global: 1

You are not going to be able to declare X in a %local symbol table
-- by having it in the parameter list --
before you try to access %global X.

This concept is known as masking:
You can access %global X until you mask it with a %local declaration.
Your declaration was in the parameter list of macro NEW.
My example %put the %global X, then declared X %local and %put the local
value. You will not be able to switch back to accessing the %global X.

As always the important question is: Why do you want to do this?

This is major pain in the program for new macro users:
keeping track of which macro var they are using.

It often shows up as a major Can't-figure-this-out
when using the same loop index -- I -- in a calling and a called macro.

14   %macro CALLING(A);
15   %DO I = 1 %TO 3;%PUT CALLING MSG: &I.;%CALLED(&I);%END;
16   %PUT _LOCAL_;%MEND;
17   %macro CALLED(B);
18   %DO I = 1 %TO 3;%PUT CALLED MSG: &I.;%END;%MEND;
19   %CALLING(1);
CALLING MSG: 1
CALLED MSG: 1
CALLED MSG: 2
CALLED MSG: 3
CALLING I 5
CALLING A 1

mac-var I is %local to CALLING
macro CALLED resets it and increments it thru its loop,
and on exit, back to CALLING I fails the CALLING loop value and exits.
To fix this, add a
%local I;
to macro CALLED.


OpSys: WinNT   Ver: 8.1
---> cheerful provider of TESTED SAS code!*! <---
By using your intelligence you can sometimes make your problems twice as
complicated.
-- Ashleigh Brilliant