The first chunk of code below seems to work as expected. But I thought I would be able to ask -tokenize- to separate "tokens" by comma as a delimiter.
(The reason for not simply using single spaces between each word/token, is that I want the words in the local macros to line up, as shown below, making it easy to check that my code is correct. Also, a clarification: I intenionally avoid retaining the original variable names as I never modify the original data. So, I use -rename- rather than -clonevariable-.)
Code:
// Define local macros of item names, one for Norwegian and one for English local nor_items "alene, gange, hjelp_1, hjelp_2, hjelp_3, hjelp_4, hjelp_5" local eng_items "alone, dist, Nclean, Ngroc, Nrep, Ndigi, Nbed" foreach parent in mo fa { // Loop through the items while "`nor_items'" != "" & "`eng_items'" != "" { // Get the current token from each macro gettoken nor_item nor_items: nor_items gettoken eng_item eng_items: eng_items // Remove the comma from the current tokens local nor_item = subinstr("`nor_item'", ",", "", 1) local eng_item = subinstr("`eng_item'", ",", "", 1) // Rename variables fre `parent'r`nor_item' rename `parent'r`nor_item' `parent'_`eng_item' // Recode missing values recode `parent'_`eng_item' (9996/9999 = .) } }
But this code does not work:
Code:
// Define local macros of item names, one for Norwegian and one for English local nor_items "alene, gange, hjelp_1, hjelp_2, hjelp_3, hjelp_4, hjelp_5" local eng_items "alone, dist, Nclean, Ngroc, Nrep, Ndigi, Nbed" foreach parent in mo fa { // Loop through the items // TRYING TO AVOID THIS PART while "`nor_items'" != "" & "`eng_items'" != "" { // Get the current token from each macro gettoken nor_item nor_items: nor_items, parse(",") gettoken eng_item eng_items: eng_items, parse(",") // TRYING TO AVOID THIS PART Remove the comma from the current tokens // local nor_item = subinstr("`nor_item'", ",", "", 1) // local eng_item = subinstr("`eng_item'", ",", "", 1) // Rename variables fre `parent'r`nor_item' rename `parent'r`nor_item' `parent'_`eng_item' // Recode missing values recode `parent'_`eng_item' (9996/9999 = .) } }
Code:
variable far not found
Yes, the first code works. But can the code be simplified, in the interest of readers when the code is published? Any help would be appreciated.
Even though Stata's error message does refer to "far", I suspect the challenge lies in my use of either one OR several spaces after the comma to separate words in the local macro.
0 Response to loop in parallel with tokenize, comma as delimiter
Post a Comment