DATA Step, Macro, Functions and more

Creating a list of indexed names

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Creating a list of indexed names

May somebody help me with SAS syntax. 

 
I would like to create a list
 
%let listVars = a1|a2|a3|a4|a5;
 
OR a dataset
 
dataset myListVars;
input item $32.;
dataline;
a1
a2
a3
a4
a5
;
 
How can I do it without listing ALL names. Is it possible to do something like: 
 
%let listVars = a1 – a5;
 
OR 
dataset myListVars;
input item $32.;
dataline;
a1 - a5
;
 
Thank you
 
Julia

Accepted Solutions
Solution
‎01-25-2016 05:07 PM
Trusted Advisor
Posts: 1,115

Re: Creating a list of indexed names

[ Edited ]

Once you have the dataset, creating the macro variable is easy, too:

data myListVars;
length item $32;
do _n_=1 to 5;
  item=cats('a',_n_);
  output;
end;
run;

proc sql noprint;
select *
into :listvars separated by '|'
from myListVars;
quit;

View solution in original post


All Replies
Super User
Posts: 17,829

Re: Creating a list of indexed names

What are you planning to use this for down the line?

 

You could use a loop, but there's probably better ways to do whatever you're trying to do is my guess.

data want;
     do i=1 to 99;
          Item=catt("A", put(i, z2.));
          output;
     end;
run;

 

Contributor
Posts: 38

Re: Creating a list of indexed names

Are you trying to put a list of variable names from a table that already exists into a macro variable? Or, are you trying to create a table with a list of variable names?

Solution
‎01-25-2016 05:07 PM
Trusted Advisor
Posts: 1,115

Re: Creating a list of indexed names

[ Edited ]

Once you have the dataset, creating the macro variable is easy, too:

data myListVars;
length item $32;
do _n_=1 to 5;
  item=cats('a',_n_);
  output;
end;
run;

proc sql noprint;
select *
into :listvars separated by '|'
from myListVars;
quit;
Contributor
Posts: 27

Re: Creating a list of indexed names

Thank you very much. This is exactly what I need. I just didn't know the first part. 

Trusted Advisor
Posts: 1,115

Re: Creating a list of indexed names

[ Edited ]

You're welcome. But if you only need the macro variable, you can get it even more easily:

data _null_;
array x[5] (1:5);
call symputx('listvars', 'a'||catx('|a', of x:));
run;

 

Contributor
Posts: 27

Re: Creating a list of indexed names

Thank you! Even better!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 267 views
  • 3 likes
  • 4 in conversation