Hi Everyone,
I have a list of value in the following format/statement:
%LET list= a b c d e f g h e;
a b c d e f g h e;
I would like to use this list to create a dataset with a column containing these value.
So the data should look like
var
a
b
c
...
e
Can you please help me with that?
Thank you,
HHC
So you have a macro variable with a space delimited list of words and you want to convert it into a dataset? So it this example the macro variable is name LIST.
%LET list= a b c d e f g h e;
You haven't said what dataset you want to create or what name you want to use for the variable to hold the words.
So let's make a dataset named WANT and use WORD as the name of the variable.
You also did not say the maximum number of characters that any word could contain. Let's just assume that is 8.
So a simple DO loop should do the trick.
data want ;
length word $8 ;
do index=1 to countw("&list",' ');
word = scan("&list",index,' ');
output;
end;
run;
I think you may need PROC TRANSPOSE ?
So you have a macro variable with a space delimited list of words and you want to convert it into a dataset? So it this example the macro variable is name LIST.
%LET list= a b c d e f g h e;
You haven't said what dataset you want to create or what name you want to use for the variable to hold the words.
So let's make a dataset named WANT and use WORD as the name of the variable.
You also did not say the maximum number of characters that any word could contain. Let's just assume that is 8.
So a simple DO loop should do the trick.
data want ;
length word $8 ;
do index=1 to countw("&list",' ');
word = scan("&list",index,' ');
output;
end;
run;
Hi Tom,
Thank you again for helping me out.
It works perfectly.
HHC
Note that if the list is comma separated you can use it directly in the DO statement without the need for INDEX or SCAN().
So for numeric values:
%let list=1,2,3,4,5;
data want;
do value=&list;
output;
end;
run;
And for character variable include the quotes in the macro variable LIST. Note it is best to define the length of the variable first otherwise SAS will guess a length based on the first value in the list.
%let list='a',"b",'c' ;
data want;
length value $8;
do value=&list;
output;
end;
run;
I would like to go some steps backwards: why do have a list of values in a macro-variable? Is this a homework-task or a real-life problem? If it is the later, than it is most likely possible to create the required dataset without the detour using a macro-variable.
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.