BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NinnaLinde
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
NinnaLinde
Fluorite | Level 6
I am sorry if I am not clear enough.

The macrovariables doesn't get created. The log doesn't contain these five macrovariables in the list when I run the %put _user_
Kurt_Bremser
Super User

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
Fluorite | Level 6
Thank you very much for your time!
Kurt_Bremser
Super User

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 975 views
  • 2 likes
  • 3 in conversation