Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(Diagnosis1 Diagnosis2) str1 Diagnosis3 str4(Diagnosis4 Diagnosis5) 3 3 "3" "T345" "T345" 3 3 "4" "T88" "T77" 34 4 "4" "T76" "T88" 3 3 "4" "T76" "A76" 3 4 "3" "A89" "A89" 3 3 "4" "A09" "A09" 4 5 "6" "T89" "T89" end
Question:
I would like to create a loop (code done) and replace all string variables starting with T8* to binary variables
CODE:
forvalues p = 4/5 {
generate Diagx`p' = 0
replace Diagx`p' = 1 if Diagnosis`p' == substr("T345",2,.) | Diagnosis`p' == substr("T8",1,.)
replace Diagx`p' = 2 if Diagnosis`p' == "T77"
label values Diagx`p' Diagx
}
However, stata still doesn't read my code to substitute all the T88 and T89 to binary variable = 1
What am I doing wrong please?
Comment