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

I'm wondering how I can extract an item from a list contained in a macro variable. I know I can do something like this using arrays inside of a data step, but am seeking a way to do this outside of a data step.

 

I've included a simplified example, in which I create a macro variable containing a list and want to extract the 2nd item. 

 

%let list = dog cat mouse;
%put &list$2; /*want this to return cat/*

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

%put &list$2;

is not really the syntax you want. It writes to the log a string which is your variable &list with $2 appended.

 

To extract words from a string, you want to use the %scan macro function.

 

%put %scan(&list,2);
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

%put &list$2;

is not really the syntax you want. It writes to the log a string which is your variable &list with $2 appended.

 

To extract words from a string, you want to use the %scan macro function.

 

%put %scan(&list,2);
--
Paige Miller
Tom
Super User Tom
Super User

What do you consider the second "item"?

A macro variable is just a string of characters.

 

If you want to treat the macro variable as a space delimited list of values then you can use the %SCAN() function

%let second=%scan(&list,2,%str( ));

Or if you use the macro variable value to generate actual SAS code you can then use the SCAN() function.

second = scan("&list",2,' ');

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 2379 views
  • 3 likes
  • 3 in conversation