Hello everyone
I want to make 5 macro variables that contain a list of variables. It doesn't seem to work for me, and I cant figure out why.
%let fil = var1;
%let tal = 1;
proc contents data= svar out=varliste varnum;
run;
data var1 var2 var3 var4 var5 ;
set varliste(keep=name varnum);
if varnum in (120, 127:277) then output var1;
if varnum in (121, 278:428) then output var2;
if varnum in (122, 429:579) then output var3;
if varnum in (123, 580:730) then output var4;
if varnum in (124, 731:881) then output var5;
run;
%macro varliste (tal, fil);
proc sql;
select name into :varliste&tal.
from &fil;
run;
%mend varliste;
%varliste (1, var1);
%varliste (2, var2);
%varliste (3, var3);
%varliste (4, var4);
%varliste (5, var5);
%put _user_;
I hope someone can help me!
Thanks,
Ninna
Macro variables created in a macro will be local to that macro, unless they already exist in the global symbol table.
Add a %GLOBAL statement:
%macro varliste (tal, fil);
%global varliste&tal;
proc sql;
select name into :varliste&tal.
from &fil;
quit;
%mend varliste;
Also note that PROC SQL is terminated by a QUIT and not a RUN statement.
Since you want to create a list of names in each macro variable, you need to tell SQL that:
%macro varliste (tal, fil);
%global varliste&tal;
proc sql;
select name into :varliste&tal. separated by " "
from &fil;
quit;
%mend varliste;
If you want to use the list in SQL, change the separator to a comma.
@NinnaLinde wrote:
Hello everyone
I want to make 5 macro variables that contain a list of variables. It doesn't seem to work for me, and I cant figure out why.
We don't know what this means. You haven't told us what is wrong, and what should happen that would fix it.
I'm going to take a guess that you need a %GLOBAL statement inside your macro named %VARLISTE
%global varliste&tal;
If that doesn't fix the problem, then explain the problem, and explain what would be the desired result.
And in the future, please explain what is wrong and the desired result. Don't just show us code and say it isn't working.
Macro variables created in a macro will be local to that macro, unless they already exist in the global symbol table.
Add a %GLOBAL statement:
%macro varliste (tal, fil);
%global varliste&tal;
proc sql;
select name into :varliste&tal.
from &fil;
quit;
%mend varliste;
Also note that PROC SQL is terminated by a QUIT and not a RUN statement.
Since you want to create a list of names in each macro variable, you need to tell SQL that:
%macro varliste (tal, fil);
%global varliste&tal;
proc sql;
select name into :varliste&tal. separated by " "
from &fil;
quit;
%mend varliste;
If you want to use the list in SQL, change the separator to a comma.
And, seeing that you have ~1000 variables in your initial dataset, I question that design. Data is usually easier to deal with in a long layout, so you should look at this aspect of your task.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.