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

How can I get the variable of a macro using index?

 

%vars = apple ball cats ;

 

How can I get apple ball or cat based on index?

 

I have convert it to table and use proc sql to get the variables.  Is there any way to get variable based on index without  converting it to table and using proc sql?

 

I could easily do vars[0] to get apple and vars [2] to get cats in python.

 

Thank you,

Shone

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Since all you have done is assign a string of characters to a macro variable you do not have a "list" or an "array" so your Python example is off base.  If you want to interpret that string as a space delimited list then use the %scan() function.

%let list=apple ball cats ;
%let i=1 ;
%let item=%scan(&list,&i,%str( ));

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

Since all you have done is assign a string of characters to a macro variable you do not have a "list" or an "array" so your Python example is off base.  If you want to interpret that string as a space delimited list then use the %scan() function.

%let list=apple ball cats ;
%let i=1 ;
%let item=%scan(&list,&i,%str( ));
Shone
Calcite | Level 5

Sorry but how would you get the rest of item except the item from given index? For instance, if we give 1 we want ball and cat. 

Tom
Super User Tom
Super User

@Shone wrote:

Sorry but how would you get the rest of item except the item from given index? For instance, if we give 1 we want ball and cat. 


I cannot think of an case where that would be useful. The main purpose of macro code is to generate code. So perhaps you could generate code like:

where findw("&list",x,' ','t')
  and x ne "%scan(&list,&i,%str( ))"
;

So if X is in the list but not equal to the i-th item in the list.

Shone
Calcite | Level 5
Thank you so much. I was looking it in wrong way. If given the var list above, how can I get the max index of that list?
Tom
Super User Tom
Super User

@Shone wrote:
Thank you so much. I was looking it in wrong way. If given the var list above, how can I get the max index of that list?

To find how many words are in a delimited list use the COUNTW() function. To use that it macro code use the %SYSFUNC() macro function.

%let n=%sysfunc(countw(a b c,%str( )));

Will set N to 3.

Shone
Calcite | Level 5

Thank you so much. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 2550 views
  • 0 likes
  • 2 in conversation