DATA Step, Macro, Functions and more

Turning local macro variable into global using proc sql into:

Reply
Regular Learner
Posts: 1

Turning local macro variable into global using proc sql into:

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;
Super User
Posts: 11,343

Re: Turning local macro variable into global using proc sql into:

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;
Super User
Posts: 19,868

Re: Turning local macro variable into global using proc sql into:

[ Edited ]

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. 

Ask a Question
Discussion stats
  • 2 replies
  • 142 views
  • 1 like
  • 3 in conversation