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
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( ));
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( ));
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.
@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 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.
Thank you so much.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.