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

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