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,' ');

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 1000 views
  • 3 likes
  • 3 in conversation