DATA Step, Macro, Functions and more

Error while resolving the macro variable

Reply
N/A
Posts: 1

Error while resolving the macro variable

Hi,

I am getting the below error when executing my sas program when I am trying to make the macro variable T01_UN_KEY1 to Global

Error: Attempt to %GLOBAL a name (T01_UN_KEY1) which exists in a local environment

So I changed my code something like below

%DO I=1 %TO &KEY_STRING_CNT.;
%GLOBAL G_T01_UN_KEY&I;
%LET T01_UN_KEY&I = && G_T01_UN_KEY&I..;
%END;

Now I am not getting the error. But the values in dataset for the variable KEYWORD1 are not correct. It just gives commas without values when it is supposed to have values separated by commas.

Could you please resolve my problem. It is very urgent.

Below is the program for your reference.

%let T01_ADDKEY = TEST,MISC,OTHER;

DATA UNUS_KEY;
KEY=SYMGET("T01_ADDKEY");
KEY=UPCASE(KEY);
count= count(KEY,',');
COUNT_NEW=COUNT+1;
do i = 1 to count +1;
KEY1 = trim(left(scan(KEY,i,',')));
OUTPUT;
end;
RUN;
DATA UNUS_KEY1;
SET UNUS_KEY;
CALL SYMPUT(COMPRESS('T01_UN_KEY'||_N_),TRIM(LEFT(KEY1)));
RUN;
%LET KEY=%SYSFUNC(OPEN(SASDATA.UNUS_KEY1));
%LET KEY_STRING_CNT=%SYSFUNC(ATTRN(&KEY,NOBS));
%LET RC=%SYSFUNC(CLOSE(&KEY));

DATA SASDATA.UNUSUAL_TEMP;
SET SASDATA.&CLIENT._NOEX;
LENGTH KEYWORD1 $1000. TEMP $1. ;
%DO I=1 %TO &KEY_STRING_CNT.;
%GLOBAL G_T01_UN_KEY&I;
%LET T01_UN_KEY&I = && G_T01_UN_KEY&I..;
%END;


%IF &KEY_STRING_CNT. > 0 %THEN %DO;
%DO I =1 %TO &KEY_STRING_CNT.;
IF INDEX(UPCASE(ACCT_DESCR),"&&T01_UN_KEY&I.") > 0 THEN DO;

KEYWORD1 = TRIM(LEFT(KEYWORD1)) || TEMP || %STR("&&T01_UN_KEY&I.");
TEMP=",";
END;
%END;

OUTPUT SASDATA.UNUSUAL_TEMP;

%END;

RUN;
Trusted Advisor
Posts: 2,113

Re: Error while resolving the macro variable

I don't know that I've found all of your errors, but you have a space between the "&&" and the "G" in this statement.
%LET T01_UN_KEY&I = && G_T01_UN_KEY&I..;
Super Contributor
Super Contributor
Posts: 3,174

Re: Error while resolving the macro variable

You will want to read up on SAS MACRO variables and the topic of "scope", that being global versus local -- particularly how a DATA step invocation of CALL SYMPUT plays into the situation.

Scott Barry
SBBWorks, Inc.


Recommended Google advanced search argument, this topic/post:

macro variable global local data step site:sas.com
Valued Guide
Posts: 632

Re: Error while resolving the macro variable

Given Doc&Duke's correction. There are still a couple of things that I do not understand about your program. I suspect that there is a misunderstanding of the use of the macro language vs the DATA step.

Notice that you are attempting to place a series of macro variables in the global symbol table, but you have not given them a value. If they were loaded somewhere else (outside of the code piece that we were shown), then there is no need to use the %GLOBAL statement.
Ask a Question
Discussion stats
  • 3 replies
  • 110 views
  • 0 likes
  • 4 in conversation