Let's say I have the macro:
local words = "first second third fourth"
I want to select the first 3 words of it. I know that I can select, say the second, the word using:
local select: word 2 of `words'
di "`select'"
However, I would like to have a line that selects the first N
words (in this case N=3
) of the said macro. For now, I am using a loop structure that seems quite overkill for such a simple procedure.
local words = "first second third fourth"
local select: word 1 of `words'
forvalues i=2/3 {
local rest : word `i' of `words'
local select `select' `rest'
}
di "`select'"
first second third
Any better solution? Thank you
If you knew there were 4 words you could look for the last space and split before and after. I am not picking up that you know that in general.
Here is another general method.
local words = "first second third fourth"
tokenize "`words'"
local select "`1' `2' `3'"
di "`select'"
first second third
Here is another general method:
local words = "first second third fourth"
mata : tokens = tokens(st_local("words"))
mata : st_local("select", invtokens(tokens[1..3]))
di "`select'"
first second third
That is easier to extend to larger numbers of words (just replace 3).