BookmarkSubscribeRSS Feed
mhodge20
Calcite | Level 5

I am creating macro variables inside of a do loop using the proc sql's into:, I understand that because it is inside a macro function that they are local. How do I transform to global? %global statement doesnt seem to be working.

 

This is the error

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

 

%macro num;
	proc sql;
		create table list1 as
		select distinct customerid from payments;
	quit;

   	%do i = 1 %to 6;
	
   		proc sql noprint;
   			select customerid into: ids&i separated by ' '
   			from list1
				where monotonic() between ((6553*&i)-6552) and (6553*&i); 
	
   		quit;
   %global ids&i;
%end;
%mend num; %num;
2 REPLIES 2
ballardw
Super User

Did you try:

%macro num;
	proc sql;
		create table list1 as
		select distinct customerid from payments;
	quit;

   	%do i = 1 %to 6;
   %global ids&i;
	
   		proc sql noprint;
   			select customerid into: ids&i separated by ' '
   			from list1
				where monotonic() between ((6553*&i)-6552) and (6553*&i); 
	
   		quit;
                %end;
   %mend num;
   %num;
Reeza
Super User

Looks like you're creating a series of macro variables based on the number of observations?

 

It may be worthwhile to consider a data step solution instead, where you don't really need a macro at all and can use CALL SYMPUTX to create the macro variable. 

 

data want;
set have;
length value_list $32562.;

if _n_ =1 then i=1;

value_list = catx(" ", trim(value_list), id);

if _N_ >= i*6534 then do;
call symputx ('myvar'||i, value_list, 'g');
i+1;
call missing(value_list);
end;

run;

 Here's an untested sketch of how that may look. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 10026 views
  • 1 like
  • 3 in conversation