The Code for position in list is
%let list_pos=%sysfunc(countw(%substr(&list,1,%index(&list,16AUG2014)+1)));
Cheers
Question
This might be a rather simple question but I am new to SAS and am clueless even after researching on this in Google.
I have a macro variable as -
%let list = 12AUG2013 13AUG2013 15AUG2013 16AUG2014 09SEPT2014;
I need to get the following things -
a) Total words in list: In R this would be length(list). But in SAS, length counts each character. COUNTW does not work. Anyway I can do this?
b) Finding the ith word: If I need the 3rd element in this list, I would say list[3] in R. How can I do that in SAS?
c) Finding the position of an element: Suppose I need to know which position 16AUG2014 is at in the list variable, how can I get it?
Thanks for all the help!
Solution 2
The Code for position in list is
%let list_pos=%sysfunc(countw(%substr(&list,1,%index(&list,16AUG2014)+1)));
Cheers
OTHER TIPS
As you're asking about macro variables, it's a little different to using SAS data-step functions. Your questions provide a useful example of how they differ. Some data-step functions have macro function equivalents %SCAN
, %SUBSTR
etc. Others will require the use of %SYSFUNC
, which allows most SAS datastep functions to be converted into macro functions.
So, referring to your example:
%let list = 12AUG2013 13AUG2013 15AUG2013 16AUG2014 09SEPT2014; %let list_numwords = %sysfunc(countw(&list)); /* This example shows the use of SYSFUNC */ %let list_word3 = %scan(&list,3); /* These examples show the use of SCAN and INDEX, inbuilt macro functions*/ %let list_pos16AUG2014 = %index(&list,16AUG2014);
The code creates new macro variables that store the answers to your questions a,b and c* respectively.
* If you require the word number of 16AUG2014
(i.e. 4), then this is a little more difficult as I don't think there's a string function for this in SAS. It would involve using a combination of COUNT
and %SUBSTR
.