Dear Statalist community,
I am encountering an issue when running parallel loops. Since my real dataset is confidential, I am demonstrating the problem using a mock dataset.
My goal is to generate a string variable, color_name, that contains the full color names based on an existing variable, color_code. The variable color_code includes some missing values, and I would like color_name to remain missing in those cases as well.
The problem is that when I run the code below, observations with missing values end up with "red" in color_name instead of missing. After examining the output more closely, I noticed that during the first iteration of the loop, color_name is set to "red" for all observations. In subsequent iterations, this value is correctly replaced for observations whose color_code appears in list1. However, for observations with missing values (id number 10) or those whose codes are not present in list1 (id number 9) the initial value "red" remains.
I was hoping to get some insight into why Stata behaves this way and how I might modify my code to avoid this issue.
I am using Stata version 18.0 on Windows 10.
I am encountering an issue when running parallel loops. Since my real dataset is confidential, I am demonstrating the problem using a mock dataset.
My goal is to generate a string variable, color_name, that contains the full color names based on an existing variable, color_code. The variable color_code includes some missing values, and I would like color_name to remain missing in those cases as well.
The problem is that when I run the code below, observations with missing values end up with "red" in color_name instead of missing. After examining the output more closely, I noticed that during the first iteration of the loop, color_name is set to "red" for all observations. In subsequent iterations, this value is correctly replaced for observations whose color_code appears in list1. However, for observations with missing values (id number 10) or those whose codes are not present in list1 (id number 9) the initial value "red" remains.
I was hoping to get some insight into why Stata behaves this way and how I might modify my code to avoid this issue.
I am using Stata version 18.0 on Windows 10.
Code:
clear
set obs 10
gen id = _n
input str12 color_code
rd
blk
bl
yl
vl
cy
gr
wt
pnk
end
gen color_name = ""
local list1 "rd" "blk" "bl" "yl" "vl" "cy" "gr" "wt"
local list2 "red" "black" "blue" "yellow" "violet" "cyan" "green" "white"
local n : word count `list1'
di "`n'"
forvalues i = 1/`n' {
local x : word `i' of `list1'
local y : word `i' of `list2'
di "`x' "
di " `y'"
replace color_name = "`y'" if color_code == "`x'"
list color_code color_name in 1/10
}

Comment