I'm trying to label variables in a forvalues
loop on Stata version 11.2.
However, I can't get the extended string function word # of string
idiom to work:
local names `""Growth" "Mature" "All""'
forvalues i = 0/2 {
local name : word `i' of `names'
display "`name'"
}
This follows a Statalist solution, but I keep getting invalid syntax
errors.
I also tried local names Growth Mature All
, but that doesn't work either.
Is there a way to do this algorithmically?
word 0
won't work. Stata starts counting at 1
. Change your loop to 1/3
and it will work.
Also, consider tokenize
:
. tokenize `""Growth" "Mature" "All""' . forval i = 1/3 { 2. di "``i''" 3. } Growth Mature All
tokenize
splits a string into tokens, meaning words separated by spaces unless quotation marks bind words together. The tokens have names 1
, 2
, 3
. As you go round the loop, i
is first 1
, so the request is to display the first macro, etc.