Hello,
I used sencode (https://ideas.repec.org/c/boc/bocode/s417701.html) to encode and replace many string variables with values of either ("Yes", "No", or "Unknown"). First I changed "Unknown" to equal "" and then I used sencode to change the Yes/No responses to numeric values with labels. I used sencode so that I could replace the variables instead of having to generate new variables and then dropping the old ones, which would be required with encode. However, unlike encode where "No" will always equal 1 and "Yes" will always equal 2 based on their alphabetical order, sencode applies the 1/2 values based on sequential order of appearance in the data. Therefore, in some of my variables No = 1 and Yes = 2 and in others No = 2 and Yes = 1. It would be a great feature if sencode had an "alphabetical" option to mimic the way encode works. I'd like to change all of these variables to have values/labels of No = 0 and Yes = 1. I planned to use the code below once I finished, but now it would mislabel the variables where 1 = Yes and 2 = No. Does anyone have a trick for applying a version of the code below or something similar that will account for the different values for Yes and No across variables (either 1 and 2 or 2 and 1, respectively)?
Thank you very much for your time and help.
Tom
I used sencode (https://ideas.repec.org/c/boc/bocode/s417701.html) to encode and replace many string variables with values of either ("Yes", "No", or "Unknown"). First I changed "Unknown" to equal "" and then I used sencode to change the Yes/No responses to numeric values with labels. I used sencode so that I could replace the variables instead of having to generate new variables and then dropping the old ones, which would be required with encode. However, unlike encode where "No" will always equal 1 and "Yes" will always equal 2 based on their alphabetical order, sencode applies the 1/2 values based on sequential order of appearance in the data. Therefore, in some of my variables No = 1 and Yes = 2 and in others No = 2 and Yes = 1. It would be a great feature if sencode had an "alphabetical" option to mimic the way encode works. I'd like to change all of these variables to have values/labels of No = 0 and Yes = 1. I planned to use the code below once I finished, but now it would mislabel the variables where 1 = Yes and 2 = No. Does anyone have a trick for applying a version of the code below or something similar that will account for the different values for Yes and No across variables (either 1 and 2 or 2 and 1, respectively)?
Code:
findname, all(inlist(@, ., 1, 2)) local(valchange) label define yesno 0 "No" 1 "Yes" foreach v of local valchange { replace `v' = 0 if `v' == 2 label val `v' yesno }
Tom
Comment