BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7
Hi all,

I have the below portion of the code which works perfectly fine but when I put the whole code inside another macro it fails. The section of the code looks like this.


%macro check_duplicate;
%global dup_list;
if &dup_list= %then %do;
%let dup_list=‘99999’;
%end;
%mend check_duplicate;

%check_duplicate;

This above section was working fine on my code but then I had to make some changes and put the whole code inside another macro and I am getting this error.

ERROR: Attempt to %GLOBAL a name (DUP_LIST) which exists in a local environment.

How can I handle this error without making much changes to the structure of the code.

Any suggestions or ideas will be highly appreciated.

Thanks in advance.

Nick

6 REPLIES 6
Astounding
PROC Star

The problem is with the "other" macro that calls this macro.

 

That other macro is creating a local macro variable named DUP_LIST.  Change the other macro so that it creates DUP_LIST as a global macro variable.

nickspencer
Obsidian | Level 7
@Astounding I don’t think I understand when you say change other macro to create dup_list. Do you mean having the %global dup_list outside this check duplicate macro?
Astounding
PROC Star

That's an easy way.

 

The variable is somehow getting created by the "other" macro as well.  So putting the %global statement outside of all macros should take care of the problem.  Alternatively, you could search for other code that also creates DUP_LIST.  But stick with the easy way if that sounds appropriate (add the %GLOBAL statement before running any macros).

 

Reeza
Super User

@nickspencer wrote:



This above section was working fine on my code but then I had to make some changes and put the whole code inside another macro and I am getting this error.

Are you saying you embedded your macro definitions? That's never a good idea.

 

%macro firstMacro ();

....


%macro secondMacro();
....

%mend secondMacro;

....
%mend firstMacro;

Tom
Super User Tom
Super User

You cannot use %GLOBAL to define a macro variable in the GLOBAL scope if there is already a macro variable in another (local) scope with that same name.

 

Why do you want to make that macro variable as global to begin with?  Is so the macro can return a value to the caller?  Then just run the %GLOBAL statement when it is needed.

%if not %symexist(dup_list) %then %global dup_list;

If you are just using the macro variable as INPUT to the macro and it doesn't exist then just make a local macro variable instead. No use cluttering the global scope with unneeded macro variables, that is how you got into this trouble to begin with.

%if not %symexist(dup_list) %then %local dup_list;
ballardw
Super User

Here's an idea: Provide the entire code of both macros.

 

That will help us avoid a lot of "if you did this then …" guesses.

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1223 views
  • 2 likes
  • 5 in conversation