BookmarkSubscribeRSS Feed
sanvi
Calcite | Level 5
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;
3 REPLIES 3
Doc_Duke
Rhodochrosite | Level 12
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..;
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
ArtC
Rhodochrosite | Level 12
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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
  • 3 replies
  • 711 views
  • 0 likes
  • 4 in conversation