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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

5 REPLIES 5
Phil_NZ
Barite | Level 11

I think you may need PROC TRANSPOSE ?

 

     

 
Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Tom
Super User Tom
Super User

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;

hhchenfx
Barite | Level 11

Hi Tom,

Thank you again for helping me out.

It works perfectly.

HHC

Tom
Super User Tom
Super User

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;
andreas_lds
Jade | Level 19

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 702 views
  • 0 likes
  • 4 in conversation