Hi,
I would like to loop through a list of strings to fill in a string variable with a condition. Basically, to a number "n" correspond a country 3 letter name. I know how to loop through variables but I'm struggling with strings.
Note that I am not interested in other methods to do achieve this goal. I'd like to learn how to loop through strings for future reference! Thanks!
I would like to loop through a list of strings to fill in a string variable with a condition. Basically, to a number "n" correspond a country 3 letter name. I know how to loop through variables but I'm struggling with strings.
Code:
sysuse auto, clear bys rep78: gen n=_n gen country = . * These are strings (not variables) local var "AUD BEF CAD CHF DEM DKK EUR FRF GBP ITL JPY NLG NOK NZD SEK" display "`var'" local n: word count `var' forvalues i = 1/`n' { local v1 : word `i' of "`var'" replace country = "`v1'" if n==`i' }
Comment