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

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
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

6 REPLIES 6
Reeza
Super User

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;

 

DanZ
Obsidian | Level 7

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?

FreelanceReinh
Jade | Level 19

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;
braverju
Obsidian | Level 7

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

FreelanceReinh
Jade | Level 19

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;

 

braverju
Obsidian | Level 7

Thank you! Even better!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 1058 views
  • 3 likes
  • 4 in conversation